---
title: "Run a code snippet"
method: POST
path: "/sandboxes/{id}/code"
tags: ["Runtime"]
---

# Run a code snippet

`POST /sandboxes/{id}/code`

Writes `code` to a temp file and runs it with the chosen interpreter.
Cleaner than `/exec` for multi-line scripts because you don't have to
deal with shell escaping. Response shape matches `/exec`.

Supports the same `stream: true` opt-in as `/exec`; see that endpoint's
description for the SSE event shape.

## Path parameters

- `id` string, required

## Request body

- CodeInput
  - `language` 'python' | 'node', required
  - `code` string, required — Snippet source. Multi-line via `\n` in the JSON string.
  - `timeout_seconds` integer
  - `cwd` string
  - `env` object — Extra environment variables for this snippet only. Same semantics as `env` on `/exec`, per-call override on top of the sandbox's default environment.
  - `stream` boolean — When true, the response is SSE (`text/event-stream`) as the snippet runs, same behaviour as `/exec` with `stream: true`.

## Response `200`

Code completed.

When the request body has `stream: true`, the response is
`text/event-stream`, one `ExecStreamFrame` per `data:` event.

- ExecResultEnvelope
  - `message` string, required
  - `data` ExecResult, required
    - `stdout` string, required
    - `stderr` string, required
    - `exit_code` integer, required
    - `duration_ms` integer, required

## Other responses

- `400` — Validation error / invalid state transition
- `404` — Sandbox or related resource not found (also returned when owned by another user)

## Changes

- **2026-06-29** `4a016f074bc0` — 1 breaking, 1 info
  - removed the media type `application/x-ndjson` for the response with the status `200`
  - added the media type `text/event-stream` for the response with the status `200`
- **2026-05-23** `b236bf76f9a7` — 1 info
  - added the new optional request property `env`

[Change history](https://skmtc.dev/brimblehq/apis/brimble-sandbox-api/changes/sandboxes/:id/code/post.md)

---

[API](https://skmtc.dev/brimblehq/apis/brimble-sandbox-api.md) · [All operations](https://skmtc.dev/brimblehq/apis/brimble-sandbox-api/llms.txt) · [OpenAPI document](https://skmtc-service-production.skmtc.workers.dev/v1/apis/brimblehq/brimble-sandbox-api/revisions/7fe2df041597/schema)
