# How do I run code in a sandbox?

A sandbox is a small Linux computer of your own that starts in a few seconds. Whatever runs in it cannot touch your computer. Use it to run code an AI wrote, try a package or check a script.

## Steps

1. **Create a sandbox.** You get a fresh Linux machine with Python, Node.js and the usual tools.
2. **Run code or a command.** Python code directly, anything else as a shell command (`pip install`, `git clone`, `npm test`).
3. **Read the result.** Every run gives you its output (`stdout`), its errors (`stderr`) and an exit code – `0` means success.
4. **Delete the sandbox** when you are done – or keep it, see [Keep a workspace](https://sandbox.wbx.cz/docs/howto/workspace.md).

**Ask your assistant**

No code: copy this into Claude or ChatGPT with [WBX Sandbox connected](https://sandbox.wbx.cz/docs/connect.md) and change it to fit.

```
Create a WBX sandbox and run this Python code in it. Show me the output.

import platform, sys
print(platform.platform(), sys.version)

Then install the package cowsay and use it to print "Hello from WBX".
```

**Python**

`pip install wbxsandbox`, your key in `WBXSANDBOX_API_KEY`.

```
from wbxsandbox import Sandbox

with Sandbox.create() as sb:                      # a fresh Linux sandbox, removed at the end
    r = sb.run_code("import sys, platform\nprint(platform.platform(), sys.version)")
    print(r.stdout)
    r = sb.run("pip install -q cowsay && python -m cowsay -t 'Hello from WBX'")
    print(r.exit_code, r.stdout)
```

**TypeScript**

`npm install wbxsandbox` (Node.js 20+); save as `.mts` for top-level `await`.

```
import { WBX } from "wbxsandbox";

const wbx = new WBX();
const sandbox = await wbx.sandboxes.create();     // a fresh Linux sandbox
try {
  const r = await sandbox.runCode("import sys, platform\nprint(platform.platform(), sys.version)");
  console.log(r.stdout);
  const c = await sandbox.commands.run("pip install -q cowsay && python -m cowsay -t 'Hello from WBX'");
  console.log(c.exitCode, c.stdout);
} finally {
  await sandbox.kill();
}
```

**curl**

Just `curl` and `jq`; your key in `WBXSANDBOX_API_KEY`.

```
H="Authorization: Bearer $WBXSANDBOX_API_KEY"
SB=$(curl -s -X POST https://sandbox.wbx.cz/v1/sandboxes -H "$H" -d '{}' | jq -r .id)
curl -s -X POST https://sandbox.wbx.cz/v1/sandboxes/$SB/exec -H "$H" -d '{"cmd": "python3 -c \"import platform; print(platform.platform())\""}'
curl -s -X DELETE https://sandbox.wbx.cz/v1/sandboxes/$SB -H "$H"
```

## How to tell it worked

- The output starts with `Linux-…` and a Python version.
- The exit code is `0` and you see the cow saying *Hello from WBX*.
- In the portal, *Sandboxes* shows the sandbox while it runs.

## If it doesn't work

| What you see | What to do |
| --- | --- |
| Exit code is not `0` | The command failed – the reason is in `stderr`. An assistant shows it to you; in code print `r.stderr`. |
| `401 unauthorized` | The API key is missing or wrong. Check `WBXSANDBOX_API_KEY` or create a new key in *API keys*. |
| `429` with a limit on concurrent sandboxes | Your plan runs a set number of sandboxes at once (2 on Free). Delete sandboxes you no longer need, or let them sleep. |
| `pip install` takes long | Installing is the slow part. Do it once and start copies: [Prepare once, copy many](https://sandbox.wbx.cz/docs/howto/clone.md). |

## What it costs

A sandbox is billed while it runs, by its vCPU and memory. The default template `base` (2 vCPU, 512 MB) costs about US$0.07 (€0.06, 1.6 Kč) an hour. A sleeping sandbox uses no compute; only its disk is billed as storage. Current prices: [Pricing](https://sandbox.wbx.cz/pricing).

## Next steps

- [Upload and download files](https://sandbox.wbx.cz/docs/howto/files.md): Give the sandbox your data and take the result back.
- [Commands and processes](https://sandbox.wbx.cz/docs/sandboxes/commands.md): Timeouts, streaming output, background processes.
- [Your own agent](https://sandbox.wbx.cz/docs/guides/own-agent.md): Let a language model run the code it writes.

Source: https://sandbox.wbx.cz/docs/howto/run-code
