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 Go SDK, ensure that you have:

Set your Endpoint Id

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

package main

import (
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com.runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
// Retrieve the API key and base URL from environment variables
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

// Check if environment variables are set
if apiKey == "" {
log.Fatalf("Environment variable RUNPOD_API_KEY is not set")
}
if baseURL == "" {
log.Fatalf("Environment variable RUNPOD_BASE_URL is not set")
}


// Use the endpoint object
// ...
}

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

The following are actions you use on the

Here is the revised documentation based on the Go Sample:

Run the Endpoint

Run the Endpoint using 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.

package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com.runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}

jobInput := rpEndpoint.RunSyncInput{
JobInput: &rpEndpoint.JobInput{
Input: map[string]interface{}{
"prompt": "Hello World",
},
},
Timeout: sdk.Int(120),
}

output, err := endpoint.RunSync(&jobInput)
if err != nil {
panic(err)
}

data, _ := json.Marshal(output)
fmt.Printf("output: %s\n", data)
}

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.

package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}

jobInput := rpEndpoint.RunInput{
JobInput: &rpEndpoint.JobInput{
Input: map[string]interface{}{
"mock_delay": 95,
},
},
RequestTimeout: sdk.Int(120),
}

output, err := endpoint.Run(&jobInput)
if err != nil {
panic(err)
}

data, _ := json.Marshal(output)
fmt.Printf("output: %s\n", data)
}

Get results from an asynchronous run

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

package main

import (
"encoding/json"
"fmt"
"log"
"os"
"time"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}

// Initiate the asynchronous run
jobInput := rpEndpoint.RunInput{
JobInput: &rpEndpoint.JobInput{
Input: map[string]interface{}{"mock_delay": 95},
},
RequestTimeout: sdk.Int(120),
}
runOutput, err := endpoint.Run(&jobInput)
if err != nil {
log.Fatalf("Failed to initiate the run: %v", err)
}

// Extract the ID from the run output
runID := *runOutput.Id
fmt.Printf("Run ID: %s\n", runID)

// Prepare the input for status polling
statusInput := rpEndpoint.StatusInput{
Id: sdk.String(runID),
}

// Poll the status until it completes or fails
var statusOutput *rpEndpoint.StatusOutput
for {
statusOutput, err = endpoint.Status(&statusInput)
if err != nil {
log.Printf("Error checking status: %v", err)
time.Sleep(5 * time.Second)
continue
}

statusJSON, _ := json.Marshal(statusOutput)
fmt.Printf("Status: %s\n", statusJSON)

if *statusOutput.Status == "COMPLETED" || *statusOutput.Status == "FAILED" {
break
}

time.Sleep(5 * time.Second)
}

// Retrieve the final result (assuming it's available in the status output)
if *statusOutput.Status == "COMPLETED" {
fmt.Println("Run completed successfully!")
// Handle the completed run's output if needed
} else {
fmt.Println("Run failed!")
// Handle the failed run if needed
}
}

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.

package main

import (
"encoding/json"
"fmt"

"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {

apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
panic(err)
}

request, err := endpoint.Run(&rpEndpoint.RunInput{
JobInput: &rpEndpoint.JobInput{
Input: map[string]interface{}{
"prompt": "Hello World",
},
},
})
if err != nil {
panic(err)
}

streamChan := make(chan rpEndpoint.StreamResult, 100)

err = endpoint.Stream(&rpEndpoint.StreamInput{Id: request.Id}, streamChan)
if err != nil {
// timeout reached, if we want to get the data that has been streamed
if err.Error() == "ctx timeout reached" {
for data := range streamChan {
dt, _ := json.Marshal(data)
fmt.Printf("output:%s\n", dt)
}
}
panic(err)
}

for data := range streamChan {
dt, _ := json.Marshal(data)
fmt.Printf("output:%s\n", dt)
}

}
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.

package main

import (
"encoding/json"
"fmt"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {

apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
panic(err)
}

healthInput := rpEndpoint.StatusInput{
Id: sdk.String("20aad8ef-9c86-4fcd-a349-579ce38e8bfd-u1"),
}
output, err := endpoint.Status(&healthInput)
if err != nil {
panic(err)
}

healthData, _ := json.Marshal(output)
fmt.Printf("health output: %s\n", healthData)

}

Status

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

package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {

apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}
input := rpEndpoint.StatusInput{
Id: sdk.String("5efff030-686c-4179-85bb-31b9bf97b944-u1"),
}
output, err := endpoint.Status(&input)
if err != nil {
panic(err)
}
dt, _ := json.Marshal(output)
fmt.Printf("output:%s\n", dt)
}

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.

package main

import (
"encoding/json"
"fmt"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {

apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
panic(err)
}

cancelInput := rpEndpoint.CancelInput{
Id: sdk.String("00edfd03-8094-46da-82e3-ea47dd9566dc-u1"),
}
output, err := endpoint.Cancel(&cancelInput)
if err != nil {
panic(err)
}

healthData, _ := json.Marshal(output)
fmt.Printf("health output: %s\n", healthData)

}

Timeout

You can set the maximum time to wait for a response from the endpoint using the RequestTimeout field in the RunInput struct.

package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}

jobInput := rpEndpoint.RunInput{
JobInput: &rpEndpoint.JobInput{
Input: map[string]interface{}
RequestTimeout: sdk.Int(120),
}

output, err := endpoint.Run(&jobInput)
if err != nil {
panic(err)
}

data, _ := json.Marshal(output)
fmt.Printf("output: %s\n", data)
}

Execution policy

You can specify the TTL (Time-to-Live) and ExecutionTimeout values for the job using the Input map of the JobInput struct.

package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}

jobInput := rpEndpoint.RunInput{
JobInput: &rpEndpoint.JobInput{
Input: map[string]interface{}{
"ttl": 3600, // Set the TTL value, e.g., 3600 seconds (1 hour)
"execution_timeout": 300, // Set the ExecutionTimeout value, e.g., 300 seconds (5 minutes)
},
},
RequestTimeout: sdk.Int(120),
}

output, err := endpoint.Run(&jobInput)
if err != nil {
panic(err)
}

data, _ := json.Marshal(output)
fmt.Printf("output: %s\n", data)
}

For more information, see Execution policy.

Purge Queue

Create an instance of the PurgeQueueInput struct and set the desired values. Call the PurgeQueue method of the Endpoint with the PurgeQueueInput instance.

PurgeQueue() doesn't affect Jobs in progress.

package main

import (
"fmt"
"log"
"os"

"github.com/runpod/go-sdk/pkg/sdk"
"github.com/runpod/go-sdk/pkg/sdk/config"
rpEndpoint "github.com/runpod/go-sdk/pkg/sdk/endpoint"
)

func main() {
apiKey := os.Getenv("RUNPOD_API_KEY")
baseURL := os.Getenv("RUNPOD_BASE_URL")

endpoint, err := rpEndpoint.New(
&config.Config{ApiKey: &apiKey},
&rpEndpoint.Option{EndpointId: &baseURL},
)
if err != nil {
log.Fatalf("Failed to create endpoint: %v", err)
}

purgeQueueInput := rpEndpoint.PurgeQueueInput{
RequestTimeout: sdk.Int(5), // Set the request timeout to 5 seconds
}

purgeQueueOutput, err := endpoint.PurgeQueue(&purgeQueueInput)
if err != nil {
panic(err)
}

fmt.Printf("Status: %s\n", *purgeQueueOutput.Status)
fmt.Printf("Removed: %d\n", *purgeQueueOutput.Removed)
}