---
title: Run Sandbox Commands over HTTP
description: Run commands in a sandbox, stream their output, write to stdin, and stop them with plain HTTPS requests from any language.
---
The sandbox HTTP API runs commands in a [sandbox](/sandboxes) with plain HTTPS requests. Use it from any language or runtime that can make an HTTP request, without the TypeScript SDK or a WebSocket.

**Note:** This is a preview feature under active development.

## How it works

Every command runs as a named session on the sandbox. The command keeps running after the request that started it ends, and the sandbox keeps its output and exit code after it exits. Later requests address the command by its session name.

Calling the API takes two steps:

1. Request a session token from the Public API. The token is valid for 5 minutes and covers any number of requests to one sandbox.
2. Send requests to the sandbox gateway with that token.

The gateway speaks HTTP/1.1 and closes the connection after each response. It doesn't send CORS headers, so call it from a server or script rather than a browser.

## Get a session token

`POST /api/v1/shell-sessions` returns a session token for one sandbox and the URL of the gateway to send it to.

```bash
RESPONSE=$(curl --silent --request POST \
  --url https://backboard.railway.com/api/v1/shell-sessions \
  --header "Authorization: Bearer $RAILWAY_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "environmentId": "<environment-id>",
    "instanceId": "<sandbox-id>",
    "kind": "sandbox",
    "scope": "shell"
  }')
```

All four fields are required:

| Field | Value |
|-------|-------|
| `environmentId` | The ID of the environment the sandbox runs in. |
| `instanceId` | The sandbox ID, as shown by `railway sandbox list` or `sandbox.id` in the SDK. |
| `kind` | Must be `sandbox`. |
| `scope` | Must be `shell`. |

The response holds the token and the gateway URL:

```json
{
  "token": "eyJhbGciOiJFUzI1NiIs...",
  "baseUrl": "https://ssh.railway.com:2226",
  "expiresInSec": 300
}
```

The examples on this page read both values into variables:

```bash
export SESSION_TOKEN=$(echo "$RESPONSE" | jq -r .token)
export BASE_URL=$(echo "$RESPONSE" | jq -r .baseUrl)
```

Request a new token before `expiresInSec` runs out. Commands keep running when a token expires.

### Tokens

The endpoint accepts these Public API tokens:

| Token type | Header | Notes |
|------------|--------|-------|
| Account token | `Authorization: Bearer <token>` | |
| Workspace token | `Authorization: Bearer <token>` | Access follows the member who created the token. |
| OAuth token | `Authorization: Bearer <token>` | Needs the `workspace:member` or `project:member` scope. |
| Project token | `Project-Access-Token: <token>` | Only for sandboxes in the token's own environment. |

You need at least the member role on the sandbox's project. To create a token, see [Creating a token](/integrations/api#creating-a-token).

A failed request returns a JSON body with an `error` message, for example `400` for a missing field, `403` when you don't have access, and `404` when the sandbox doesn't exist.

## Run a command

`POST /http/exec` starts a command. With `"wait": true`, the request returns once the command exits, with its output and exit code.

```bash
curl --request POST \
  --url "$BASE_URL/http/exec" \
  --header "Authorization: Bearer $SESSION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"command": "npm test", "cwd": "/app", "wait": true}'
```

```json
{
  "sessionName": "quiet-comet-7kq",
  "exitCode": 0,
  "stdout": "> test\n42 passing\n",
  "stderr": "",
  "truncated": false,
  "timedOut": false
}
```

The request body takes these fields. Unknown fields are rejected, and the body can be up to 1 MiB.

| Field | Description |
|-------|-------------|
| `command` | Required. The command to run, interpreted by the sandbox's shell. Up to 256 KiB. |
| `sessionName` | A name for the command. Railway generates one, such as `quiet-comet-7kq`, when you omit it. See [Retry safely](#retry-safely). |
| `cwd` | An absolute path to an existing directory. Defaults to the image's working directory. |
| `env` | Environment variables, as an object of strings. They override variables of the same name. |
| `wait` | `true` to return when the command exits. Defaults to `false`. |
| `timeoutSec` | Seconds before Railway stops the command. `0` or omitted means no timeout. Up to 604,800 (7 days). |
| `stdin` | `true` to keep stdin open for [writing](#write-to-stdin). Defaults to `false`, which closes stdin at start. |
| `maxOutputBytes` | With `wait`, the bytes of stdout and of stderr to return. Defaults to 16,000, up to 1,048,576. |

The response has these fields:

| Field | Description |
|-------|-------------|
| `sessionName` | The command's session name. Use it in every later request. |
| `exitCode` | The exit code. `-1` when a signal ended the command, and `null` when the sandbox stopped before the command exited. |
| `stdout`, `stderr` | The first `maxOutputBytes` of each stream. |
| `truncated` | `true` when output was cut. [Read the logs](#read-output) for all of it. |
| `timedOut` | `true` when `timeoutSec` ran out. |

When `timeoutSec` runs out, Railway sends `SIGTERM` to the command, then `SIGKILL` 5 seconds later if it's still running.

Output is returned as UTF-8 text, and invalid bytes are replaced. To read binary output, encode it in the command, for example with `base64`.

### Wait for a long-running command

A request with `"wait": true` waits up to 60 seconds. If the command is still running, the response is `202 Accepted`:

```json
{ "sessionName": "quiet-comet-7kq", "running": true }
```

The command keeps running. [Check it](#check-a-command) with `?wait=true` until it exits, then read its output from the [logs](#read-output).

### Run a command in the background

Omit `wait` to start a command and return immediately. The response is `202 Accepted` with the same body.

```bash
curl --request POST \
  --url "$BASE_URL/http/exec" \
  --header "Authorization: Bearer $SESSION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"command": "npm run build", "sessionName": "build-1042"}'
```

A command counts as exited once every process writing to its stdout and stderr has exited. To leave a server running after the command returns, redirect the server's output, for example `npm start > server.log 2>&1 &`.

## Check a command

`GET /http/exec/{sessionName}` returns whether the command is running and, once it exits, its exit code.

```bash
curl --url "$BASE_URL/http/exec/build-1042?wait=true" \
  --header "Authorization: Bearer $SESSION_TOKEN"
```

```json
{
  "sessionName": "build-1042",
  "running": false,
  "exitCode": 0,
  "exitedAt": "2026-11-27T14:02:11Z",
  "timedOut": false
}
```

With `?wait=true`, the request waits up to 60 seconds for the command to exit. If it's still running, the response has `"running": true`, and you can send the request again.

## Read output

`GET /http/exec/{sessionName}/logs` streams the command's output as newline-delimited JSON, one record per line. Without `follow`, the response ends at the output written so far. With `?follow=true`, it stays open until the command exits.

```bash
curl --no-buffer \
  --url "$BASE_URL/http/exec/build-1042/logs?follow=true" \
  --header "Authorization: Bearer $SESSION_TOKEN"
```

```plaintext
{"type":"stdout","seq":1,"data":"> build\n"}
{"type":"stderr","seq":2,"data":"warning: unused import\n"}
{"type":"stdout","seq":3,"data":"done in 41s\n"}
{"type":"exit","exitCode":0,"exitedAt":"2026-11-27T14:02:11Z"}
```

| Record `type` | Description |
|---------------|-------------|
| `stdout`, `stderr` | Output from that stream. `seq` orders records across both streams. |
| `truncated` | Output up to `seq` is no longer stored. |
| `exit` | The last record, with `exitCode` and `exitedAt`. `timedOut` appears when `true`. |

Only an `exit` record means the command has finished. A response that ends without one was cut off or read a running command. To resume, pass the last `seq` you received as `?fromSeq=`, and the response starts with the next record.

The sandbox stores about the most recent 8 MiB of each command's output. To find out whether older output was dropped, read with `?fromSeq=0`. The response then starts with a `truncated` record when it was.

## Write to stdin

Start the command with `"stdin": true`, then send bytes with `POST /http/exec/{sessionName}/stdin`. The request body is written to the command's stdin as is.

```bash
curl --request POST \
  --url "$BASE_URL/http/exec/agent-7/stdin" \
  --header "Authorization: Bearer $SESSION_TOKEN" \
  --data-binary @prompt.txt
```

A `204 No Content` response confirms the whole body was written to the command's stdin. Concurrent requests to the same command never interleave: each body is written in one piece.

Add `?close=true` to close stdin after the body. Send it with an empty body to close stdin on its own.

A request fails with `502 stdin_failed` when the command stops reading or the body sends no bytes for 60 seconds. Part of the body may have been written already, so check the command's state before sending it again.

## Stop a command

`POST /http/exec/{sessionName}/kill` sends a signal to the command and its child processes. The default signal is `TERM`.

```bash
curl --request POST \
  --url "$BASE_URL/http/exec/build-1042/kill" \
  --header "Authorization: Bearer $SESSION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"signal": "INT"}'
```

The response is `{"delivered": true}` once the signal is sent. The command can take time to exit, so [check it](#check-a-command) with `?wait=true` to confirm. The signal can be `HUP`, `INT`, `QUIT`, `KILL`, or `TERM`, with or without the `SIG` prefix. A command that has already exited returns `409 exited`.

## Retry safely

The session name makes `POST /http/exec` safe to retry. Send a `sessionName` with every command you might retry. A retry with the same name and the same `command`, `cwd`, `env`, `stdin`, and `timeoutSec` returns the command that's already running or finished, and doesn't start it again. Railway sets the `Idempotent-Replayed: true` header on these responses.

A retry with the same name but a different command returns `409 idempotency_mismatch`.

Session names are 1 to 64 characters of lowercase letters, digits, and hyphens, and can't start with a hyphen. Anyone with access to the sandbox can use any session name in it.

## Keep output after a command exits

The sandbox keeps a command's output and exit code after it exits, until the sandbox is destroyed or the time below runs out:

| After the command exits | Kept for |
|-------------------------|----------|
| Its output was read to the `exit` record, by `logs` or a `wait` request | 3 hours |
| Otherwise | 24 hours |

After that, requests for the session name return `404 not_found`, and a create with the same name starts the command again.

## Limits

| Limit | Value |
|-------|-------|
| Session token lifetime | 5 minutes |
| `wait` on a create or a status check | 60 seconds |
| Command | 256 KiB |
| Create request body | 1 MiB |
| Output stored per command | About 8 MiB |
| Concurrent connections and SSH sessions per sandbox | 50 |

The gateway rate limits requests per account, or per project token, whichever minted the session token. The limit is shared with the CLI's and SDK's connections to sandboxes. A limited request returns `429 Too Many Requests` with a plain-text body and a `Retry-After` header that gives the seconds to wait.

## Errors

Gateway errors have a JSON body with a machine-readable `error` code and a `message`:

```json
{
  "error": "not_found",
  "message": "no session build-1042; sessions are kept 3h after they are fully read, 24h otherwise"
}
```

| Status | `error` | Cause |
|--------|---------|-------|
| 400 | `invalid_request` | A malformed body or query parameter |
| 400 | `invalid_session_name` | A session name with characters other than lowercase letters, digits, and hyphens |
| 400 | `start_failed` | The command couldn't start, for example because `cwd` doesn't exist |
| 401 | `unauthorized` | A missing, invalid, or expired session token. Request a new one. |
| 403 | `forbidden` | The session token's scope isn't `shell` |
| 403 | `denied` | Access to the sandbox was refused. See `message` for the reason. |
| 404 | `not_found` | No session with that name |
| 405 | `method_not_allowed` | The wrong HTTP method. The `Allow` header names the right one. |
| 409 | `idempotency_mismatch` | The session name is in use by a different command |
| 409 | `exited` | The command has already exited |
| 409 | `stdin_closed` | The command's stdin is closed |
| 409 | `not_durable` | The target can't keep sessions. Use a sandbox. |
| 429 | `too_many_sessions` | The sandbox has 50 connections and SSH sessions open |
| 501 | `unsupported_by_sandbox` | The sandbox's runtime is too old for this request. It works after the sandbox restarts. |
| 502 | `exit_unconfirmed` | The command's exit wasn't reported. Check its status to get it. |
| 502 | `stdin_failed` | The body didn't all reach stdin. See [Write to stdin](#write-to-stdin). |
| 502 | `routing_failed`, `sandbox_unavailable`, `sandbox_error` | The gateway couldn't reach the sandbox or open a session. Retry the request. |
| 504 | `sandbox_unreachable` | The sandbox didn't respond in time. Retry the request. |