# Embed a sandbox

Show a sandbox's terminal or desktop inside your own product: an agent's work for a user to watch, or a live environment they can type into. You ask the API for an embed and put its URL in an `<iframe>`. The URL is signed, expires, and only the origins you list can frame it.

## 1. Make an embed (server side)

```
curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/embed \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"ui":"terminal","mode":"view","ttl_s":3600,"origins":["https://app.example.com"]}'
# 201 {"embed":{"id":"emb_…","sandbox_id":"sbx_…","mode":"view","ui":"terminal",…,"expires_at":…,
#   "url":"https://api.kosa8.com/embed/emb_….…","snippet":"<iframe src=…>"}}
```

| Field | What |
|---|---|
| `ui` | `terminal` (default) or `desktop` (the [kosa8-desktop](https://docs.kosa8.com/guides/cloud-desktop/) image's screen, through noVNC). |
| `mode` | `view` (default): watch only. `interact`: keyboard and mouse too. |
| `ttl_s` | How long the URL works: 60 seconds to 7 days, default 1 hour, never past the sandbox's `delete_after`. |
| `origins` | 1 to 10 origins (`https://host[:port]`, or `http://localhost:port` for development) that may frame it and receive its events. |
| `command` | Terminal only: the program to show, such as `["python3"]`. Default: a login shell, or in view mode the sandbox's tmux session. |

Make the embed on your server with your API key, and hand only its `url` to the page. The URL is the credential: anyone who has it can open the embed (from one of your origins) until it expires. `DELETE /v1/sandboxes/{id}/embeds/{embed_id}` ends it sooner; `GET /v1/sandboxes/{id}/embeds` lists the live ones. Creating, opening and revoking an embed are recorded in the audit log.

## 2. Put it on the page

```
<iframe id="sbx" src="EMBED_URL" width="960" height="600" style="border:0"
        allow="clipboard-read; clipboard-write"></iframe>
<script>
  // Lifecycle events from the embed. Check the origin: only kosa8's API sends these.
  window.addEventListener("message", (e) => {
    if (e.origin !== "https://api.kosa8.com" || e.data?.source !== "kosa8") return;
    switch (e.data.type) {
      case "ready":              console.log("connected", e.data.ui, e.data.mode); break;
      case "exited":             console.log("program ended", e.data.code); break;
      case "approval_requested": showApproval(e.data.approval); break; // {id, tool, subject, reason, at}
      case "expired":            document.getElementById("sbx").remove(); break;
      case "error":              console.warn(e.data.message); break;
    }
  });
</script>
```

Every event is `{source: "kosa8", type, embed_id, sandbox_id, …}`, posted only to the origins the embed lists.

| Event | When |
|---|---|
| `ready` | The terminal is connected, or the desktop has loaded. |
| `exited` | The terminal's program ended (`code` is its exit code), or the sandbox stopped or was deleted. |
| `approval_requested` | An [agent hook](https://docs.kosa8.com/guides/agent-hooks/) in this sandbox is waiting for a person (effect `ask`). The hook's event must carry the sandbox's id as `sandbox_id`. |
| `expired` | The URL's time is up, or it was revoked. |
| `error` | Something the page could not recover from. |

## Watching an agent

Each terminal embed is a terminal of its own, in a fresh container with the sandbox's `/workspace`. To let people watch one program, run it in tmux with its socket in `/workspace`; a view-only embed attaches to that session read-only by default:

```
command -v tmux || apk add --no-cache tmux || { apt-get update && apt-get install -y tmux; }   # the templates have no tmux
mkdir -p /workspace/.kosa8-tmux
tmux -S /workspace/.kosa8-tmux/main.sock new -d -s main 'claude -p "fix the failing tests"'
```

The view-only embed installs tmux in its own container when the image has none, and waits for the session if the program has not started yet, so an embed made first begins showing it once it does. Images with tmux already (the `universal` templates) skip the install. On the 256 MiB templates (`base`, `python`) installing tmux can run out of memory: use a `universal` template or a size such as `2x4` for a sandbox people will watch. Start the program with a background exec (`kosa8 cloud sandbox exec -d`) or from a terminal, so tmux outlives the command that started it.

## What view-only means

View-only is enforced on kosa8's side, not by the page. A view-only terminal goes through the kosa8 API, which passes the output on and drops every key the viewer's browser sends. A view-only desktop connects to a second VNC server in the sandbox (port 6081) that ignores the mouse and keyboard.

## Limits

- At most 50 live embeds per sandbox.

- A desktop embed frames noVNC through a private [preview](https://docs.kosa8.com/guides/cloud-previews/) (opened for you on port 6080 or 6081). Its cookie is partitioned (CHIPS), which Chrome, Edge and Firefox allow in a frame. Safari blocks third-party cookies, so there it shows a sign-in error; use a terminal embed, or open the preview's link in a new tab.

- An interactive terminal connects straight to the sandbox's host over `wss://`; your page's CSP does not need to allow it, since it runs inside the embed.

- A terminal already open stays open after its embed expires or is revoked, as any terminal does once connected; new connections are refused.

---
Source: https://docs.kosa8.com/guides/cloud-embed/
