The Beam Node SDK makes it easy to interact with Beam deployments and tasks from any JavaScript environment.
With the Node SDK, you can:
- List and call deployments
- Stream real-time responses
- Monitor and cancel tasks
Installation
Install the package with npm
:
npm install @beamcloud/beam-js
…or using yarn
:
yarn add @beamcloud/beam-js
Importing
Import the package:
import beam from "@beamcloud/beam-js";
Then, initialize the Beam Client, passing your Beam Token as a parameter:
const client = await beam.init("YOUR_BEAM_TOKEN");
You can find your Beam Token in the API Keys section of the dashboard.
Usage
Deployments
list
Retrieves a list of all deployments.
const deployments = await client.deployments.list();
get
Fetches a deployment by ID.
const deployment = await client.deployments.get("DEPLOYMENT_ID");
delete
Deletes a deployment by ID.
await client.deployments.delete("DEPLOYMENT_ID");
refresh
Reloads the deployment data.
const deployment = await client.deployments.get("DEPLOYMENT_ID");
await deployment.refresh();
call
Makes an API request to the deployment.
await client.deployments.call("DEPLOYMENT_ID");
realtime
Opens a WebSocket connection for real-time updates.
const stream = deployment.realtime();
stream.on("data", (data) => {
console.log(data);
});
Here’s a more complete example:
import beam from "@beamcloud/beam-js";
const streamResponse = async () => {
const client = await beam.init(process.env.BEAM_TOKEN);
const deployment = await client.deployments.get({
id: process.env.BEAM_DEPLOYMENT_ID,
});
const connection = await deployment.realtime();
const payload = {
event: "Echo this back",
};
connection.onmessage = (message) => {
console.log(`🎉 Response: ${message.data}`);
};
connection.send(JSON.stringify(payload));
setTimeout(() => {
connection.close();
}, 1000);
};
streamResponse();
Tasks
list
Retrieves a list of all tasks.
const tasks = await client.tasks.list();
get
Fetches a specific task by ID.
const task = await client.tasks.get("TASK_ID");
console.log(task.data);
delete
Deletes a task by ID.
await client.tasks.delete("TASK_ID");
cancel
Cancel a task.
const task = await client.tasks.get("TASK_ID");
await task.cancel();
refresh
Reloads the task data.
const task = await client.tasks.get("TASK_ID");
await task.refresh();