terminal or previews in requirements when you create the sandbox. BoxRouter then uses only a route that provides the capability, and contract.gates records what you got.
Before You Begin: BOXROUTER_API_KEY set and a client installed, as in the quickstart.
Open a Terminal
A terminal is a real PTY running a Bash login shell. The image needspython3, which the shell’s helper uses. The shell ends when it exits, when you kill it, when timeout_seconds passes (1 to 3,600; default 600) or when the sandbox ends.
terminal.output() yields raw bytes until the shell exits. The SDK examples send a command that exits so the stream finishes. The sandbox scope then closes its terminal handles and destroys the sandbox.
logout line. The shell confirms its exit with status 3.
output() returns exact TTY bytes, including shell prompts, echoed input, and ANSI escape sequences.
Use commands when you need program stdout without terminal formatting.
Decode the bytes as text only when needed. Input also goes in as bytes, up to 64 KiB per write, with no newline added; await each write before sending the next. A confirmed exit event sets terminal.exit_code / terminal.exitCode; until then the value is None / null, which means unknown.
Ctrl-] ends the CLI stream and leaves the remote shell running. The CLI example’s trap then destroys its sandbox. Remove that trap to keep the sandbox and shell running.
Reconnect
If an output stream ends unexpectedly, callterminal.output() again on the same handle to resume after its last consumed event. Reconnecting never repeats writes. A replay gap raises an error; the SDK does not silently skip missing output.
Closing a terminal handle ends its local stream; the remote shell and sandbox keep running.
Advanced: Resume from a Saved Cursor
Persistterminal.id and terminal.last_event_id before disconnecting. In a new process, open the same terminal
and pass the saved cursor to output(). This resumes output after the last event you consumed; never resend input.
Raw Events and Replay
terminal.events() exposes typed output, exit and error events for clients that need event metadata. Raw REST output
carries Base64 bytes. BoxRouter retains a 1 MiB sliding window for 60 seconds after the shell exits; a cursor outside
that window returns 410.
See the terminal event reference, terminal input reference
and terminal open reference for the raw operations.
Exit Status and Restarts
Until BoxRouter confirms the exit,exit_code is null. Treat null as unknown, not success. A killed terminal reports -1; a hang-up reports 129.
Open a Preview
A preview is an HTTPS URL for one port in the sandbox. Name the ports and the kind of link you accept when you create the sandbox; opening any other port fails withnot_named. If no route supports public previews, creation fails with no_eligible_route.
Start a server and wait for its port before opening the preview. The SDK and CLI helpers return when the port accepts connections inside the sandbox.
Each example requests sandbox deletion on exit; check the sandbox list if cleanup fails before retrying.
409 port_not_listening, with the output so far in details.
Open the returned URL while the script waits; it lists the server’s working directory. Press Enter to destroy the sandbox
and close the preview. A public preview is reachable by anyone with its URL. The CLI prints only the URL; HTTP and
the SDKs return an object like this:
Choose the preview access you need:
access: "any"accepts what the route offers. The result reports which access it provides.publicorprivaterequires that access. BoxRouter never falls back from private to public.
credential: "link", for a clickable
link) or headers, which you send with every request.
The low-level API returns a URL even when nothing listens on the port. The SDK and CLI wait helpers above check that the server accepts connections first.
expires_at is set only when the provider enforces an expiry. You cannot shorten the link’s lifetime.
Opening a port that already has a preview with the same access returns that preview.
Pausing and resuming a sandbox forgets its previews, because no route can read one back. Open the port again instead of caching the URL.
A preview stops answering when its sandbox is destroyed.
Reach a Sandbox over SSH
The hosted API does not offer SSH access. Creation refusesssh as a requirement. Use a terminal for an interactive shell and commands for scripted work. Provider support lists what the hosted API does not provide.
Next Steps
Run a Background Process
Start a server that a preview can reach.
Run Commands
Run scripted work without a shell.
Capabilities and Isolation
Check which routes offer terminals and previews.