POST /v1/sandboxes/{sandbox_id}/run_code
Runs code in the sandbox's Jupyter kernel and returns its rich results (text, images, HTML, JSON), output and any error. State persists between calls, like cells in a notebook. Needs a sandbox created from the `code-interpreter` template.

Authentication

Bearer Token (b3os_sk_ API key or b3os_pat_ token) API Key (cookie: __Host-b3os_session)

Path Parameters

sandbox_id string required path
The sandbox ID (`sbx_…`).
Example: "sbx_01m5t68q1b590rj801nbzsqzkk"

Request Body required

application/json
code string REQUIRED
language string
Enum: python, javascript
timeout_ms integer
env object

Responses

200 The cell's results.
application/json
object string REQUIRED
Enum: run_code_result
language string REQUIRED
Enum: python, javascript
execution_count integer REQUIRED
results object[] REQUIRED
Array of:
mime_type string REQUIRED
`text/plain`, `text/html`, `text/markdown`, `application/json`, `image/png`, `image/svg+xml`, …
data string REQUIRED
The value; base64 for binary types such as `image/png`.
is_main_result boolean
The value of the cell's last expression.
stdout string REQUIRED
stderr string REQUIRED
error object | null REQUIRED
The exception the cell raised, or `null`.
name string REQUIRED
value string REQUIRED
traceback string REQUIRED
duration_ms number REQUIRED
timing object
Server-side phase durations in milliseconds with microsecond resolution: the same phases as the `Server-Timing` header, keyed `<phase>_ms`. `total_ms` is always present. Phase names are product-specific and documented per operation. Example values in this reference are illustrative, not measurements.
total_ms number REQUIRED
400 The request failed validation (`invalid_request`, `invalid_url`).
401 Missing, malformed, expired or revoked credentials (`unauthorized`).
403 Authenticated but not allowed (`forbidden`, `egress_denied`).
404 No such sandbox in this project (`sandbox_not_found`).
409 The sandbox has no code kernel (`kernel_unavailable`).
413 The request body exceeds this operation's limit.
415 The Content-Type is not accepted by this operation.
422 The kernel does not run this language (`language_unsupported`).
429 A rate, concurrency or quota limit applies (`rate_limited`, `concurrency_limited`, `quota_exceeded`).
500 Something failed inside B3OS (`internal`). Retry with backoff.
502 The agent inside the microVM did not answer (`guest_unreachable`).
503 The node or the product is not available right now (`node_unavailable`, `product_unavailable`, `capacity_unavailable`).
curl -X POST 'https://beta-api.b3os.org/v1/sandboxes/sbx_01m5t68q1b590rj801nbzsqzkk/run_code' \  -H 'Authorization: Bearer YOUR_API_TOKEN' \  -H 'Content-Type: application/json' \  -d '{  "code": "import math\nmath.sqrt(2)\n"}'
const response = await fetch('https://beta-api.b3os.org/v1/sandboxes/sbx_01m5t68q1b590rj801nbzsqzkk/run_code', {  method: 'POST',  headers: {      "Authorization": "Bearer YOUR_API_TOKEN",      "Content-Type": "application/json"  },  body: JSON.stringify({    "code": "import math\nmath.sqrt(2)\n"  })});const data = await response.json();console.log(data);
import requestsheaders = {    'Authorization': 'Bearer YOUR_API_TOKEN'}response = requests.post('https://beta-api.b3os.org/v1/sandboxes/sbx_01m5t68q1b590rj801nbzsqzkk/run_code', headers=headers, json={  "code": "import math\nmath.sqrt(2)\n"})print(response.json())
200 Response
{  "object": "run_code_result",  "language": "python",  "execution_count": 3,  "results": [    {      "mime_type": "text/plain",      "data": "1.4142135623730951",      "is_main_result": true    }  ],  "stdout": "",  "stderr": "",  "error": null,  "duration_ms": 4.8,  "timing": {    "kernel_ms": 4.1,    "total_ms": 5.2  }}
Ask a question... ⌘I