SPAWNBOX

API reference

Every HTTP endpoint for sandboxes, commands, files, templates, services, secrets, volumes, tenants, events and nodes.

Each /v1 request must have the header Authorization: Bearer <key>. Spawnbox keeps only the SHA-256 hash of each key. An error response has this format: {"error": {"code", "message", "request_id", "retryable"}}.

Errors

Code HTTP status Retryable Meaning
invalid_request 400 No A field is missing or wrong.
unauthorized 401 No The key is missing, wrong or revoked.
forbidden 403 No The key does not have the scope for this call.
not_found 404 No The sandbox, template or other resource does not exist.
conflict 409 No The resource is not in a state that allows this call, or the name is already used.
protected_env 409 No A snapshot would show env values of the sandbox.
build_failed 409 No A build that the sandbox needs could not be made. The same error is given for 5 minutes.
quota_exceeded 429 Yes A tenant quota is full. Try again after other sandboxes end.
no_capacity 503 Yes No host has room. The response has Retry-After: 30.
node_unreachable 503 Yes The host of the sandbox cannot be reached.
volume_unavailable 503 Yes The only copy of a volume is on a host that cannot be reached.
rebuilding 503 Yes A build that the sandbox needs is being made.
sandbox_cold 503 Yes A worker stopped the sandbox, and the primary starts it again.
stale_route 409 Yes Forward listener only: the sandbox moved or started again on another host.
sandbox_paused 503 No Forward listener only: the sandbox is paused and X-Spawnbox-Wake: never was set.
wake_busy 503 Yes Forward listener only: the host wakes too many sandboxes now. The response has Retry-After: 5.
too_many_requests 503 Yes Forward listener only: the sandbox has too many requests in flight. The response has Retry-After: 1.
not_ready 503 Yes Forward listener only: the service on the port is not healthy after 30 s.
upstream_timeout 504 No Forward listener only: the wake or the app took too long.
upstream_unreachable 502 No Forward listener only: nothing answers on the port in the sandbox.
draining 503 Yes Forward and preview requests: the sandbox is drained.
generation_conflict 409 No An endpoint switch named a generation that the endpoint is not at. The message has the current generation and target.
internal 500 Yes An unexpected error. The server log has the cause with the request_id.

retryable in the response has the same value. Repeat a retryable call after a short wait, with backoff.

Sandbox states

State Meaning Next states
starting The VM starts, for a create, a fork or a cold start. running; failed if the start fails; deleted if it is deleted meanwhile; cold for a persistent sandbox
running The VM runs. paused, deleted; cold (persistent) or failed/deleted (ephemeral) when the VM is lost
paused Memory and disk are saved in <data-dir>/paused/<id>, and the VM is stopped. running at a resume; deleted; cold (persistent) or failed (ephemeral) if the files cannot be used
cold Persistent only. No VM and no saved memory. The start record (template build, env, secrets, network rules, volumes, services, ports) is kept. starting at the next exec, file, terminal, clone, preview or resume call; deleted
failed The sandbox could not start or lost its VM. It ends. None
deleted The sandbox was deleted, expired or lost. It ends. None

failed and deleted are final. These events show the changes:

Change Event Reason
A create, fork or cold start ends sandbox.created, sandbox.forked, sandbox.cold_started
Pause and resume sandbox.paused, sandbox.resumed memory_pressure for a pause to free memory
An ephemeral sandbox loses its VM, or a server restart finds it running sandbox.lost The state is failed or deleted.
The timeout ends sandbox.expired
A persistent sandbox becomes cold sandbox.cold server_restarted, shutdown (the server stopped before it could pause), vm_exited, pages_lost, pause_failed, froze_while_saving, resume_failed, guest_too_old, paused_files_missing, capacity (no room for its idle pause), start_unfinished, gone_from_node, node_reported
The primary fences a lost host, or a standby takes over node.fenced, then sandbox.cold for each persistent sandbox of the host fenced with proof (operator, autoscaler, self_fence), or failover
A host stops its copy of a persistent sandbox sandbox.fenced self_fenced or lease_lost (the host fenced itself), stale_generation (a newer copy exists), resume_fenced
A cold start uses a new build sandbox.rebuilt missing, outdated, guest_too_old
A move to another host sandbox.migrated

Each cold start adds 1 to generation. A host stops a copy of a sandbox from an older generation. Refer to Fence lost hosts.

A template build is queued, building, ready or failed. A ready build becomes outdated when the kernel, base disk or runtime of the host changes, and removed when a newer build replaces it or its template is deleted. The files of outdated and removed builds are deleted.

Sandboxes

  • POST /v1/sandboxes creates a sandbox. Fields: template (default base), timeout_s, idle_pause_s, env, labels, egress, secrets, volumes, persistent, services. Org keys can also send tenant.
  • "persistent": true keeps the sandbox until it is deleted. Do not send timeout_s. idle_pause_s must be more than 0 (default 600). On a fleet, the primary runs it, or a worker with --persistent-placement any. Refer to Keep a sandbox until you delete it.
  • The create response has timings_ms and started_from. The value of started_from is warm_pool or build.
  • For a token or password that the sandbox sends in an HTTP header, use a secret with env, not env. The sandbox then sees only a placeholder, and the value never enters the VM. Refer to Secrets.
  • Responses, events and logs never show env values.
  • Each host adds up the memory_mib of its running sandboxes. A create, fork or resume that goes over --memory-overcommit-pct of the host memory (default 150) gives 503 no_capacity with Retry-After: 30. A primary first tries the other hosts. Paused sandboxes do not count.
  • When free host memory drops below --memory-pressure-pct (default 10) percent, or processes wait for memory that share of the time, the host pauses the sandbox with no running call that was used least recently, one at a time, and refuses creates and resumes with 503 no_capacity until memory is fine again. The sandbox.paused event has the reason memory_pressure. The next call resumes the sandbox as usual.
  • A Firecracker guest gives the pages it frees back to the host, so a sandbox uses only the memory it needs now. Templates built before this are rebuilt once.
  • GET /v1/sandboxes lists sandboxes. Org keys can add ?tenant=.
  • GET /v1/sandboxes/{id} gets one sandbox. It has persistent, generation (1, plus 1 for each cold start), cold_starts and last_cold_start_at. It never shows env values. A persistent sandbox has no expires_at.
  • States: starting, running, paused, cold, deleted, failed. Only persistent sandboxes become cold.
  • DELETE /v1/sandboxes/{id} deletes a sandbox. A cold sandbox, or one that is starting again, is deleted at once. A start that ends after the delete stops its VM.
  • Spawnbox deletes a sandbox when its timeout_s ends. The maximum is --max-timeout-s. Time in the paused state is not counted.
  • POST /v1/sandboxes/{id}:pause saves the memory and disk in <data-dir>/paused/<id> and stops the VM.
  • POST /v1/sandboxes/{id}:resume starts the VM again. For a cold sandbox, it boots a new VM from the template build.
  • A cold start is given 120 seconds. Calls that come during a start wait for it. If it fails or takes longer, the sandbox stays cold and the next call tries again.
  • If the sandbox is already in the requested state, pause and resume do nothing.
  • A paused sandbox shows "state": "paused" and paused_at.
  • Paused sandboxes stay after a server restart. Spawnbox deletes them after --paused-retention-s (default 7 days). Persistent sandboxes are not deleted.
  • Spawnbox encrypts the env of paused and persistent sandboxes on disk with --secrets-key-file. The env of the sandbox and of all its services is one ChaCha20-Poly1305 value, bound to the tenant and the sandbox ID. A value cannot be moved to another variable, service or sandbox. At start, Spawnbox encrypts env that an earlier version kept in another form. On SQLite, the first start after the upgrade also compacts the database file, to clear the plain copies.
  • If secrets.key cannot read the stored env of a sandbox, the server does not start. Usually the key is not the one that encrypted it; put back the right key. With --allow-unreadable-env the server starts, and leaves those sandboxes and their files as they are, so a later start with the right key resumes them. This helps only when the key reads everything else: secrets.key also encrypts the egress CA key, the stored secrets and, with Postgres, the files that primaries share. With a different key, the server still stops at those. Use the flag when the key is right but the env of some sandboxes cannot be read, for example rows that were damaged or copied from another installation.
  • Memory files and disks are not encrypted. Put <data-dir> on an encrypted disk; spawnbox doctor warns when it is not.
  • On Firecracker, a pause writes only the memory that changed since the sandbox was started or resumed. Spawnbox then merges it with the earlier memory in the background; a resume or a move waits for that merge. The first pause after a fork, a failed pause or a lazy (UFFD) restore writes all the memory. On the test server a full pause took approximately 0.7 s, and up to 12 s while earlier pauses were still being written.
  • POST /v1/sandboxes/{id}:fork starts copies of a running sandbox on its host. Field: count, 1 to 16 (default 1).
    • The response is 201 with sandboxes (the copies that started) and failed (the number that did not start).
    • Each copy gets the memory, processes and files of the sandbox. It also gets its template, labels, env, secrets, network rules and the time that remains before its timeout.
    • Each copy gets its own ID, hostname and random numbers. Its env is encrypted again for its own ID.
    • Each copy counts against the quotas of the tenant.
    • On the test server, 10 copies take approximately 0.7 s.
  • POST /v1/sandboxes/{id}:snapshot saves a running sandbox as a new template. Field: name.
    • The response is 201 with template, build and spec.
    • The template is in the tenant of the sandbox. Its env is the env of its template. Secrets are not saved. Add them again when you create a sandbox from the template.
    • A sandbox created with env, or with a service that has env, gives 409 protected_env, because the template would show the values. Use secrets for these values, or put them in the template spec.
    • The template has a snapshot field. You cannot rebuild the template.
    • Without a build store, sandboxes from the template start only on the host of the original sandbox. With a build store, a host with the same fingerprint and CPU model can get the files from the store.
    • On the test server, a snapshot takes approximately 0.4 s.
  • Fork and snapshot stop the sandbox for the time of the save. They do not operate on a paused sandbox or on a sandbox with volumes.

Commands

  • POST /v1/sandboxes/{id}/exec runs a command. Fields: cmd, cwd, env, user, timeout_s, stdin (maximum 1 MiB).
  • The JSON response has these fields:
    • stdout and stderr, maximum 1 MiB each. The stdout_truncated and stderr_truncated flags show if output was cut.
    • exit_code, signal and timed_out.
    • duration_ms and exec_id.
  • With the header Accept: text/event-stream, the response is a stream of stdout, stderr and exit events. If you close the stream, Spawnbox stops the command.
  • The request ends when the command ends. A background process that the command started can continue.
  • Environment variables come from the template, then the sandbox, then the command. A later value replaces an earlier value.

Files

  • GET /v1/sandboxes/{id}/files?path= gets the contents of a file, or a JSON list for a directory.
  • PUT writes the request body to the file. Spawnbox creates the parent directories. It replaces the file only after it receives the full body.
  • DELETE deletes a file. To delete a directory, add recursive=true.
  • A relative path starts at /workspace.
  • Spawnbox does file operations as user, unless you set user=. Usual file permissions apply.

Terminals

  • GET /v1/sandboxes/{id}/pty?cols=&rows=&user=&cwd=&cmd= opens a WebSocket. It runs bash -l, or cmd, on a real terminal.
  • Binary frames carry terminal data in the two directions.
  • To change the terminal size, send the text frame {"type":"resize","cols","rows"}.
  • At the end, the server sends {"type":"exit","exit_code","signal","duration_ms"}.
  • If you close the WebSocket, Spawnbox stops the shell.
  • A browser can use a session token with the terminal audience instead of an API key: add session_token=<token> to the query.

Browser session tokens

  • POST /v1/sandboxes/{id}/session-tokens makes a token for one sandbox. Fields:
    • audience (required): terminal or preview.
    • port: the published port that a preview token opens. It is required for preview, and not allowed for terminal.
    • ttl_s: 1 to 900, default 900.
  • The response has token and expires_at. The key must have sandboxes:write. A missing or bad audience or port gives 400 invalid_request.
  • The token is a JWT (HS256) with an aud claim. A terminal token opens only the terminal of that sandbox. A preview token opens only the private preview URL of that port. A token does not work on other API calls.
  • The token stops at expires_at, or when the key that made it is revoked.
  • Each new token records a sandbox.session_token_created event, with its audience.
  • Spawnbox signs tokens with <data-dir>/session.key. The primary sends this key to the workers. If you delete the file, all tokens stop.

Git

  • POST /v1/sandboxes/{id}/git:clone clones a repository. Fields: url (https:// or http://), path (default /workspace/<repo>), branch, depth, user, timeout_s.
  • The response has path, commit, branch and duration_ms.
  • A git_https secret on the sandbox gives access to private repositories. The token does not go into the sandbox.

Templates

  • POST /v1/templates creates a template and starts a build. Fields:
    • name.
    • setup: commands that run as root, with network access.
    • warmup: commands that run as user immediately before the snapshot. Background processes that they start continue in each sandbox.
    • env.
    • resources: vcpus, memory_mib, disk_gib.
    • min_warm: the minimum number of warm VMs that each host keeps for this template, 0 to 16. The default is 0. Refer to Warm pools.
    • base: a container image to start from, for example node:22-bookworm-slim. If you do not set it, the template starts from the Debian base. Refer to Start a template from a container image.
    • registry_secrets: names of held secrets that Spawnbox uses only to get base. A maximum of 4.
  • The response is 202 with the template and its build.
  • GET /v1/templates lists templates. GET and DELETE /v1/templates/{name} get and delete one template.
  • GET /v1/templates/{name}/builds/{build_id} gets the state, the error and the end of the build log.
  • You cannot change or delete the base template.
  • If you save a template again, Spawnbox starts a new build. Until the new build is ready, sandboxes use the last ready build. If you change only min_warm, Spawnbox does not start a new build.
  • If a template has no ready build, a create request gives 409.
  • Builds stay after a restart. Spawnbox builds again only when the kernel, base disk, Firecracker version or --disk-io-engine changes.
  • Spawnbox runs a maximum of --max-builds builds at the same time.

Services

  • services in POST /v1/sandboxes is a list of up to 8 services. Each has these fields:
    • name: 1 to 63 of a-z, 0-9 and -. It cannot start with -. Each name must be different.
    • command: the program and its arguments. It cannot be empty.
    • cwd (optional): an absolute path.
    • env (optional): it cannot set the CA variables, such as SSL_CERT_FILE, or the variable of a secret of the sandbox.
    • port (optional): 1 to 65535. Each port must be different.
    • access (optional, needs port): private (default), token or public.
    • health (optional, needs port): {"http": "/path"} or {"tcp": true}, with interval_s (1 to 3600, default 10) and failures (1 to 100, default 3).
    • restart (optional): always (default) or on-failure.
  • A wrong value gives 400 invalid_argument. A template without support for services gives 409 conflict.
  • GET /v1/sandboxes/{id}/services gives the state of each service: {"services": [{"name", "state", "pid", "restarts", "last_exit", "started_at"}]}.
    • state is starting (until the first good health check), healthy, unhealthy, backoff (it waits to start again) or stopped.
    • last_exit is {"code": 1}, {"signal": 9} or null.
  • POST /v1/sandboxes/{id}/services/{name}:restart stops a service and starts it again. The response is 204. An unknown name gives 404.
  • GET /v1/sandboxes/{id}/services/{name}/logs gives the last 1 MiB of output as text. With ?follow=true, the response is a stream of output events ({"text": "..."}) that continues with new output.
  • A stop sends SIGTERM to the process group of the service, and SIGKILL after 5 s.
  • These calls resume a paused or cold sandbox, and count as activity. The work of a service in the VM does not keep the sandbox awake.
  • Spawnbox never shows the env values of a service. It encrypts the services of paused and persistent sandboxes on disk, in one value with the env of the sandbox.
  • A fork copies the services and their processes. A snapshot template does not keep the list of services.

Preview URLs

  • POST /v1/sandboxes/{id}/ports publishes a port and gives its URL. Fields: port, access, ttl_s.
  • GET lists the published ports.
  • DELETE /v1/sandboxes/{id}/ports/{port} removes a published port.
  • access has four values:
    • token (default): the URL contains a token. The token stops after ttl_s (default 24 hours). At the first visit, it becomes a cookie.
    • public: all persons with the URL can open it.
    • private: the request must have the API key in Authorization, or a preview session token for the port. Add the token to the URL as ?spawnbox_session=<token>. At the first visit, it becomes a cookie. Spawnbox removes this cookie before the request goes to the app.
    • gateway: the port has no preview URL. Only the forward listener can reach it.
  • For each access, Spawnbox removes these from a request before it goes to the app: the spawnbox_session and spawnbox_preview cookies, the spawnbox_session query parameter, and an Authorization header with a Spawnbox API key (Bearer sbx_...) or a session token that this install made. On public and token ports, other Authorization values go to the app, so an app can use its own logins. On private ports, Spawnbox removes all Authorization values.
  • Spawnbox serves previews on --preview-listen at --preview-url. It finds the sandbox from the host name.
  • HTTP/1.1 and WebSockets are supported.
  • A request to the port of a service waits up to 30 s until the service is healthy. Then it gives 503.

Forward requests from a gateway

Use the forward listener when your own HTTP gateway or load balancer sends traffic to sandbox ports. The gateway owns the host names, TLS for users and its own logins. Spawnbox wakes the sandbox and sends the request to the port.

  • Start it with --forward-listen ADDR. Add --forward-tls-cert and --forward-tls-key for TLS. Put the listener on a private network. Only your gateway must reach it.

  • Make a key with only the sandboxes:forward scope for the gateway. This key cannot call the other APIs. A tenant key reaches only the sandboxes of its tenant. A revoked key stops working within 5 s.

  • Publish the port first, with POST /v1/sandboxes/{id}/ports. Use access: gateway if the port must not have a preview URL. The listener forwards to a published port of each access.

  • The gateway sends the original request, with these headers:

    Header Required Meaning
    X-Spawnbox-Key Yes The API key
    X-Spawnbox-Target Yes sbx_ID:PORT, or endpoint:NAME for an endpoint of the key’s tenant (default for an org key)
    X-Spawnbox-Wake No any (default) resumes a paused sandbox and starts a cold one. paused only resumes. never wakes nothing.
    X-Spawnbox-Timeout-Ms No How long to wait for the response headers of the app. Default 60000, at most --forward-max-response-s.
  • Spawnbox removes all X-Spawnbox-* headers before the request goes to the app, and from the response of the app. Other headers, the path, the query and the body go to the app without change.

  • The response has X-Spawnbox-Sandbox and X-Spawnbox-Generation. The generation goes up each time the sandbox starts on a host. A request to an endpoint also has X-Spawnbox-Endpoint-Generation.

  • If Spawnbox refuses the request, the body is a normal error and the response has X-Spawnbox-Error with the code. A response without this header comes from the app.

  • Limits:

    • --forward-max-wakes (default 8): wakes that run at once on a host. A request waits up to --forward-wake-queue-s (default 10) for a slot, then gets 503 wake_busy. Requests for a sandbox that another request already wakes wait for that wake and take no slot.
    • --forward-max-in-flight (default 1024): requests and WebSockets in flight for one sandbox. More get 503 too_many_requests.
    • A wake can take up to 60 s. Then the request gets 504 upstream_timeout.
    • A request to the port of a service waits up to 30 s until the service is healthy. Then it gives 503 not_ready. After a wake, a request to a port without a service waits up to 30 s until the port takes a connection.
  • HTTP/1.1 and WebSockets are supported.

  • In a fleet, the forward listener runs on the primary and on edges (with the same flags). It sends each request to the host of the sandbox. If the sandbox moved, the request is sent again once, when its body is at most 1 MiB. Edges send requests to an endpoint to the primary.

Port readiness

GET /v1/sandboxes/{id}/ports/{port}/ready tells whether a port takes traffic:

{ "ready": false, "generation": 3, "state": "running", "reason": "service \"web\" on port 8000 is not healthy" }
  • The port of a service is ready when the service is healthy. Another published port is ready when it takes a TCP connection in the sandbox.
  • Without wait_s, the call does not wake the sandbox. A sandbox that is not running, a port that is not published and a draining sandbox are not ready.
  • With ?wait_s=N (at most 120), the call wakes the sandbox and waits up to N seconds for the port to be ready.
  • The key needs sandboxes:read.

Drain a sandbox

A drain takes a sandbox out of traffic without cutting the requests that it serves.

  • POST /v1/sandboxes/{id}:drain with timeout_s (1 to 3600, default 30) and then (none by default, pause or delete). The response is 202 with the sandbox.
  • New forwarded and preview requests get 503 draining at once. A draining sandbox that is paused does not wake for them.
  • When no forwarded or preview request or WebSocket is left, or at the timeout, Spawnbox does then. A pause or delete ends the requests that are left.
  • Commands, files, terminals and other API calls continue to operate.
  • The sandbox stays out of traffic until POST /v1/sandboxes/{id}:undrain or an endpoint switches to it, also after then. An undrain before the timeout stops the drain.
  • GET /v1/sandboxes/{id} shows drain with started_at, deadline, in_flight and then while the sandbox is drained.
  • Events: sandbox.drain_started, and sandbox.drained with in_flight, closed (requests that the pause or delete ended) and then.
  • The drain is kept in the memory of the host that runs the sandbox. If that host restarts, the drain ends.

Endpoints

An endpoint is a stable name for one sandbox port. To replace a sandbox, switch the endpoint to the new one. Gateways send requests to X-Spawnbox-Target: endpoint:NAME.

  • POST /v1/endpoints with name, sandbox and port. The endpoint is in the tenant of the sandbox. A name is 1 to 63 of a-z, 0-9 and -, unique in its tenant. A new endpoint has generation 1.

  • GET /v1/endpoints and GET /v1/endpoints/{name} show the target, the generation and the target_state. Org keys add ?tenant= for an endpoint that is not in default.

  • DELETE /v1/endpoints/{name} removes the endpoint, not its target.

  • PUT /v1/endpoints/{name} switches it:

    {
      "sandbox": "sbx_NEW",
      "port": 8000,
      "if_generation": 4,
      "ready_wait_s": 60,
      "previous": { "drain_s": 30, "then": "pause" }
    }
    • if_generation is required. The switch happens only if the endpoint is at this generation. Else the response is 409 generation_conflict. Of two switches from the same generation, one wins.
    • The switch ends a drain of the new target, so you can switch back to a target that an earlier switch drained.
    • ready_wait_s (0 to 300, default 60): Spawnbox wakes the new target and waits until its port is ready. If it is not ready in time, or its port is not published, the response is 503 not_ready and the endpoint does not change. 0 skips the check. The key needs sandboxes:read for this.
    • previous: after the switch, the old target drains for drain_s (default 30), then does then (default pause). If previous is not given, these defaults apply. null leaves the old target alone.
    • The drain starts --endpoint-propagation-s (default 2) after the switch, so requests that read the endpoint just before the switch still reach the old target. If an endpoint of the tenant targets the old sandbox again by then, it does not drain. A request to an endpoint that gets draining reads the endpoint again and is sent once more, when its body is at most 1 MiB.
    • The response has the endpoint with its new generation, and previous with the old target.
  • Each response through an endpoint has X-Spawnbox-Endpoint-Generation, so you can see which generation served it.

  • Events: endpoint.created, endpoint.switched, endpoint.deleted.

  • Endpoints are kept in the database of the primary and are in backups.

Network access (egress)

  • Each TCP connection from a sandbox goes through the egress proxy. Spawnbox records each connection.

  • Spawnbox always blocks these addresses:

    • private and link-local addresses, which include cloud metadata
    • loopback addresses
    • the addresses of the host
    • port 25 (SMTP)
  • A policy has this format: {"mode": "allow_all" | "allowlist" | "deny_all", "allow": [...], "deny": [...]}. A pattern is a host name or *.example.com.

    • allowlist checks the TLS SNI or the HTTP Host name. It allows only ports 80 and 443.
    • deny is checked by name too: the TLS SNI, the HTTP Host name or the DNS query. Thus, when any policy has a deny list, allow_all refuses a TCP connection without a name on any port, for example to a bare address or TLS without SNI. It also drops UDP (except DNS) and ICMP. Without a deny list, allow_all allows these. An address written as a name, such as Host: 93.184.215.14 or 0x5db8d70e, counts as no name.
    • deny blocks names, not addresses. Spawnbox does not compare the address that an allowed name resolves to with the addresses of denied names. A sandbox that can use a name whose DNS it controls, such as a wildcard DNS service that answers 1-2-3-4.example with 1.2.3.4, can reach the address of a denied host. Thus, a deny list stops mistakes and casual use, not a determined program. Use allowlist when that matters.
    • A name check sees only the name that the client sends in clear text. A server that hosts many names, such as a CDN, can serve a denied name behind an allowed one (domain fronting or TLS Encrypted Client Hello). Use allowlist when that matters.
    • deny_all also blocks DNS.
  • Each DNS query from a sandbox goes to the DNS forwarder of Spawnbox, for all DNS server addresses. The forwarder sends the query to --dns and records the name, the type, the result and the addresses.

    • When the policy does not allow a name, the answer is REFUSED.
  • Optional speed limits, in megabits per second, from 1 to 100000:

    • max_egress_mbps: the data that the sandbox sends.
    • max_ingress_mbps: the data that the sandbox receives.

    For example: {"mode": "allow_all", "max_egress_mbps": 20, "max_ingress_mbps": 100}. The limits also apply after a resume.

  • Spawnbox applies three policies: --egress-policy of the server, the egress of the tenant and the egress of the sandbox. The strictest policy wins. For speed limits, the lowest limit wins.

Secrets

  • POST /v1/secrets saves a secret. Fields: name, type (http_header or git_https), value, allowed_hosts, env, header, format, username.

  • GET, PATCH and DELETE /v1/secrets/{name} get, change and delete a secret. They never show the value.

  • Spawnbox encrypts each value with ChaCha20-Poly1305. The key is in <data-dir>/secrets.key, or in --secrets-key-file.

  • Use a secret for each value that a sandbox sends in an HTTP header. With env, the sandbox gets a placeholder in that variable, and the proxy puts the value in the request. Code in the sandbox cannot read or leak the value.

    CAUTION: Make a backup copy of secrets.key. Without this file, you cannot read your secrets.

  • For HTTPS requests to allowed_hosts, the proxy puts the value in the header of the secret. The default header is Authorization: Bearer. git_https uses Basic authentication.

  • After a PATCH, the next request uses the new value.

  • A PATCH that adds to allowed_hosts or changes header or format must also send value. Without it, the response is 400 value_required, so a key that can change a secret cannot send its stored value to a new host. A PATCH that only removes hosts does not need value.

  • The secret.updated and secret.rotated events show allowed_hosts. When the hosts change, they also show previous_allowed_hosts. They never show the value.

  • Sandboxes trust the CA of the proxy. SSL_CERT_FILE, NODE_EXTRA_CA_CERTS, REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE point to the system certificate bundle.

Volumes

  • POST /v1/volumes creates a volume. Fields: name, size_gib (1 to 1024, default 10), tenant. A name has 1 to 63 characters: a-z, 0-9 and -. It cannot start with -.
  • GET /v1/volumes lists volumes. GET /v1/volumes/{name} gets one volume. The response shows size_gib, backend, node and mounts: the sandboxes that use the volume.
  • DELETE /v1/volumes/{name} deletes a volume and its data. If a sandbox uses the volume, the response is 409. Delete the sandbox first.
  • To mount volumes, add volumes to POST /v1/sandboxes. Each item has volume, path and read_only (default false).
  • path is an absolute path, for example /data. It cannot be /, or in /proc, /sys, /dev, /run or /spawn. Two paths cannot be one in the other.
  • Spawnbox makes the disk of a volume when a sandbox mounts it the first time. The disk uses space on the host only for the data in it.
  • Usage shows the full size_gib of each volume as disk use.

Tenants and keys

  • POST /v1/tenants creates a tenant. Fields: id, name, quotas, egress, isolation.

  • isolation is any (default) or microvm-only. With microvm-only, the sandboxes of the tenant run only on Firecracker hosts. Refer to Firecracker or gVisor.

  • GET /v1/tenants lists tenants. GET, PATCH and DELETE /v1/tenants/{id} get, change and delete one tenant. GET also shows the current usage.

  • The default tenant always exists.

  • You cannot delete a tenant that has sandboxes, templates or secrets. The response is 409.

  • When you delete a tenant, Spawnbox cancels its keys.

  • There are two types of keys:

    • Org keys (sbx_org_...) can access all tenants. Older keys (sbk_...) are org keys.
    • Tenant keys (sbx_tn_...) can access only their tenant. For other tenants, the response is 404.
  • POST /v1/keys creates a key. Fields: tenant, scopes, name. Spawnbox shows the key only in this response.

  • GET /v1/keys lists keys. DELETE /v1/keys/{id} cancels a key.

  • Scopes: sandboxes:read, sandboxes:write, sandboxes:forward, templates:write, secrets:write, tenants:admin (only org keys), audit:read, usage:read. A key without a scope list gets all the scopes that its type allows.

  • A template or secret is for all tenants, or only for one tenant (?tenant=). If two have the same name, the tenant’s own one wins.

  • Quotas. The value null means no limit.

    Quota Default for a new tenant
    running_sandboxes 20
    running_vcpus 40
    running_memory_mib 80 GiB
    max_session_s 24 hours
    max_paused_retention_s 7 days
    monthly_egress_bytes No limit
    snapshot_storage_gib No limit
    volume_storage_gib No limit
    persistent_sandboxes 0

    The default tenant has no limits, except persistent_sandboxes. persistent_sandboxes counts persistent sandboxes in each state until they are deleted. If a request goes over a quota, the response is 429 quota_exceeded.

Events

  • You can send the header X-Spawnbox-Step: <id> with each request, for example the ID of a tool call of your agent. You can also send a W3C traceparent header.
  • The events from the request then have step_id and trace_id. Each event also has tenant, sandbox and actor.
  • Event types:
    • sandbox.created (with forked_from for a copy), .paused, .resumed, .forked, .snapshotted, .expired, .killed, .lost, .cold (with reason), .cold_started (with generation, build and duration_ms), .rebuilt (with from_build, to_build and reason), .fenced (with reason and generation: a host stopped its copy), .migrated, .session_token_created
    • node.joined, .up, .down, .draining, .drained, .undrained, .retiring, .retired, .removed, .fenced (with node and reason), primary only
    • exec.started, exec.finished (with the last 64 KiB of output)
    • pty.opened, pty.closed
    • service.started (with pid), service.exited (with exit), service.unhealthy (with failures), service.restarted (with restarts and the reason exit, unhealthy or request). Each has name.
    • file.written (with the SHA-256 hash), file.deleted
    • port.published, port.unpublished
    • net.connection, net.blocked
    • net.dns (with name, type, verdict, rcode, answers and duration_ms)
    • secret.injected (without the value)
    • volume.created, volume.deleted
    • sandbox.drain_started, sandbox.drained
    • endpoint.created, endpoint.switched, endpoint.deleted
    • changes to tenants, keys, secrets and templates, such as template.saved and template.deleted
    • autoscale.requested, .done, .failed (primary only)
  • GET /v1/events and GET /v1/sandboxes/{id}/events get events. These are the filters: tenant, sandbox, step_id, type, since, until. The type exec.* gets all the exec events.
  • Use limit (maximum 1000) and cursor to get more pages.
  • follow=true streams new events as server-sent events. After a disconnection, send Last-Event-ID to continue.
  • The key must have the scope audit:read.
  • --otlp-endpoint also sends the events to OpenTelemetry as OTLP/HTTP log records.
  • --otlp-endpoint also sends traces. Each create and each command is one trace. A create has the spans fetch_build, volumes, claim, restore and fixups. A command has the spans open and start. On a primary, the span sandbox.place contains one node span for each host that it tries.
  • If a request has a traceparent header, its trace contains the Spawnbox spans. The events of the request have the same trace_id.
  • Spawnbox keeps events for --events-retention-s (default 7 days).

Warm pools

A warm VM is a sandbox that Spawnbox restored before a request came. Its vCPUs are paused. A create takes a warm VM, starts its vCPUs and gives it a new clock, random seed and hostname.

  • Spawnbox keeps a pool for each template build and network mode. The network mode comes from the egress policies: allow_all, allowlist or deny_all.
  • The target size of a pool is the number of creates in the last 30 seconds. The base pool for the server’s egress policy has a minimum of --warm-pool-min. A template with min_warm has that minimum. A pool with no creates and no minimum falls to zero.
  • The min_warm pool of a template uses the egress policy of the template’s tenant (allow_all for org-wide templates). A sandbox with a narrower egress policy uses a different pool.
  • In a fleet, each host keeps min_warm warm VMs.
  • Warm VMs use a maximum of --warm-pool-memory-pct of the host memory. They do not use the last --max-builds free slots. If a create or resume has no free slot, Spawnbox stops a warm VM.
  • Warm VMs are not counted in quotas or usage. Spawnbox counts them when a create takes them.
  • GET /v1/warm-pools shows each pool: template, build, network, ready, filling, target, hits and misses. The key must have the scope tenants:admin.

Nodes

These calls operate only on a primary. The key must have the scope tenants:admin.

Call Function
POST /v1/nodes/tokens Make a join token. Optional body: {"ttl_s": 3600}, from 60 to 604800. The response has token, expires_at and ca_sha256.
GET /v1/nodes List the workers and edges that are not removed
GET /v1/nodes/autoscale Get the load, the workers in each state and the autoscaler requests. Refer to Add and remove workers automatically.
GET /v1/nodes/{id} Get one worker or edge
DELETE /v1/nodes/{id} Remove a worker or an edge. The sandboxes of a worker are marked deleted.
POST /v1/nodes/{id}:drain Put no new sandboxes on the worker. You cannot drain an edge. With the body {"migrate":true}, also move its sandboxes and volumes to other hosts: persistent sandboxes become cold and start again on other hosts. Gives 202 when it moves sandboxes, with drain_pass set to started, or to already_running when a pass already runs, in which case it starts nothing. Call it again to retry what did not move.
POST /v1/nodes/{id}:undrain Put new sandboxes on the worker again. Gives 409 if the worker retires.
POST /v1/nodes/{id}:retire Retire the worker before you stop its machine. Optional body: {"migrate":true,"reason":"..."}. Gives 202. Refer to Retire a worker safely.
POST /v1/nodes/{id}:fenced Tell the primary that the host of a down or removed worker is off. Its persistent sandboxes become cold. Optional body: {"reason":"..."}. The response has sandboxes_cold. Gives 409 if the worker is not down or removed. Refer to Fence lost hosts.
POST /v1/sandboxes/{id}:migrate Move one sandbox to a different host. Optional body: {"node":"nod_..."} or {"node":"primary"}.

Each node has id, kind (worker or edge), url, state (ready, suspect, down), draining, last_seen, sandboxes (sandboxes on it that are not cold), volumes (volumes whose home it is), version (its Spawnbox release, null before it reports one), config_current, retirement and capacity (free slots, free memory, reserved memory and its limit, whether memory is short, ready templates, warm VMs, CPU model and fingerprint). Sandboxes move only between hosts with the same fingerprint and CPU model. retirement is null, or has id, origin (api or autoscaler), state (draining, blocked, ready_to_terminate or removed), migrate, reason, created_at, ready_at, ended_at, failed_moves and waiting_for.

Each sandbox has the field node: the ID of the worker that runs it, or null for the primary.

Usage

  • GET /v1/usage?from=&to=&group_by=sandbox gives one row for each tenant and each UTC hour. With group_by=sandbox, it gives one row for each sandbox.
  • The default range is the current month. The maximum range is 93 days.
  • The key must have the scope usage:read.
  • Each row has these fields:
    • vcpu_s and mem_gib_s: the reserved size while the sandbox runs. A paused sandbox uses no vCPU or memory.
    • disk_gib_h: the bytes that are on the disk of the host.
    • egress_bytes: the TCP bytes sent and received.
  • Limits:
    • Spawnbox counts the bytes of a connection when the connection closes. Thus, an egress quota does not stop open connections.
    • In open mode, Spawnbox does not count UDP and ICMP.
    • Spawnbox does not measure the time when the server is stopped.

This page is the “API reference” section of README.md ↗. Found a mistake? Edit it on GitHub ↗