# Snapshots

Set a sandbox up once (clone the repo, install the packages, warm the caches), save it, and start every later sandbox from there.

**CLI**

```
ID=$(kosa8 cloud sandbox create --template python --ttl 15m --json | jq -r .sandbox.id)
kosa8 cloud sandbox exec $ID -- sh -c 'pip install --quiet --target /workspace/lib six && echo ok'

# A snapshot outlives the sandbox: start as many copies as you like from it
SNAP=$(kosa8 cloud snapshot create $ID --name deps-installed --json | jq -r .snapshot.id)
kosa8 cloud sandbox rm $ID

COPY=$(kosa8 cloud sandbox create --from-snapshot $SNAP --ttl 15m --json | jq -r .sandbox.id)
kosa8 cloud sandbox exec $COPY -- ls /workspace/lib

kosa8 cloud sandbox rm $COPY
kosa8 cloud snapshot rm $SNAP
```

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"
JSON='Content-Type: application/json'
SBX=$(curl -s $API/sandboxes -H "$AUTH" -H "$JSON" -d '{"template":"python","ttl_s":900}' | jq -r .sandbox.id)
curl -s $API/sandboxes/$SBX/exec -H "$AUTH" -H "$JSON" \
  -d '{"cmd":["sh","-c","echo warmed > /workspace/cache"]}' > /dev/null

SNAP=$(curl -s $API/sandboxes/$SBX/snapshots -H "$AUTH" -H "$JSON" \
  -d '{"name":"warm-cache"}' | jq -r .snapshot.id)
curl -s -X DELETE $API/sandboxes/$SBX -H "$AUTH" > /dev/null    # the snapshot stays

COPY=$(curl -s $API/sandboxes -H "$AUTH" -H "$JSON" -d "{\"snapshot\":\"$SNAP\",\"ttl_s\":900}" | jq -r .sandbox.id)
until [ "$(curl -s $API/sandboxes/$COPY -H "$AUTH" | jq -r .sandbox.state)" = running ]; do sleep 1; done
curl -s $API/sandboxes/$COPY/exec -H "$AUTH" -H "$JSON" -d '{"cmd":["cat","/workspace/cache"]}' | jq -r .stdout

curl -s -X DELETE $API/sandboxes/$COPY -H "$AUTH" > /dev/null
curl -s -X DELETE $API/snapshots/$SNAP -H "$AUTH"
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

with cloud.sandboxes.create(template="python", ttl_s=900) as box:
    box.exec("echo warmed > /workspace/cache")
    snap = box.snapshot("warm-cache")            # kept until you delete it

with cloud.sandboxes.create(snapshot=snap["id"], ttl_s=900) as copy:
    print(copy.exec("cat /workspace/cache").stdout)   # warmed

cloud.snapshots.delete(snap["id"])
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();

const box = await cloud.sandboxes.create({ template: 'python', ttlS: 900 });
await box.exec('echo warmed > /workspace/cache');
const snap = await box.snapshot('warm-cache');   // kept until you delete it
await box.delete();

const copy = await cloud.sandboxes.create({ snapshot: snap.id, ttlS: 900 });
console.log((await copy.exec('cat /workspace/cache')).stdout); // warmed
await copy.delete();
await cloud.snapshots.delete(snap.id);
```

## Snapshots and checkpoints

**Checkpoint**

Belongs to one sandbox and goes with it. The newest 10 are kept. For undo.

**Snapshot**

Belongs to your organization and is kept until you delete it, whatever happens to the sandbox it came from. For starting new sandboxes.

## The calls

| `POST /v1/sandboxes/{id}/snapshots` | Save it, with an optional `name`. The sandbox keeps running. |
|---|---|
| `POST /v1/sandboxes` `{"snapshot": "snap_…"}` | Start a new sandbox from it |
| `GET /v1/snapshots` | List them, newest first, with `max`, how many you may keep |
| `DELETE /v1/snapshots/{id}` | Delete it. Sandboxes made from it are not touched. |

How many snapshots you can keep depends on your plan (past it: `402 plan_limit`), with at most 50 per organization at once (`429 snapshot_limit`). A snapshot's bytes are billed as storage until you delete it; see [pricing](https://kosa8.com/pricing/). Sandboxes with secrets, or on dedicated cores, cannot be snapshotted.

For background commands, environment and warm pools built on snapshots, see [snapshots, background and env](https://docs.kosa8.com/guides/cloud-snapshots-background/).

---
Source: https://docs.kosa8.com/concepts/snapshots/
