Skip to main content

Endpoints

Interacting with RunPod's endpoints is a core feature of the SDK, enabling the execution of tasks and the retrieval of results. This section covers the synchronous and asynchronous execution methods, along with checking the status of operations.

Prerequisites

Before using the RunPod JavaScript, ensure that you have:

  • Installed the RunPod JavaScript SDK.
  • Configured your API key.

Set your Endpoint Id

Set your RunPod API key and your Endpoint Id as environment variables.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);

This allows all calls to pass through your Endpoint Id with a valid API key.

In most situations, you'll set a variable name endpoint on the Endpoint class. This allows you to use the following methods or instances variables from the Endpoint class:

Run the Endpoint

Run the Endpoint with the either the asynchronous run or synchronous runSync method.

Choosing between asynchronous and synchronous execution hinges on your task's needs and application design.

Run synchronously

To execute an endpoint synchronously and wait for the result, use the runSync method on your endpoint. This method blocks the execution until the endpoint run is complete or until it times out.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.runSync({
"input": {
"prompt": "Hello, World!",
},
});

console.log(result);

Run asynchronously

Asynchronous execution allows for non-blocking operations, enabling your code to perform other tasks while waiting for an operation to complete.

For non-blocking operations, use the run method on the endpoint. This method allows you to start an endpoint run and then check its status or wait for its completion at a later time.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
"input": {
"prompt": "Hello, World!",
},
});

console.log(result);

Get results from an asynchronous run

The following example shows how to get the results of an asynchronous run.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function main() {
const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
input: {
prompt: "Hello, World!",
},
});

console.log(result);
console.log("run response");
console.log(result);

const { id } = result; // Extracting the operation ID from the initial run response

// Check the status in a loop, similar to the working example
for (let i = 0; i < 20; i++) {
// Increase or decrease the loop count as necessary
const statusResult = await endpoint.status(id);
console.log("status response");
console.log(statusResult);

if (
statusResult.status === "COMPLETED"
|| statusResult.status === "FAILED"
) {
// Once completed or failed, log the final status and break the loop
if (statusResult.status === "COMPLETED") {
console.log("Operation completed successfully.");
console.log(statusResult.output);
} else {
console.log("Operation failed.");
console.log(statusResult);
}
break;
}

// Wait for a bit before checking the status again
await sleep(5000);
}
}

main();

Poll the status of an asynchronous run

Uses await endpoint.status(id) to check the status of the operation repeatedly until it either completes or fails. After each check, the function waits for 5 seconds (or any other suitable duration you choose) before checking the status again, using the sleep function. This approach ensures your application remains responsive and doesn't overwhelm the Runpod endpoint with status requests.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

// Function to pause execution for a specified time
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function main() {
try {
const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
input: {
prompt: "Hello, World!",
},
});

const { id } = result;
if (!id) {
console.error("No ID returned from endpoint.run");
return;
}

// Poll the status of the operation until it completes or fails
let isComplete = false;
while (!isComplete) {
const status = await endpoint.status(id);
console.log(`Current status: ${status.status}`);

if (status.status === "COMPLETED" || status.status === "FAILED") {
isComplete = true; // Exit the loop
console.log(`Operation ${status.status.toLowerCase()}.`);

if (status.status === "COMPLETED") {
console.log("Output:", status.output);
} else {
console.error("Error details:", status.error);
}
} else {
await sleep(5000); // Adjust the delay as needed
}
}
} catch (error) {
console.error("An error occurred:", error);
}
}

main();

Stream

Stream allows you to stream the output of an Endpoint run. To enable streaming, your handler must support the "return_aggregate_stream": True option on the start method of your Handler. Once enabled, use the stream method to receive data as it becomes available.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

async function main() {
const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
input: {
prompt: "Hello, World!",
},
});

console.log(result);

const { id } = result;
for await (const result of endpoint.stream(id)) {
console.log(`${JSON.stringify(result, null, 2)}`);
}
console.log("done streaming");
}

main();
note

The maximum size for a payload that can be sent using yield to stream results is 1 MB.

Health check

Monitor the health of an endpoint by checking its status, including jobs completed, failed, in progress, in queue, and retried, as well as the status of workers.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);

const health = await endpoint.health();
console.log(health);

Status

Use the status method and specify the id of the run to get the status of a run.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

async function main() {
try {
const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
input: {
prompt: "Hello, World!",
},
});

const { id } = result;
if (!id) {
console.error("No ID returned from endpoint.run");
return;
}

const status = await endpoint.status(id);
console.log(status);
} catch (error) {
console.error("An error occurred:", error);
}
}

main();

Cancel

You can cancel a Job request by using the cancel() function on the run request. You might want to cancel a Job because it's stuck with a status of IN_QUEUE or IN_PROGRESS, or because you no longer need the result.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

async function main() {
try {
const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
input: {
prompt: "Hello, World!",
},
});

const { id } = result;
if (!id) {
console.error("No ID returned from endpoint.run");
return;
}

const cancel = await endpoint.cancel(id);
console.log(cancel);
} catch (error) {
console.error("An error occurred:", error);
}
}

main();

Timeout

To set a timeout on a run, pass a timeout value to the run method. Time is measured in milliseconds.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
"input": {
"prompt": "Hello, World!",
},
}, 5000);

console.log(result);

Execution policy

You can set the maximum time to wait for a response from the endpoint in the policy parameter.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
const result = await endpoint.run({
"input": {
"prompt": "Hello, World!",
},
policy: {
executionTimeout: 5000,
},
});

console.log(result);

For more information, see Execution policy.

Purge queue

You can purge all jobs from a queue by using the purgeQueue() function.

purgeQueue() doesn't affect Jobs in progress.

const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
import runpodSdk from "runpod-sdk";

async function main() {
try {
const runpod = runpodSdk(RUNPOD_API_KEY);
const endpoint = runpod.endpoint(ENDPOINT_ID);
await endpoint.run({
input: {
prompt: "Hello, World!",
},
});

const purgeQueue = await endpoint.purgeQueue();
console.log(purgeQueue);
} catch (error) {
console.error("An error occurred:", error);
}
}

main();