Base URL: https://your-statekeep-instance.com
Auth header: x-api-key: sk_<keyId>_<secret>
Content-Type: application/json for all POST/PUT/PATCH requests
- Health & Monitoring
- Authentication
- API Key Management
- Definitions
- Actors
- Scheduled Events
- Webhooks
- Admin
- Organizations (Admin)
- Archives & Export
- Priority & Headers
- Error Reference
No authentication required. Returns server status, APV engine mode, and uptime.
Response 200:
{
"status": "ok",
"engine": "wasm",
"db": "ok",
"uptime": 3612,
"timestamp": "2025-05-19T10:00:00.000Z"
}| Field | Values | Meaning |
|---|---|---|
status |
ok / degraded |
degraded if DB is unreachable |
engine |
wasm |
APV WASM engine loaded; server fails to start if apv-engine.mjs is missing |
db |
ok / error |
SQLite reachability |
uptime |
integer (seconds) | Server process uptime |
No authentication required. Reports heartbeat status for all background workers.
Response 200 (all healthy) / 503 (any worker stale):
{
"healthy": true,
"checkedAt": 1716163200000,
"staleThresholdMs": 120000,
"workers": [
{
"workerId": "migrate-worker-abc123",
"workerType": "migrate-worker",
"pid": 12345,
"startedAt": 1716160000000,
"lastBeat": 1716163190000,
"staleSecs": 10,
"healthy": true
}
]
}A worker is considered stale if its last heartbeat is older than 2 minutes.
No authentication required. Returns real-time stats for the worker thread priority queue.
Response 200:
{
"workerCount": 20,
"totals": {
"queued": { "high": 0, "normal": 3, "low": 12 },
"served": { "high": 1240, "normal": 8900, "low": 450 },
"avgWaitMs": { "high": 2, "normal": 8, "low": 34 }
},
"perWorker": [
{
"slotIndex": 0,
"queued": { "high": 0, "normal": 1, "low": 0 },
"served": { "high": 62, "normal": 445, "low": 22 },
"avgWaitMs": { "high": 1, "normal": 9, "low": 30 }
}
]
}Three priority tiers: high (dashboard requests), normal (API calls), low (background migration). Served in weighted round-robin: 3 high → 2 normal → 1 low per round. Within each tier, requests are served in per-org round-robin so no single org can starve another.
Returns Prometheus text-format metrics. No authentication required.
# HELP statekeep_actors_active Total active actors
# TYPE statekeep_actors_active gauge
statekeep_actors_active 1200
# HELP statekeep_actors_needs_rescue Actors in needs_rescue status
# TYPE statekeep_actors_needs_rescue gauge
statekeep_actors_needs_rescue 0
# HELP statekeep_migration_jobs_pending Pending migration jobs
# TYPE statekeep_migration_jobs_pending gauge
statekeep_migration_jobs_pending 3
# HELP statekeep_wal_size_bytes SQLite WAL file size in bytes
# TYPE statekeep_wal_size_bytes gauge
statekeep_wal_size_bytes 2097152
# HELP statekeep_api_latency_p99_ms API latency P99 in milliseconds
# TYPE statekeep_api_latency_p99_ms gauge
statekeep_api_latency_p99_ms 12
When STATEKEEP_API_KEY is set, all API requests (except GET /v1/health) require an x-api-key header with that value — missing key → 401, wrong key → 403. WebSocket streams may pass it as ?api_key= instead. When it is not set, the server is unauthenticated and binds to 127.0.0.1 only (override with STATEKEEP_ALLOW_UNAUTHENTICATED=true).
x-api-key: sk_live_...Admin-only routes (/v1/admin/*, /v1/internal/*) require x-admin-key with STATEKEEP_ADMIN_KEY. If no admin key is configured they are reachable from loopback only.
The dashboard asks for the API key on first use and keeps it for the browser tab.
Deploy a new machine definition or update an existing one. Idempotent: re-deploying the same id with the same definition JSON is a no-op (returns idempotent: true).
Request body:
{
"id": "loan-v2",
"definition": { "initial": "idle", "states": { ... } },
"parentId": "loan-v1",
"historyPath": ["APPLY", "PAY_FEE"],
"stateMapping": { "in_review": "under_review" },
"contextTransform": {
"newPath.field": "oldPath.field"
},
"confirmToken": "a3f1b2c4-...",
"refinement": 1
}| Field | Type | Required | Description |
|---|---|---|---|
id |
string | Yes | Unique version identifier |
definition |
object | Yes | XState v5 machine config (JSON only — no functions) |
parentId |
string | No | Previous version to migrate actors from |
historyPath |
string[] | No | Ordered event sequence starting from the actor's first event (a prefix, not a substring). Actors currently at exactly this point migrate on deploy; actors that reach it later migrate on their next event. Actors already past it are not moved retroactively. Omit or [] for wildcard (all actors) |
stateMapping |
object | No | { oldStateName: newStateName } — remap actors in removed/renamed states |
contextTransform |
object | No | { "new.path": "old.path" } — dot-notation copy applied to actor context during migration. The old field is kept (additive, not destructive). Paths containing __proto__, constructor or prototype are rejected (400). |
confirmToken |
string | No | UUID from a prior requires_confirmation response |
refinement |
integer | No | APV refinement level (default: 1) |
Response 201 (deployed):
{
"id": "loan-v2",
"parentId": "loan-v1",
"deployedAt": 1716163200000,
"affectedActors": 120,
"strandedTagged": 0,
"engineAvailable": true,
"idempotent": false,
"warnings": [
{ "type": "DEAD_END_STATE", "severity": "warn", "message": "State 'review' has no outgoing transitions" }
]
}Response 200 (requires confirmation — definition NOT written yet):
{
"status": "requires_confirmation",
"strandedActors": [
{ "currentState": "in_progress", "count": 8 }
],
"safeActors": 34,
"confirmToken": "a3f1b2c4-...",
"expiresIn": 300,
"message": "8 actors are in states removed by this version..."
}Re-submit with confirmToken to tag stranded actors needs_rescue and proceed with deployment.
Validation errors (400):
{
"error": "Definition validation failed",
"errors": [
{ "type": "INVALID_TRANSITION", "severity": "error", "message": "State 'idle' transitions to undefined state 'typo'" }
]
}Hard error types: EMPTY_STATES, INVALID_INITIAL, INVALID_TRANSITION, COMPOUND_NO_INITIAL, XSTATE_ERROR, UNDEFINED_INITIAL
Soft warnings (stored, non-blocking): DEAD_END_STATE, UNREACHABLE_STATE, NO_TERMINAL_STATE
Validate a definition without deploying or writing anything to the database.
Request:
{ "definition": { "initial": "idle", "states": { ... } } }Response 200 (valid):
{ "valid": true, "warnings": [] }Response 400 (invalid):
{
"valid": false,
"errors": [
{ "type": "INVALID_INITIAL", "severity": "error", "message": "..." }
]
}Dry-run a deployment: compute which actors would migrate, which would be stranded, and what stateMapping would be needed. Nothing is written.
Request: Same body as PUT /v1/definitions (without confirmToken)
Response 200:
{
"wouldMigrate": 45,
"wouldStrand": 8,
"strandedStates": ["in_progress"],
"requiresConfirmation": true
}Run event sequences against a definition without spawning persistent actors. Used for testing machine logic before deployment.
Request:
{
"definition": { "initial": "idle", "states": { ... } },
"scenarios": [
{
"name": "happy path",
"initialContext": { "userId": "u_001" },
"events": ["START", "SUBMIT", "APPROVE"],
"expectedStates": ["idle", "running", "submitted", "approved"],
"expectDone": true
}
]
}Events can be plain strings ("START") or objects ({ "type": "START", "payload": {} }).
expectDone: true fails if the machine is not in a final state after all events. expectDone: false fails if the machine unexpectedly reaches a final state.
Response 200:
{
"results": [
{
"name": "happy path",
"passed": true,
"states": ["idle", "running", "submitted", "approved"],
"finalState": "approved",
"done": true
}
]
}List all definitions. Returns latest version of each machine family by default.
Query params: ?limit=50&offset=0
Response 200:
{
"definitions": [
{
"id": "loan-v2",
"machineId": "loan",
"parentId": "loan-v1",
"deployedAt": 1716163200000,
"actorCount": 120
}
]
}Get a single definition including its machine JSON.
Response 200:
{
"id": "loan-v2",
"machineId": "loan",
"parentId": "loan-v1",
"definition": { "initial": "idle", "states": { ... } },
"deployedAt": 1716163200000
}Get deployment status and migration progress for a definition version.
Response 200:
{
"id": "loan-v2",
"totalJobs": 120,
"completed": 115,
"pending": 5,
"failed": 0,
"percentDone": 95.8
}Show which states are added, removed, or unchanged compared to the parent definition.
Response 200:
{
"added": ["compliance_review"],
"removed": ["in_review"],
"unchanged": ["idle", "approved", "rejected"]
}Aggregate actor counts by current state for this definition version.
Response 200:
{
"id": "loan-v2",
"byState": {
"idle": 42,
"underwriting": 35,
"compliance_review": 18,
"approved": 25
}
}Aggregate actor counts across all versions of a machine family.
Response 200:
{
"machineId": "loan",
"totalActors": 220,
"byVersion": {
"loan-v1": 100,
"loan-v2": 120
}
}Spawn a new actor instance.
Request:
{
"definitionId": "loan-v2",
"initialContext": {
"applicantId": "usr-001",
"amount": 50000
}
}Machine alias (recommended pattern): Pass the machineId (the root definition's id) instead of a specific version. The server resolves it to the latest non-deprecated version in that family automatically. This means client code never needs to track which version is "current" — new deployments are picked up on the next spawn without any client change.
{ "definitionId": "loan" }This works because every definition has a machineId — the ID of the first definition in its family, inherited through the parentId chain. Passing that root ID triggers the alias resolution.
Response 201:
{
"id": "actor_7f3a1b2c...",
"definitionId": "loan-v3",
"requestedAs": "loan",
"stateValue": "idle",
"context": { "applicantId": "usr-001", "amount": 50000 },
"done": false
}definitionId may be:
- an exact version id (e.g.
loan-v2) — used as-is, except the family's root version id (e.g.loan-v1), which acts as the family alias and resolves to the newest active version; - the machine name, i.e. the
idinside the definition JSON (e.g.loan) — also resolves to the newest active version (409 if two families share the name).
requestedAs is present whenever the id was resolved to a different version.
Retrying safely: pass "idempotencyKey": "<your id>" (or an Idempotency-Key header). If the
answer was lost (a crash, a timeout, a dropped connection) and the client sends the same
spawn again, it gets 200 with the actor the first attempt created ("duplicate": true)
instead of a second actor. The actor, its first entry-action jobs and timers, its SPAWN
history event and the key are written in one transaction, so a crash never leaves half of
them. A database that is briefly unreachable answers 503 (retry with the same key).
An actor spawned with the family alias is dated by the version it got, so every later deployment applies to it. An actor pinned to an exact older version after a newer one was deployed is, by design, not moved by that deployment.
Spawn up to 500 actors in a single request. Processed in batches of 50 internally.
Request:
{
"actors": [
{ "definitionId": "loan-v2", "initialContext": { "applicantId": "u_001" } },
{ "definitionId": "loan-v2", "initialContext": { "applicantId": "u_002" } }
]
}The machine alias works here too: pass the machineId root ID in any item's definitionId and the server resolves it to the latest version. All unique definition IDs in the batch are resolved in a single pre-pass (not N per-actor DB lookups).
Response 207 (Multi-Status):
{
"created": [
{
"id": "actor_...",
"definitionId": "loan-v3",
"requestedAs": "loan",
"stateValue": "idle"
}
],
"failed": [],
"total": 2
}requestedAs appears in each created item when the provided ID was resolved to a different version. Items in failed contain { index, definitionId, error }.
List actors with optional filters. Cursor-paginated.
Query params:
| Param | Type | Description |
|---|---|---|
status |
string | Filter by status: active, migrating, needs_rescue, terminated, archived |
definitionId |
string | Filter to a specific definition version |
limit |
integer | Page size (default 50, max 200) |
after |
string | Cursor from previous page's nextCursor |
Response 200:
{
"actors": [
{
"id": "actor_7f3a1b2c...",
"definitionId": "loan-v2",
"stateValue": "underwriting",
"status": "active",
"createdAt": 1716163200000,
"updatedAt": 1716163260000
}
],
"nextCursor": "actor_7f3a...",
"hasMore": true
}Convenience alias for GET /v1/actors?status=needs_rescue. Returns actors that are stranded and need manual intervention.
Get full state for a single actor including decrypted context.
Response 200:
{
"id": "actor_7f3a1b2c...",
"definitionId": "loan-v2",
"stateValue": "underwriting",
"status": "active",
"context": { "applicantId": "usr-001", "amount": 50000 },
"historyFingerprint": "2:79cff0d92c527c446ce6978ad96cec1dafe8b38bd6902bbce1ea27caca22539c",
"createdAt": 1716163200000,
"updatedAt": 1716163260000,
"done": false
}Send an event to an actor, advancing it to the next state.
Headers:
X-Priority: high— optional; routes this request to the high-priority queue (used internally by the dashboard). Default:normal.
Request:
{
"type": "APPROVE",
"payload": { "approvedBy": "officer_42" },
"idempotencyKey": "evt_req_abc123"
}| Field | Type | Required | Description |
|---|---|---|---|
type |
string | Yes | Event type (must match a transition in the definition) |
payload |
object | No | Merged into actor context |
idempotencyKey |
string | No | If provided, duplicate requests with the same key return the current state instead of re-processing. Format: [a-zA-Z0-9_\-.:], max 128 chars |
Response 200:
{
"actorId": "actor_7f3a1b2c...",
"stateValue": "compliance_review",
"context": { "applicantId": "usr-001", "approvedBy": "officer_42" },
"done": false,
"migratedTo": null
}migratedTo is non-null if this event also triggered a migration to a new definition version.
When idempotencyKey was already processed, the response includes "idempotent": true.
Error responses:
| Code | code field |
Meaning |
|---|---|---|
| 404 | — | Actor not found |
| 409 | ACTOR_NEEDS_RESCUE |
Actor is stranded — events blocked until rescued |
| 400 | — | Other validation error |
Cursor-paginated event history for an actor.
Query params: ?limit=50&after=0
after is an event row ID (integer). Use the last event's id as cursor for the next page.
Response 200:
{
"actorId": "actor_7f3a1b2c...",
"events": [
{
"id": 1,
"type": "SPAWN",
"payload": null,
"tick": 42,
"processedAt": 1716163200000
},
{
"id": 2,
"type": "APPROVE",
"payload": { "approvedBy": "officer_42" },
"tick": 45,
"processedAt": 1716163260000
}
],
"nextCursor": 2,
"hasMore": false
}Migration routing decisions for an actor — the full audit trail of which version each migration attempted to route the actor to and why.
Response 200:
{
"actorId": "actor_7f3a1b2c...",
"decisions": [
{
"evaluatedAt": 1716163200000,
"decision": "migrated",
"reason": "prefix_match",
"fromDefinitionId": "loan-v1",
"toDefinitionId": "loan-v2",
"prefixHash": "a3f1b2c4..."
}
]
}Export a complete actor record — state, context, and full event history — as a single JSON document.
Response 200: Full actor JSON including all events.
Terminate an actor. The actor's state is preserved in the database but no further events can be sent.
Headers: X-Priority: high — optional, same semantics as event endpoint.
Response 200:
{
"actorId": "actor_7f3a1b2c...",
"stateValue": "approved",
"status": "terminated"
}Update actor metadata. Currently supports resetting needs_rescue actors back to active.
Request:
{ "status": "active" }Response 200:
{ "id": "actor_7f3a1b2c...", "status": "active" }Schedule a future event to be sent to an actor.
Request:
{
"type": "SEND_REMINDER",
"payload": { "channel": "email" },
"fireAt": 1716250000000
}fireAt is a Unix timestamp in milliseconds. The scheduler worker checks for due events every 5 seconds.
Response 201:
{
"scheduleId": "sched_abc123",
"actorId": "actor_7f3a1b2c...",
"eventType": "SEND_REMINDER",
"fireAt": 1716250000000,
"status": "pending"
}List all scheduled events for an actor.
Response 200:
{
"scheduled": [
{
"scheduleId": "sched_abc123",
"eventType": "SEND_REMINDER",
"fireAt": 1716250000000,
"status": "pending",
"createdAt": 1716163200000
}
]
}Status values: pending, fired, failed, cancelled
Cancel a pending scheduled event.
Response 204 (no body), or 404 if not found / already fired.
List all pending scheduled events across all orgs. Requires X-Admin-Key header.
Response 200:
{
"scheduled": [
{
"scheduleId": "sched_abc123",
"actorId": "actor_7f3a1b2c...",
"orgId": "org_xyz",
"eventType": "SEND_REMINDER",
"fireAt": 1716250000000,
"status": "pending"
}
]
}List scheduled events that have exhausted all retry attempts. Requires X-Admin-Key header.
Register a webhook endpoint. The URL must be HTTPS.
Request:
{
"url": "https://yourapp.com/webhooks/statekeep",
"secret": "at-least-16-chars-random-secret",
"events": ["state.changed", "actor.migrated"]
}Valid event types:
state.changed— actor transitioned to a new stateactor.migrated— actor migrated to a new definition versionactor.terminated— actor was terminatedactor.needs_rescue— actor became stranded (consented: whether a deployment preview listed it)scheduled.fired— a scheduled event was deliveredscheduled.failed— a scheduled event failed all retries
Response 201:
{
"id": "wh_abc123",
"url": "https://yourapp.com/webhooks/statekeep",
"events": ["state.changed", "actor.migrated"],
"active": true,
"createdAt": 1716163200000
}The secret is stored encrypted and never returned after creation.
Signature verification:
Every webhook delivery includes a X-StateKeep-Signature: sha256=<hex> header. Verify it with HMAC-SHA256:
const expected = 'sha256=' + crypto
.createHmac('sha256', YOUR_SECRET)
.update(rawBody) // raw request body bytes
.digest('hex');
const valid = crypto.timingSafeEqual(
Buffer.from(expected),
Buffer.from(request.headers['x-statekeep-signature'])
);Webhook payload envelope:
{
"event": "state.changed",
"actorId": "actor_7f3a1b2c...",
"orgId": "org_xyz",
"ts": 1716163260000,
"data": {
"stateValue": "approved",
"context": { "applicantId": "usr-001" }
}
}List all webhooks for the authenticated org. Secrets are never included.
Response 200:
{
"webhooks": [
{
"id": "wh_abc123",
"url": "https://yourapp.com/webhooks/statekeep",
"events": ["state.changed"],
"active": true,
"createdAt": 1716163200000
}
]
}Get a single webhook.
Response 200: Single webhook object (same shape as list item).
Update a webhook — change URL, events list, or activate/deactivate.
Request:
{
"url": "https://newurl.com/hook",
"events": ["state.changed", "actor.terminated"],
"active": false
}All fields are optional. Response 200: Updated webhook object.
Deactivate a webhook. The record is kept for audit purposes (active: false).
Response 204 (no body)
Queue a test delivery to verify the endpoint is reachable.
Response 200:
{ "deliveryId": "del_abc123", "status": "queued" }List recent delivery attempts for a webhook.
Response 200:
{
"deliveries": [
{
"deliveryId": "del_abc123",
"event": "state.changed",
"status": "delivered",
"responseCode": 200,
"attemptedAt": 1716163260000,
"attempts": 1
}
]
}Admin endpoints require the X-Admin-Key header (the STATEKEEP_ADMIN_KEY environment variable value). Do not expose admin keys to API clients.
Force-restart a worker pool. type must be a registered worker type.
Response 200:
{ "restarted": true, "workerType": "migrate-worker" }Force an actor into archived status immediately, bypassing the GC retention window.
Response 200:
{ "actorId": "actor_7f3a1b2c...", "status": "archived" }Delete a stale worker heartbeat record. Use when a worker shows unhealthy but the process has since restarted.
Response 204 (deleted) / 404 (not found)
List archived actor metadata (actors GC'd by the gc-worker).
Query params: ?limit=50&after=0
Response 200:
{
"archives": [
{
"actorId": "actor_7f3a1b2c...",
"definitionId": "loan-v2",
"archivedAt": 1716163200000,
"archivePath": "/opt/statekeep/archives/actor_7f3a.json.gz"
}
]
}Export all actors for a machine family as JSON or CSV.
Query params: ?format=json or ?format=csv
Response 200: JSON array or CSV text depending on format.
Restore an archived actor back to active status.
Response 200:
{ "actorId": "actor_7f3a1b2c...", "status": "active", "restored": true }The POST /v1/actors/:id/event and DELETE /v1/actors/:id endpoints accept an optional X-Priority header that controls queue placement in the worker thread pool.
| Value | Queue | Used by |
|---|---|---|
urgent |
Urgent queue (burst mode) | Dashboard manual actions (send event, get state, terminate) |
high |
High-priority queue (3/round) | Dashboard background polling |
normal |
Normal queue (2/round) | Default for all API calls |
| (absent) | Normal queue | Same as normal |
Background workers (migrate-worker) use low priority internally (not client-settable).
Round-robin schedule: 3 high → 2 normal → 1 low per round, then repeat. urgent requests get exclusive access for the first 5 seconds of continuous urgent load, then 1 urgent slot per round interleaved with the normal schedule. Within each tier, requests are served in per-org round-robin so one org's burst cannot starve another.
You generally do not need to set X-Priority — the default normal is appropriate for all application-tier API calls. Use high only if you are building a dashboard-like UI that requires lower latency for read-after-write consistency.
All error responses follow this shape:
{
"error": "Human-readable message",
"code": "MACHINE_READABLE_CODE"
}code is only present when there is a specific machine-readable error type.
| Code | Meaning |
|---|---|
| 200 | Success (or requires_confirmation for definitions) |
| 201 | Created |
| 204 | Deleted (no body) |
| 207 | Multi-Status (bulk operations — check failed array) |
| 400 | Validation error — fix request body |
| 401 | Missing or invalid API key |
| 403 | Insufficient tier for this endpoint |
| 404 | Resource not found (or org isolation — cross-org resources always 404, never 403) |
| 409 | Conflict — ACTOR_NEEDS_RESCUE or actor is terminated/archived |
| 503 | Workers unhealthy (only on GET /v1/health/workers) |
| Code | Endpoint | Meaning |
|---|---|---|
ACTOR_NEEDS_RESCUE |
POST /v1/actors/:id/event |
Actor is stranded; rescue it first via PATCH /v1/actors/:id |
EMPTY_STATES |
PUT /v1/definitions |
Definition has no states |
INVALID_INITIAL |
PUT /v1/definitions |
initial references a nonexistent state |
INVALID_TRANSITION |
PUT /v1/definitions |
A transition targets a nonexistent state |
COMPOUND_NO_INITIAL |
PUT /v1/definitions |
Compound state has children but no initial |
XSTATE_ERROR |
PUT /v1/definitions |
XState threw during machine construction |
UNDEFINED_INITIAL |
PUT /v1/definitions |
Machine accepted but started with value = undefined |
STATE_NOT_MAPPABLE |
Internal (HYDRATE) | Migration aborted — actor's state has no mapping in new version |
CONTEXT_TRANSFORM_FAILED |
Internal (HYDRATE) | contextTransform failed — migration aborted |
When PUT /v1/definitions returns HTTP 200 (not 201) with status: "requires_confirmation":
- Save the
confirmToken(expires in 5 minutes by default) - Review
strandedActors— if any are unexpected, addstateMappingto reroute them - Re-submit the same request body with
"confirmToken": "<token>"added - The definition is written, stranded actors are tagged
needs_rescue
The confirming PUT predicts the stranding again, under the workflow's deploy lock, immediately before the commit. You get a fresh preview (with a new confirmToken) instead of a deployment when the token is expired or already used, the definition changed, or any actor that would now be stranded was not listed in the preview (notInPreview gives how many). Fewer stranded actors than previewed is fine. Tokens are stored in the database: a preview from one server can be confirmed through any other, and survives restarts.
An actor whose state changes between that final check and the commit can still be stranded without having been in the preview. Every stranding emits the actor.needs_rescue webhook with consented: true|false, so these are reported, never silent:
{ "actorId": "…", "fromDefinitionId": "order-v1", "toDefinitionId": "order-v2",
"reason": "STATE_NOT_FOUND", "stateValue": "shipped", "consented": false }