Skip to main content
This page walks through the calls an app needs: create a machine, run a command in it, pause and resume it, and delete it, then open a shell and the desktop and read usage. Everything runs on your server with an API key (ibk_...).
Never send the API key to a browser.

Install and authenticate

The SDK reads INSTABOX_API_KEY and INSTABOX_API_URL, or takes { apiKey, apiUrl } as options for a key from your own secret store. Without a key, every call throws InstaboxAuthError (missing_api_key) before sending a request. Over HTTP, send the key as X-API-Key or Authorization: Bearer.
The examples use import and top-level await. Save one as a .mjs file and run it with Node.js 18 or later, or set "type": "module" in your package.json for a .js file.So that a failed step does not leave a machine running, wrap the steps after create in try and finally { await sbx.kill() }. Your app keeps the machine and deletes it when your user is done.

Create a machine

Sandbox.create waits until the machine runs, so the script prints the new machine’s ID, m_.... agent-browser is the default template: Linux, a desktop, and a browser. Later, from another request:
Over HTTP, lifecycle calls (create, pause, resume, delete) return an operation. Poll it until succeeded or failed before the next change to that machine. Lifecycle calls need an Idempotency-Key header and answer 400 without one. Reuse a key only to retry the same request.
POST /v1/machines takes {}, which creates an agent-browser machine named box- and 8 random hex digits. An unknown templateId is 400, listing the templates your plan can create from. A body field the API lacks is 400 unknown_field. For "template", the error asks: did you mean "templateId"?

Run a command and move files

The TypeScript prints the command’s output, 42, and its exit code, 0. Then it prints the file it wrote, print("hi"). To run a command without throwing on a non-zero exit, use sbx.commands.exec.
  • Send cmd (a shell string, run with bash -lc) or argv (a program and its arguments, run with no shell), not both.
  • It returns stdout, stderr, and exitCode when the command exits. After timeoutSeconds (60 by default, at most 600), the command is killed.
  • With Accept: text/event-stream (instabox exec --stream), it streams output as the command runs. To run a longer job detached, use instabox exec --background and instabox jobs.
  • It takes no Idempotency-Key, so a retry runs the command again.
  • When a disk of the machine is at least 90% full, or the command failed with No space left on device, the result carries a diskWarning. Its fields are mount, usedPct, level, noSpace, and message, and it names the full disk and the fix: instabox resize <box> --disk <GiB> grows /data, live on a running machine. instabox exec prints it on stderr.

Pause and resume the machine

The first log shows the machine’s state without waking it. The second prints true, because connect resumes the machine. Idle machines pause by themselves after minutes. To change that, call sbx.setAutoSuspend(idleMs). sbx.stop() shuts the machine down and keeps /data. See Lifecycle.

Delete the machine

The first log prints true. The second prints false, because the machine is already gone.

Open an SSH session

sbx.sshCredentials(publicKey) takes your public key and returns a short-lived certificate. Over HTTP, send your public key to POST /v1/machines/{id}/connections, with an Idempotency-Key. It returns the certificate and the host CA. See SSH.
The example prints the ssh command to run.

Drive the desktop

Any key with machines:write that can reach the machine, including a workspace key, opens its desktop over GET /v1/machines/{id}/vnc. That route is a WebSocket carrying the VNC (RFB) stream. A share link (sbx.desktop.streamUrl()) is for the machine’s creator only. See Desktop for links, embedding, and an agent loop.

Read usage and metrics per customer

Tag each machine with your customer when you create it, then read usage by that tag:
The script prints each machine’s latest CPU use, then the number of machines and their total running seconds. getUsage covers the current UTC month by default and counts running time only, not paused or stopped time. A Sandbox object has the same calls: sbx.getUsage() and sbx.getMetrics(). Sandbox.usage() returns account counts, storage, and limits.

Handle errors

Calls throw InstaboxError with status and code, such as not_found or quota_exceeded. A missing or bad key throws InstaboxAuthError. See Errors.

Use the CLI with a key

With INSTABOX_API_KEY set, the CLI needs no login:

Reference

SDK reference

Every call, by task: snapshots, labels, files and more.

API reference

Every endpoint an API key calls.

Python SDK

The same calls from Python: pip install instabox-sdk.