curl -N -X POST https://api.podflare.ai/v1/sandboxes/$SID/exec \
-H 'content-type: application/json' \
-d '{"code": "print(6*7)"}'
with Sandbox() as s:
# Blocking: accumulate all events, return ExecResult
r = s.run_code("print(6 * 7)")
print(r.stdout, r.exit_code)
# Streaming: yield events as they arrive
for ev in s.run_code_stream("for i in range(3): print(i)"):
if ev.type == "stdout":
print(ev.data)
const r = await sbx.runCode("print(6 * 7)");
console.log(r.stdout, r.exitCode);
for await (const ev of sbx.runCodeStream("for i in range(3): print(i)")) {
if (ev.type === "stdout") console.log(ev.data);
}
{"type":"stdout","data":"42","seq":0,"ts":1776510028036}
{"type":"exit","data":0,"seq":1,"ts":1776510028037}
Reference
Execute code
Run code in the sandbox. Streams NDJSON events as they happen.
POST
/
v1
/
sandboxes
/
{id}
/
exec
curl -N -X POST https://api.podflare.ai/v1/sandboxes/$SID/exec \
-H 'content-type: application/json' \
-d '{"code": "print(6*7)"}'
with Sandbox() as s:
# Blocking: accumulate all events, return ExecResult
r = s.run_code("print(6 * 7)")
print(r.stdout, r.exit_code)
# Streaming: yield events as they arrive
for ev in s.run_code_stream("for i in range(3): print(i)"):
if ev.type == "stdout":
print(ev.data)
const r = await sbx.runCode("print(6 * 7)");
console.log(r.stdout, r.exitCode);
for await (const ev of sbx.runCodeStream("for i in range(3): print(i)")) {
if (ev.type === "stdout") console.log(ev.data);
}
{"type":"stdout","data":"42","seq":0,"ts":1776510028036}
{"type":"exit","data":0,"seq":1,"ts":1776510028037}
string
required
Sandbox id from
POST /v1/sandboxes.string
required
Source code to execute.
string
default:"python"
python or bash. Python uses the sandbox’s persistent REPL
(state carries across calls); bash runs in a fresh subprocess.curl -N -X POST https://api.podflare.ai/v1/sandboxes/$SID/exec \
-H 'content-type: application/json' \
-d '{"code": "print(6*7)"}'
with Sandbox() as s:
# Blocking: accumulate all events, return ExecResult
r = s.run_code("print(6 * 7)")
print(r.stdout, r.exit_code)
# Streaming: yield events as they arrive
for ev in s.run_code_stream("for i in range(3): print(i)"):
if ev.type == "stdout":
print(ev.data)
const r = await sbx.runCode("print(6 * 7)");
console.log(r.stdout, r.exitCode);
for await (const ev of sbx.runCodeStream("for i in range(3): print(i)")) {
if (ev.type === "stdout") console.log(ev.data);
}
{"type":"stdout","data":"42","seq":0,"ts":1776510028036}
{"type":"exit","data":0,"seq":1,"ts":1776510028037}
Semantics
- REPL state persists across Python
execcalls. Variables, imports, and open file handles stay alive. - Bash does not persist. Each bash
execis a fresh subprocess. - Serialized per sandbox. Only one
execruns at a time. Concurrent requests queue; if you want parallelism,fork(). - Exceptions don’t poison the REPL. An uncaught Python exception
produces
exit.data != 0but the next call still works.
Timing
| Call | Wall clock |
|---|---|
run_code("print(6*7)") in a warm pool-hit sandbox | ~3–5 ms |
First import pandas (cold file cache) | ~500 ms |
Subsequent import pandas (warm page cache) | ~150 ms |

