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.
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:
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.
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.
The following example shows how to get the results of an asynchronous run.
Copy
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();
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.
Copy
const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;import runpodSdk from "runpod-sdk";// Function to pause execution for a specified timeconst 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 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.
Copy
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();
The maximum size for a payload that can be sent using yield to stream results is 1 MB.
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.
Copy
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);
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.
Copy
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();