> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pxxl.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Sandboxes quickstart

> Create a sandbox, run a command, write a file, and open its preview URL.

You need a Plus, Pro, or Enterprise plan and at least \$0.25 in your [wallet](https://docs.pxxl.app/account/wallet.md).

## Create an API key

In [account settings](https://docs.pxxl.app/account/settings.md), create an API key with the scope **`sandbox`** (or `all`) and the permission **`read_write`**. Store it as `PXXL_API_KEY`.

```bash theme={null}
export PXXL_API_KEY=pxxl_...
```

A `read` key can list and inspect sandboxes but can't create, run commands, or change anything.

## Create a sandbox and run a command

> [tab] Node.js
>
> ```bash theme={null}
> npm install @pxxlapp/pxxl
> ```
>
> ```ts theme={null}
> import { Pxxl } from "@pxxlapp/pxxl";
>
> const pxxl = new Pxxl({ apiKey: process.env.PXXL_API_KEY });
>
> const sandbox = await pxxl.sandboxes.create({
>   template: "node",
>   size: "small",
>   autoStopMinutes: 30,
>   labels: { project: "quickstart" },
> });
> await sandbox.waitUntilReady();
>
> await sandbox.files.write(
>   "/workspace/server.js",
>   "require('http').createServer((_, res) => res.end('hello from a sandbox')).listen(process.env.PORT)",
> );
>
> const { exitCode } = await sandbox.exec("node --version", {
>   onOutput: (chunk) => process.stdout.write(chunk),
> });
>
> await sandbox.exec("node server.js", { background: true });
> console.log(sandbox.previewUrl); // https://<domain>
>
> await sandbox.stop(); // $0 while paused, /workspace is kept
> ```

> [tab] Python
>
> ```bash theme={null}
> pip install pxxl
> ```
>
> ```python theme={null}
> import os
> from pxxl import PxxlClient
>
> client = PxxlClient(api_key=os.environ["PXXL_API_KEY"])
>
> sandbox = client.sandboxes.create(
>     template="python",
>     size="small",
>     auto_stop_minutes=30,
>     labels={"project": "quickstart"},
> )
> sandbox.wait_until_ready()
>
> sandbox.files.write("/workspace/main.py", "print('hello from a sandbox')")
> result = sandbox.exec("python main.py", on_output=lambda chunk: print(chunk, end=""))
> print(result["exitCode"], sandbox.preview_url)
>
> sandbox.stop()
> ```

> [tab] CLI
>
> ```bash theme={null}
> npm install -g @pxxlapp/pxxl
> pxxl login --api-key $PXXL_API_KEY
>
> pxxl sandbox templates   # base, node, python, go, rust, bun and their versions
> pxxl sandbox create --name demo --template node --size small --label project=quickstart --wait
> pxxl sandbox exec demo -- node --version
> echo "console.log('hi')" | pxxl sandbox write demo /workspace/hi.js
> pxxl sandbox exec demo -- node hi.js
> pxxl sandbox stop demo
> ```
>
> `pxxl sandbox exec` streams output and exits with the command's exit code, so you can use it in scripts and CI.

> [tab] curl
>
> ```bash theme={null}
> # Create
> curl --request POST https://server.pxxl.app/api/v3/sandboxes \
>   --header "Authorization: Bearer $PXXL_API_KEY" \
>   --header "Content-Type: application/json" \
>   --data '{"name":"demo","template":"node","size":"small","labels":{"project":"quickstart"}}'
>
> # Poll until "status": "running"
> curl https://server.pxxl.app/api/v3/sandboxes/sbx_123 \
>   --header "Authorization: Bearer $PXXL_API_KEY"
>
> # Run a command
> curl --request POST https://server.pxxl.app/api/v3/sandboxes/sbx_123/exec \
>   --header "Authorization: Bearer $PXXL_API_KEY" \
>   --header "Content-Type: application/json" \
>   --data '{"command":"node --version"}'
>
> # Pause
> curl --request POST https://server.pxxl.app/api/v3/sandboxes/sbx_123/stop \
>   --header "Authorization: Bearer $PXXL_API_KEY"
> ```

## Serve a preview

Anything listening on `$PORT` (3000) inside the sandbox is served at the sandbox's `previewUrl`. Start your server in the background so `exec` returns:

```bash theme={null}
pxxl sandbox exec demo --background -- 'npm run dev -- --port $PORT'
pxxl sandbox get demo   # shows the preview URL
pxxl sandbox ps demo    # the server's pid; pxxl sandbox kill demo <pid> stops it
```

## Copy files in and out

Files up to 2 MB, text or binary:

```bash theme={null}
pxxl sandbox upload demo ./logo.png /workspace/public/logo.png
pxxl sandbox download demo /workspace/public/logo.png copy.png
```

In code, `sandbox.files.write(path, bytes)` and `sandbox.files.read(path, { binary: true })` (Python: `binary=True`) do the same.

## Save a snapshot

Installed something you'll want again? Snapshot the sandbox and start the next one from it. The home directory and `/workspace` come back; running processes don't.

```bash theme={null}
pxxl sandbox snapshot demo --name deps-installed
pxxl sandbox create --snapshot snap_123 --wait
```

See [snapshots](https://docs.pxxl.app/sandboxes/overview.md#snapshots).

## Clean up

A sandbox pauses itself after `autoStopMinutes` of inactivity (60 by default), and a paused sandbox costs nothing. Delete it when you no longer need `/workspace`:

```bash theme={null}
pxxl sandbox delete demo --yes
```

See [pricing and limits](https://docs.pxxl.app/sandboxes/overview.md#pricing).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.