# Yurt API

Make, sleep, wake and delete VMs, and run commands in them, over HTTPS. Get told about changes with webhooks.

Base URL: `https://getyurt.dev/api/v1`

## Authentication

Make a key in the dashboard under **API keys**. It starts with `yurt_` and is shown once. Send it as a bearer token:

```
curl https://getyurt.dev/api/v1/vms -H "Authorization: Bearer $YURT_API_KEY"
```

A key is either:

- **full**: everything below.
- **read**: `GET` requests only (list and inspect).

Delete a key in the dashboard to revoke it right away.

## Errors

Errors return a status code and a JSON body with one field: `{"error": "what went wrong"}`.

- `400` bad input (name taken, limit reached, bad JSON)
- `401` missing or wrong API key
- `402` out of credits: add credits, then wake the VM
- `403` the key is read-only, or the account is blocked
- `404` no such VM or endpoint
- `504` an `exec` command ran past its timeout

## The VM object

```
{
  "name": "agent-1",
  "state": "running",
  "sleeps_in": "12m",
  "created": 1796220273,
  "awake_seconds_this_month": 51000,
  "cost_cents_this_month": 25.5,
  "ssh": "ssh agent-1@ssh.getyurt.dev"
}
```

- `state`: `running`, `sleeping` (memory kept, costs nothing) or `stopped`.
- `sleeps_in`: for a running VM, the time until it sleeps if it stays idle; `never` if idle sleep is off; `null` otherwise.
- Times are Unix seconds. Money is in US cents.

## Endpoints

### GET /me

Your credits and limits.

```
curl https://getyurt.dev/api/v1/me -H "Authorization: Bearer $YURT_API_KEY"
```

```
{"account": "u3", "credits_cents": 1873.4, "spent_cents_this_month": 126.6,
 "price_per_hour": 0.018, "vm_size": "2 vCPU · 2 GB RAM · 10 GB disk", "max_vms": 3}
```

### GET /vms

All your VMs: `{"vms": [VM, ...]}`.

### POST /vms

Make a VM. Takes about 10 seconds. Every ssh key on your account can log in to it.

```
curl -X POST https://getyurt.dev/api/v1/vms -H "Authorization: Bearer $YURT_API_KEY" \
  -H "Content-Type: application/json" -d '{"name": "agent-1", "idle_minutes": 30}'
```

- `name`: lowercase letters, digits and `-`, up to 24.
- `idle_minutes` (optional): sleep after this many idle minutes, or `"off"`. Default 15.

Returns the VM.

### GET /vms/{name}

One VM.

### PATCH /vms/{name}

Change idle sleep: `{"idle_minutes": 60}` or `{"idle_minutes": "off"}`. Returns the VM.

### POST /vms/{name}/sleep

Put it to sleep now. Memory and running programs are kept. Returns the VM.

### POST /vms/{name}/wake

Wake it, or boot it if it was stopped. Takes about a second. Returns the VM.

### POST /vms/{name}/exec

Run a shell command as root and wait for it. Wakes the VM if it sleeps.

```
curl -X POST https://getyurt.dev/api/v1/vms/agent-1/exec -H "Authorization: Bearer $YURT_API_KEY" \
  -H "Content-Type: application/json" -d '{"command": "cd /root/app && npm test", "timeout": 300}'
```

```
{"exit_code": 0, "stdout": "...", "stderr": ""}
```

- `timeout`: seconds, 1 to 600, default 60. Past it the command is stopped and you get `504`.
- Output is cut to the last 1 MB of each stream.
- For long jobs, start them in the background (`nohup ... &` or `tmux new -d`) and check on them with later calls.

### DELETE /vms/{name}

Delete the VM and everything on it. This can't be undone. Returns `{"deleted": "agent-1"}`.

## Webhooks

Add an endpoint in the dashboard under **Webhooks**. Yurt then POSTs JSON to it when something happens to your VMs.

Events:

- `vm.created`: a VM was made.
- `vm.awake`: a VM started running (after create, wake, or a login that woke it).
- `vm.asleep`: a VM stopped running (idle sleep, a sleep call, or out of credits).
- `vm.deleted`: a VM was deleted.
- `credits.empty`: your credits ran out and a VM was put to sleep.

`vm.awake` and `vm.asleep` arrive up to a minute after the change.

```
{"id": "evt_3f9a1c2b7d4e5f60", "type": "vm.asleep", "created": 1796220400,
 "data": {"account": "u3", "vm": "agent-1", "state": "sleeping"}}
```

Rules:

- The URL must be `https://` on port 443 and resolve to a public address. Redirects are not followed.
- Answer with any 2xx within 10 seconds. Otherwise Yurt tries again after 30 seconds, 5 minutes and 30 minutes.
- A delivery can arrive twice or out of order. Use `id` to skip duplicates.
- **Send test** in the dashboard sends a `ping` event.

### Verify the signature

Each request has `webhook-id`, `webhook-timestamp` and `webhook-signature` headers (the Standard Webhooks format). The signature is `v1,` plus the base64 HMAC-SHA256 of `id.timestamp.body`, keyed with your secret (after `whsec_`, base64-decoded).

```
import base64, hashlib, hmac, time

def verify(secret, headers, body):
    key = base64.b64decode(secret.removeprefix("whsec_"))
    msg = f"{headers['webhook-id']}.{headers['webhook-timestamp']}.".encode() + body
    good = base64.b64encode(hmac.new(key, msg, hashlib.sha256).digest()).decode()
    fresh = abs(time.time() - int(headers["webhook-timestamp"])) < 300
    return fresh and any(hmac.compare_digest(s.partition(",")[2], good)
                         for s in headers["webhook-signature"].split())
```

Libraries from standardwebhooks.com work too.

## Limits

- 3 VMs per account.
- 10 API keys and 5 webhook endpoints per account.
- `exec`: 10 minutes per call.
