> ## 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.

# SSH and port forwarding

> [//]: # (smoke: not executed, it opens an SSH session; live proof 2026-11-23 through ssh.styne.ai:2222)

Open a sandbox with stock OpenSSH: ssh, scp, sftp, port forwards and the Remote-SSH extension of VS Code and Cursor.

**You need** a running sandbox ([Quickstart](https://docs.styne.ai/quickstart.md)) and an OpenSSH public key. You authorize an OpenSSH public key on a running sandbox, then connect to Styne's SSH front door with the
sandbox's id as the user name. The front door checks the key against that sandbox only and passes the connection on to
the sandbox's own `sshd`, so terminals, file copy and forwards behave as on any Linux host.

## Authorize a key

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.styne.ai/sandboxes/$SANDBOX_ID/ssh-keys" \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "Content-Type: application/json" \
  -d "{\"publicKey\": \"$(cat ~/.ssh/id_ed25519.pub)\"}"
```

The answer names where to connect:

```json theme={"theme":"css-variables"}
{
  "keyId": "9f3a1c52d07be864",
  "fingerprint": "SHA256:...",
  "type": "ssh-ed25519",
  "comment": "me@laptop",
  "host": "ssh.styne.ai",
  "port": 2222,
  "hostKeyFingerprint": "SHA256:...",
  "user": "sbx_...",
  "command": "ssh -p 2222 sbx_...@ssh.styne.ai"
}
```

`201` is a new key, `200` a key the sandbox already had. Adding a key also makes sure the sandbox's `sshd` runs; it answers within a few seconds. The sandbox must be running or asleep after idling (it wakes for the setup and for each connection); one you paused answers `409`, resume it first.

* The user name is the sandbox's id, not the account inside the sandbox. You land as the sandbox's `user` account in
  `/home/user`.
* Keys of type `ssh-ed25519`, `ecdsa-sha2-nistp256`, `ecdsa-sha2-nistp384`, `ecdsa-sha2-nistp521` and `ssh-rsa` with
  2048 bits or more are accepted, one line, without `authorized_keys` options. A sandbox holds at most 16 keys.
* A key opens only the sandbox it was added to. A key added on one sandbox never opens another, also in your account.
* A template without an OpenSSH server answers `422`. The base template includes one.

```bash theme={"theme":"css-variables"}
curl -sS "https://api.styne.ai/sandboxes/$SANDBOX_ID/ssh-keys" -H "Authorization: Bearer $STYNE_API_KEY"

curl -sS -X DELETE "https://api.styne.ai/sandboxes/$SANDBOX_ID/ssh-keys/9f3a1c52d07be864" \
  -H "Authorization: Bearer $STYNE_API_KEY"
```

`DELETE` takes the key away from the front door at once, so no new connection opens with it. It answers `204`, also for a
key that was not there. It also ends the sandbox's open SSH sessions; the keys that remain connect again at once.

An SSH key belongs to the API key that added it. When that API key is revoked, for example because its member was
removed from the organisation, the SSH keys it added are removed and the open SSH sessions of those sandboxes end,
within 10 seconds. No new connection opens with them in the meantime.

## Connect with ssh

```bash theme={"theme":"css-variables"}
ssh -p 2222 -i ~/.ssh/id_ed25519 "$SANDBOX_ID@ssh.styne.ai"
ssh -p 2222 "$SANDBOX_ID@ssh.styne.ai" -- 'cd /home/user/app && npm test'
```

The front door listens on port 2222 of `ssh.styne.ai` (`port` in the answer). It has one host key that stays the same across restarts, and its fingerprint is `hostKeyFingerprint` in the
answer above. On the first connection compare it with the fingerprint `ssh` prints before you accept it.

If the sandbox is paused or has ended, the front door prints that before it refuses the login. Resume the sandbox and
connect again; a connection never resumes a sandbox by itself.

## Copy files

```bash theme={"theme":"css-variables"}
scp -P 2222 -i ~/.ssh/id_ed25519 ./build.tar "$SANDBOX_ID@ssh.styne.ai:/home/user/build.tar"
scp -P 2222 -i ~/.ssh/id_ed25519 "$SANDBOX_ID@ssh.styne.ai:/home/user/out.log" .
sftp -P 2222 "$SANDBOX_ID@ssh.styne.ai"
```

For single files the [files API](https://docs.styne.ai/files.md) needs no key; scp suits whole trees.

## Port forwarding

A forward reaches a server inside the sandbox from your machine, or the other way round, over the same SSH connection.
`-L` makes your `localhost:8080` answer with the server on port 3000 inside the sandbox. `-R` makes the sandbox's
`127.0.0.1:9000` answer with the server on your machine's port 9000, for as long as `ssh` runs:

```bash theme={"theme":"css-variables"}
ssh -p 2222 -N -L 8080:127.0.0.1:3000 "$SANDBOX_ID@ssh.styne.ai"
ssh -p 2222 -N -o ExitOnForwardFailure=yes -R 9000:127.0.0.1:9000 "$SANDBOX_ID@ssh.styne.ai"
```

A `-R` listener on the sandbox side binds to loopback unless you name another address and ask for it
(`-R 0.0.0.0:9000:...`); neither makes the port public. To publish a port on an HTTPS address use
[ports](https://docs.styne.ai/ports.md).

## VS Code and Cursor

Remote-SSH works against a sandbox. Put a block per sandbox in `~/.ssh/config`:

```text theme={"theme":"css-variables"}
Host styne-sandbox
  HostName ssh.styne.ai
  Port 2222
  User sbx_...
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes
  ServerAliveInterval 30
```

Then run "Remote-SSH: Connect to Host..." and pick `styne-sandbox`. The editor installs its server in the sandbox, so the
sandbox needs outbound network access and `curl` or `wget`. When the sandbox ends, the entry stops working: add the key
to the next sandbox and change `User`.

## Limits

* A connection ends when the sandbox pauses or ends. The keys of an ended sandbox are gone.
* An API restart forgets the keys of every sandbox: add them again. Nothing is opened to anyone in the meantime.
* There is no password login; the only account is `user`.

A sandbox of another account is a `404`, the same as one that does not exist.

## Operations

| Method | Path | What it does |
| - | - | - |
| `GET` | `/sandboxes/{id}/ssh-keys` | List the SSH public keys authorized on a sandbox |
| `POST` | `/sandboxes/{id}/ssh-keys` | Authorize an OpenSSH public key and get the host, port and user to connect to |
| `DELETE` | `/sandboxes/{id}/ssh-keys/{keyId}` | Revoke an authorized SSH public key |


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