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

# Ports and preview URLs

> Run a server in a sandbox and open its port at a URL you can share.

**You need** a running sandbox ([Quickstart](https://docs.styne.ai/quickstart.md)). The URL form is `https://<port>-<sandbox id>.styne.ai`.

## Run a web server and open a port

**1. Start the server**

Run it as a detached command, so the call returns while the server keeps running. The example writes a page and serves
its folder with Python's built-in server on port 3000. `detached: true` answers `202` as soon as the process runs.

```bash theme={"theme":"css-variables"}
curl -sS https://api.styne.ai/sandboxes/$SANDBOX_ID/commands \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"cmd": "sh", "args": ["-c", "mkdir -p /root/site && echo hello-from-a-sandbox > /root/site/index.html && exec python3 -m http.server 3000 --directory /root/site"], "detached": true}'
```

Bind to all interfaces, not only to `127.0.0.1`: Python's server and most frameworks do by default, `vite` and
`next dev` need `--host 0.0.0.0`.

**2. Check it from inside**

```bash theme={"theme":"css-variables"}
sleep 2
curl -sS https://api.styne.ai/sandboxes/$SANDBOX_ID/commands \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"cmd": "curl", "args": ["-sS", "http://127.0.0.1:3000/"]}'
```

The answer holds the command's `stdout`, the page: `hello-from-a-sandbox`.

**3. Open the port**

A sandbox serves its ports privately: every port needs the sandbox's traffic access token until you expose it as public
(a sandbox created from an E2B SDK, or with `network.allowPublicTraffic: true`, opens all its ports instead):

```bash theme={"theme":"css-variables"}
curl -sS -X POST https://api.styne.ai/sandboxes/$SANDBOX_ID/ports \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"port": 3000, "public": true}'
```

```text theme={"theme":"css-variables"}
{"port":3000,"public":true,"url":"https://3000-vm0123456789abcdef.styne.ai"}
```

Open that URL in a browser or `curl` it; no token is needed. With `"public": false` (the default) the port of a
token-gated sandbox is listed and still answers only with the token, which the create answer carries as `trafficAccessToken`:

```text theme={"theme":"css-variables"}
curl -H "e2b-traffic-access-token: $TRAFFIC_TOKEN" https://3000-<sandbox id>.styne.ai/
```

In the E2B SDKs `sbx.get_host(3000)` (`sbx.getHost(3000)`) returns the host part, `3000-<id>.styne.ai`.

**4. Close it and clean up**

```bash theme={"theme":"css-variables"}
curl -sS -X DELETE https://api.styne.ai/sandboxes/$SANDBOX_ID/ports/3000 \
  -H "Authorization: Bearer $STYNE_API_KEY"
curl -sS -X DELETE https://api.styne.ai/sandboxes/$SANDBOX_ID \
  -H "Authorization: Bearer $STYNE_API_KEY"
```

## The same from the SDKs

```python Python theme={"theme":"css-variables"}
from e2b import Sandbox

with Sandbox.create(template="base", timeout=900) as sbx:
    sbx.files.write("/root/site/index.html", "hello-from-a-sandbox\n")
    sbx.commands.run("python3 -m http.server 3000 --directory /root/site", background=True)
    sbx.commands.run("sleep 2")
    print(sbx.commands.run("curl -sS http://127.0.0.1:3000/").stdout.strip())
    print(sbx.get_host(3000))
```

```typescript TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create('base', { timeoutMs: 900_000 })
try {
  await sbx.files.write('/root/site/index.html', 'hello-from-a-sandbox\n')
  await sbx.commands.run('python3 -m http.server 3000 --directory /root/site', { background: true })
  await sbx.commands.run('sleep 2')
  console.log((await sbx.commands.run('curl -sS http://127.0.0.1:3000/')).stdout.trim())
  console.log(sbx.getHost(3000))
} finally {
  await sbx.kill()
}
```

Both print:

```text theme={"theme":"css-variables"}
hello-from-a-sandbox
3000-vm0123456789abcdef.styne.ai
```

## Port rules

A port a server listens on inside a sandbox is reachable at `https://<port>-<sandbox id>.<domain>`. A sandbox serves its
ports privately: every port needs the sandbox's traffic access token (header `e2b-traffic-access-token`, or the `?token=`
query for `wss://`) unless you make it public. A sandbox created from an E2B SDK serves its ports publicly, as E2B does,
and so does one created with `network.allowPublicTraffic: true`; a sandbox made through the Daytona API is private unless
it sets `public`. You can expose a port on purpose, make it public, and close it again, on any sandbox.

Exposing answers `{"port":3000,"public":true,"url":"https://3000-<id>.<domain>"}`; the list (`GET /sandboxes/{id}/ports`)
is an array of those.

* `public` defaults to `false`: the port is listed with its URL and still needs the token. Send `true` to open it to
  anyone with the URL. Send the same port again to change the flag.
* `DELETE` closes the port: it is listed as private and needs the token, also on a sandbox made with public traffic. It
  answers `204` also for a port that was not exposed.
* `49983`, the sandbox daemon's port, cannot be exposed. A sandbox exposes at most 32 ports. A browser session's ports
  always need the token.
* The list holds the ports you exposed here, not every port a process listens on. `url` is absent on a deployment that
  serves no sandbox domain.
* A sandbox created with `network.allowPublicTraffic` opens all its ports until you set a port's flag or `DELETE` it: the
  choice you make for a port wins over the sandbox's setting.
* A change reaches the edge within about 5 seconds, so a port made private may still answer without a token for that long.
* The choice belongs to the running sandbox and ends with it. It is stored, so an API restart keeps it and every API
  instance shows the same ports within 5 seconds. Until a restarted instance has loaded the table, every port on it is
  token-gated. When the store cannot be reached, a change is refused with `503` and nothing changes.
* A sandbox of another account is a `404`, the same as one that does not exist.
* The URL works while the sandbox runs. A sandbox that is paused does not serve; it wakes when you resume it, and the
  process is where it was. A killed or unknown sandbox answers `404` to everyone, with or without a token.
* Long-running servers need a long `timeout` (up to 86,400 seconds) or a refresh: see
  [Timeouts, pause and resume](https://docs.styne.ai/pause-tiers.md).
* Websocket upgrades work on the same URL; send the token as `?token=` for a private port.

> [!WARNING]
>
> A public port is open to anyone who has the URL. Do not serve secrets on it, and close it when you are done.

## Operations

| Method | Path | What it does |
| - | - | - |
| `GET` | `/sandboxes/{id}/ports` | List the ports a sandbox exposes, with URL and public flag |
| `POST` | `/sandboxes/{id}/ports` | Expose a port, public (no token) or token-gated |
| `DELETE` | `/sandboxes/{id}/ports/{port}` | Hide an exposed port again |


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