Sandbox class with a static async factory. Every method returns a Promise (or AsyncGenerator for streaming), so you can use it naturally with async/await and for await. The package ships TypeScript declarations for all public types — no @types/ package needed.
Installation
Install the package from npm. Node 20 or later is required.Configuration
Set the host
By default
Sandbox.create() reads PODFLARE_HOST from process.env and falls back to http://127.0.0.1:7070. Pass options to Sandbox.create() to override, or share a Client instance across many sandboxes.Set the API key
Pass your key as
apiKey in the options object or export PODFLARE_API_KEY so every Sandbox.create() call picks it up automatically.For local development without an API key, the SDK falls back to
http://127.0.0.1:7070 automatically. See Authentication for details.Core methods
Sandbox.create
Sandbox. Returns a fully initialized instance ready to accept calls. Always await it.
runCode
code inside the sandbox and resolves with an ExecResult once the process exits.
runCodeStream
Event objects as they arrive. Use this to display output token-by-token without waiting for the run to finish.
fork
n independent children, each inheriting the parent’s current memory and filesystem state. Children boot in parallel, so spawning 5 costs roughly the same wall-clock time as spawning 1.
diff
other. Defaults to comparing /root and /tmp; pass paths to restrict the scan.
mergeInto
winner’s filesystem and memory state as the new state of the parent. After this call, the parent’s ID remains valid and drives winner’s VM. Calling winner.close() becomes a no-op.
upload and download
close
close() in a finally block so the sandbox is cleaned up even when an error is thrown.
Full example — fork, evaluate, merge
The following example loads a dataset once, forks five children to run different queries in parallel, selects the winner, and merges its state back into the parent.Vercel AI SDK adapter
Use the"podflare/ai-sdk" subpath import to get a pre-built tool definition compatible with the Vercel AI SDK’s generateText and streamText APIs.
Types
All public types are exported from the top-level module. Import them withimport type to keep your bundle size unchanged.
Resolved by
runCode. Fields: stdout: string, stderr: string, exitCode: number.Yielded by
runCodeStream. Fields: type: "stdout" | "stderr" | "exit", data: string."python" | "bash" — accepted by runCode and runCodeStream.