Everstack
API ReferenceSandboxExecute Code

Execute Code

POST/v1/sandbox/{sandbox_id}/code
POST/v1/sandbox/{'{sandbox_id}'}/code

Your instance endpoint, shown on the instance page in the dashboard. Each instance has its own host; there is no shared API host.

Description

Executes code within a sandbox and streams the output as Server-Sent Events (SSE). The response is a stream of events that deliver stdout, stderr, exit codes, and errors as the code runs. Use context_id to maintain state across multiple executions (e.g., variables defined in one call are available in subsequent calls with the same context).

Path Parameters

ParameterTypeRequiredDescription
sandbox_idstringYesSandbox ID or name

Request Body

PropertyTypeRequiredDescription
context_idstringNoID of an existing context to execute within. If omitted, a new ephemeral context is used
codestringYesThe source code to execute
languagestringYesThe programming language (e.g., python, javascript)

SSE Event Types

Event typeDescription
stdoutStandard output produced by the code
stderrStandard error output produced by the code
exitProcess exit code (e.g., "0" for success)
errorExecution error message

Responses

200 Success

SSE stream of JSON event objects.

PropertyTypeDescription
typestringEvent type: stdout, stderr, exit, or error
datastringEvent payload

default An unexpected error response.

PropertyTypeDescription
codeinteger
messagestring
detailsobject[]

Request

curl -X POST "https://your-instance.example.com/v1/sandbox/sb_abc123/code" \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -H "Accept: text/event-stream" \
  --no-buffer \
  -d '{
    "context_id": "ctx_xyz789",
    "code": "print('"'"'hello'"'"')",
    "language": "python"
  }'
const response = await fetch(
  "https://your-instance.example.com/v1/sandbox/sb_abc123/code",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer <token>",
      "Content-Type": "application/json",
      Accept: "text/event-stream",
    },
    body: JSON.stringify({
      context_id: "ctx_xyz789",
      code: "print('hello')",
      language: "python",
    }),
  }
);

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const chunk = decoder.decode(value);
  for (const line of chunk.split("\n")) {
    if (line.startsWith("data: ")) {
      const event = JSON.parse(line.slice(6));
      console.log(event); // { type: "stdout", data: "hello\n" }
    }
  }
}
import requests
import json

with requests.post(
    "https://your-instance.example.com/v1/sandbox/sb_abc123/code",
    headers={
        "Authorization": "Bearer <token>",
        "Content-Type": "application/json",
        "Accept": "text/event-stream",
    },
    json={
        "context_id": "ctx_xyz789",
        "code": "print('hello')",
        "language": "python",
    },
    stream=True,
) as response:
    for line in response.iter_lines():
        if line and line.startswith(b"data: "):
            event = json.loads(line[6:])
            print(event)  # {"type": "stdout", "data": "hello\n"}

Response

data: {"type":"stdout","data":"hello\n"}

data: {"type":"exit","data":"0"}
{"code": 0, "message": "string", "details": [{"@type": "string"}]}