# Preview URLs

A preview opens one port of a sandbox at an https address of its own, so a person or a program can use the dev server, app or API your agent started. Previews are private unless you say otherwise: the link carries a signed token, and nobody without it gets in.

## Open a port

Start the server inside the sandbox on `0.0.0.0` (a server on `127.0.0.1` cannot be reached from outside its container), then ask for a preview with a key that has the `sandboxes:write` scope.

```
curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/previews \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"port":3000}'
# {"preview":{"id":"sbx_…:3000","port":3000,"public":false,
#   "url":"https://3000-k2v…q7.kosa8labs.com/?kosa8_preview=eyJz…",
#   "host":"3000-k2v…q7.kosa8labs.com","token":"eyJz…","expires_at":"2026-11-14T13:00:00.000Z",…}}
```

The address is random and names the port; the sandbox id is never in it. Each preview has its own origin, so one preview's page cannot read another's cookies.

## Private (the default)

- **In a browser**, open `url`. The first visit trades the token for an `HttpOnly`, `Secure` cookie on that address alone and redirects to the same page without the token, so it does not stay in the address bar or leak in a `Referer`.

- **From a program**, send the token in a header: `X-Kosa8-Preview-Token: eyJz…`. Your app's own `Authorization` header and cookies reach it untouched; kosa8's token and cookie are removed first.

- A link works for `expires_in_s` seconds: 3600 by default, 60 to 604800 (7 days). Ask for another with `POST /v1/sandboxes/{id}/previews/{port}/link`.

## Public

For a webhook receiver or a demo anyone may open, send `"public": true`. The answer has no token and the URL works for anyone who has it. Sending the same port again with `"public": false` closes it to strangers and keeps the address. Opening, changing and closing a preview are recorded in the audit log.

## End every link

```
# move the preview to a new address: every earlier link and cookie stops working
curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/previews/3000/rotate -H "Authorization: Bearer $KOSA8_API_KEY"
# close it
curl -s -X DELETE https://api.kosa8.com/v1/sandboxes/sbx_…/previews/3000 -H "Authorization: Bearer $KOSA8_API_KEY"
# what is open
curl -s https://api.kosa8.com/v1/sandboxes/sbx_…/previews -H "Authorization: Bearer $KOSA8_API_KEY"
```

Deleting the sandbox ends its previews too. A frozen organization's previews answer `423` until it is unfrozen.

## Your own domain

**Not switched on yet:** on kosa8 Cloud today, adding a domain answers **501** `not_available` (Cloudflare for SaaS is not configured). When it is, it works as follows.

Serve a preview at a name under your own domain, such as `preview.example.com`, instead of its random address. kosa8 asks Cloudflare (Cloudflare for SaaS) for the hostname and its certificate; you publish the DNS records it answers with.

```
curl -s -X POST https://api.kosa8.com/v1/sandboxes/sbx_…/previews/3000/domains \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"hostname":"preview.example.com"}'
# 201 {"domain":{"hostname":"preview.example.com","status":"pending","records":[
#   {"type":"CNAME","name":"preview.example.com","value":"domains.…","purpose":"routing"},
#   {"type":"TXT","name":"_cf-custom-hostname.preview.example.com","value":"…","purpose":"ownership"},
#   {"type":"TXT","name":"_acme-challenge.preview.example.com","value":"…","purpose":"certificate"}],…}}
```

1. At your DNS provider, add the `CNAME`. It routes the name to kosa8. The two `TXT` records are optional: they prove the name is yours and issue its certificate before the CNAME moves, so a name already serving traffic has no gap.

Poll the domain until its `status` is `active` (usually a few minutes after the CNAME resolves):

```
curl -s https://api.kosa8.com/v1/sandboxes/sbx_…/previews/3000/domains/preview.example.com -H "Authorization: Bearer $KOSA8_API_KEY"
```

3. `https://preview.example.com/` now serves port 3000, private or public as the preview is. A private preview still needs its signed link or token at the new name.

The same calls work by the preview's id, `{sandbox_id}:{port}` (the `id` field of a preview): `POST /v1/previews/sbx_…:3000/domains`. A domain follows the sandbox and port, so a rotate keeps it. `DELETE …/domains/{hostname}` removes it; closing the preview or deleting the sandbox removes its domains too. The dashboard's Previews pane has the same form. At most 5 domains per preview and 50 per organization; names under kosa8's own domains are refused, and a name another organization uses answers `409 hostname_taken`. Until custom domains are switched on for your kosa8 Cloud, adding one answers `501 not_available`.

## What a visitor can get back

| Status | Why |
|---|---|
| `401` | Private preview without a valid token, or the link has expired. |
| `404` | No preview at this address (rotated or closed). |
| `410` | The sandbox was deleted or passed its `delete_after`. |
| `502` | Nothing listens on that port, or it listens on 127.0.0.1 only. |
| `503` | The sandbox is asleep. Wake it (`POST /v1/sandboxes/{id}/wake`) and reload, or open the preview with `"wake": true`. |

Open a preview with `"wake": true` and a request to a sleeping sandbox wakes it instead of answering 503, at most `wake_per_hour` times in a rolling hour (default 12, 1 to 120; past that `429`). Sending an open port again answers with a fresh link and applies `public`, `wake` and `wake_per_hour`.

WebSockets and server-sent events pass through, so dev servers with hot reload work.

## Limits

- At most 20 open ports per sandbox.

- Opening a preview answers `501 previews_not_configured` until preview URLs are switched on for your kosa8 Cloud, and `501 not_available` on a host that does not serve them yet.

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