# Quickstart

Get from an API key to your first CapVM machine with curl: check your balance, create a machine, run a command, then stop it. [Sign up](signup.md) to get a key.

## 1. Get an agent key

The CapVM team issues API keys after your organization signs up and a human accepts the Terms. Keys aren't self-serve; self-serve keys in the [dashboard](dashboard.md) are Coming soon. Every key has:

- a **spend cap** (the most it can ever spend, in micro-USD)
- a **max concurrent machines** count
- an **allow-list of SKUs** (`arm-small`, `gpu-l4`)

> **Terms first.** Before CapVM can issue a key, add credits or start a machine for your organization, a human authorised to bind your business must accept the current Terms of Service and Acceptable Use Policy (Terms §3.1). An agent can start the signup with `POST /v1/signup`, but can't accept. See [Signup and Terms acceptance](signup.md). Without a current acceptance, those calls return `403 terms_not_accepted`.

Keys look like `ak_test_…` or `ak_live_…`. Use `ak_test_` while you build and `ak_live_` in production. The secret is shown **once**, and CapVM stores only its SHA-256 hash.

```bash
export CAPVM_KEY=ak_test_...
export CAPVM=https://api.capvm.com/v1
```

## 2. Check your balance

```bash
curl $CAPVM/balance -H "Authorization: Bearer $CAPVM_KEY"
# {"available_micro":10000000,"held_micro":0,"currency":"USD","unit":"micro_usd"}
```

Money is always an integer number of micro-USD (1 USD = 1,000,000).

Not enough credits? For now, CapVM adds credits to your account. Card top-ups are Coming soon: your agent will be able to start one with `POST /v1/checkout/sessions` (an `Idempotency-Key` header and a body like `{"amount_usd":"10.00"}`, $10 to $1000), and hand the returned Stripe `url` to a human to pay. The amount format may change. See [Billing → Topping up by card](billing-and-caps.md#topping-up-by-card).

## 3. Create a machine

`ttl_seconds` is required (60 to 7200). `Idempotency-Key` is also required: if you retry with the same key and body, you get the same machine back, never a second one.

```bash
curl $CAPVM/machines \
  -H "Authorization: Bearer $CAPVM_KEY" \
  -H "Idempotency-Key: job-42" \
  -H "Content-Type: application/json" \
  -d '{"sku":"arm-small","ttl_seconds":900}'
```

`201 Created`. The full TTL is held up front: 900 s × $0.20/h = 50,000 micro-USD ($0.05).

```json
{
  "id": "m_9b1c0e6f2d8a4c3b9e7f1a2b3c4d5e6f",
  "sku": "arm-small",
  "state": "provisioning",
  "instance_id": "i-0abc…",
  "public_ip": null,
  "region": "eu-central-2",
  "ttl_seconds": 900,
  "price_per_hour_micro": 200000,
  "hold_micro": 50000,
  "charge_micro": null,
  "created_at": "2026-10-25T11:00:00Z",
  "expires_at": "2026-10-25T11:15:00Z",
  "ready_at": null,
  "stop_requested_at": null,
  "terminated_at": null
}
```

If the credits or the key's cap can't cover the hold, you get `402` (`insufficient_credits` or `spend_cap_exceeded`) and nothing launches.

## 4. Wait for `ready`

```bash
curl $CAPVM/machines/$ID -H "Authorization: Bearer $CAPVM_KEY"
```

States: `requested` → `provisioning` → `ready` → `stopping` → `terminated`.

## 5. Run a command

Wait until the machine is `ready`. The command agent on the machine can take about a minute longer than the machine itself; if it isn't accepting commands yet, you get `409 machine_not_ready` ("Machine is running but is not accepting commands yet."). Retry.

```bash
curl $CAPVM/machines/$ID/exec \
  -H "Authorization: Bearer $CAPVM_KEY" -H "Content-Type: application/json" \
  -d '{"command":"uname -a","timeout_seconds":30,"working_directory":"/tmp"}'
# {"stdout":"…","stderr":"","exit_code":0,"truncated":false}
```

- `command` is required (1 to 8192 characters, no NUL). `timeout_seconds` is optional (1 to 300, default 30). `working_directory` is optional: an absolute path; leave it out for the machine's default.
- A non-zero exit is a normal `200` result: `{"command":"exit 7"}` returns `exit_code` 7.
- `stdout` and `stderr` are each capped at 256 KiB. `truncated` is `true` if either was cut.
- A command that runs past `timeout_seconds` returns `408 command_timeout`.

All fields and errors: [API reference → exec](api-reference.md#post-v1machinesidexec-run-a-command).

## 6. Check spend, then stop

```bash
curl $CAPVM/usage -H "Authorization: Bearer $CAPVM_KEY"
curl -X DELETE $CAPVM/machines/$ID -H "Authorization: Bearer $CAPVM_KEY"
```

`DELETE` stops the machine, charges the billed seconds (at least 60) and releases the rest of the hold. If you forget, CapVM stops the machine at `expires_at` anyway.
