Quickstart
What you’ll build
You will create a run withPOST /v1/runs, subscribe to progress with Server-Sent Events (SSE) using an authenticated connection, detect when the run reaches a terminal state, and confirm the final status with GET /v1/runs/{id}. For the full run state machine, event catalog, and awaiting_input patterns, see the linked guides below.
Prerequisites
- Node.js 20+ and npm
- An API key in the form
key_id:secret— see Authentication for creating keys withPOST /v1/api-keys, or use a key provisioned during account setup - SSE with auth headers: install the helper library (native
EventSourcecannot sendAuthorization):
Step 1: Configure credentials
Read the API base URL and key from the environment. Usehttp://localhost:3000 (or your dev URL) for local development.
Bearer <key_id>:<secret> (the raw key_id:secret after Bearer ).
Step 2: Create a run
POST /v1/runs accepts JSON and requires an idempotency-key header for safe retries.
{ id, status: "queued", …, request_id }. Extract run.id for subsequent calls.
Step 3: Stream events via SSE
Subscribe toGET /v1/runs/{id}/events/stream. The server emits SSE events named run_event; each message’s data is JSON matching the public envelope (no database id on the wire):
fetchEventSource from @microsoft/fetch-event-source so you can set Authorization.
step.progress, when payload.value.kind === "content_delta", write payload.value.content_delta to stdout (or your UI buffer).
step.done outcomes: payload.value.outcome is one of succeeded, retry_step, or fail_run (not a generic failed label).
Human-in-the-loop: if you receive run.awaiting_input, resume the run with POST /v1/runs/{id}/signal and an action of approve, reject, or submit_input — see Run lifecycle and Authentication.
Step 4: Check the result
After the stream ends for a terminal event, fetch the run record:Step 5: Handle errors
| Situation | What to do |
|---|---|
401 with AUTH_KEY_INVALID or AUTH_MISSING_HEADER | Confirm Authorization: Bearer <key_id>:<secret> and that the key is active |
404 on GET /v1/runs/{id} | Wrong run id, or run belongs to another customer scope |
409 on POST /v1/runs with duplicate idempotency-key | Same key + same body returns the existing run; response may include replayed: true — treat as success for idempotent clients |
run.worker.failed event | Inspect payload.value.reason_code for the failure cause |
error, reason_code, and request_id.
Next steps
- Authentication — key lifecycle (rotate, revoke)
- Run lifecycle — full state machine and
awaiting_input - Events reference — every event type and payload
- Usage & metering — token consumption and
GET /v1/usage - Reference recipe — end-to-end TypeScript sample client