Execute Code
POST
/v1/sandbox/{sandbox_id}/codePOST
/v1/sandbox/{'{sandbox_id}'}/codeYour 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
| Parameter | Type | Required | Description |
|---|---|---|---|
sandbox_id | string | Yes | Sandbox ID or name |
Request Body
| Property | Type | Required | Description |
|---|---|---|---|
context_id | string | No | ID of an existing context to execute within. If omitted, a new ephemeral context is used |
code | string | Yes | The source code to execute |
language | string | Yes | The programming language (e.g., python, javascript) |
SSE Event Types
Event type | Description |
|---|---|
stdout | Standard output produced by the code |
stderr | Standard error output produced by the code |
exit | Process exit code (e.g., "0" for success) |
error | Execution error message |
Responses
200 Success
SSE stream of JSON event objects.
| Property | Type | Description |
|---|---|---|
type | string | Event type: stdout, stderr, exit, or error |
data | string | Event payload |
default An unexpected error response.
| Property | Type | Description |
|---|---|---|
code | integer | |
message | string | |
details | object[] |
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"}]}
