Skip to main content

Instance Commands

Commands for managing state machine instances.

CREATE_INSTANCE

Creates a new instance of a state machine.

Request

{
"op": "CREATE_INSTANCE",
"params": {
"machine": "order",
"version": 1,
"id": "order-001",
"context": {
"customer": "alice",
"total": 99.99
},
"idempotency_key": "create-order-001"
}
}
ParameterTypeRequiredDescription
machinestringYesMachine name
versionintegerYesMachine version
idstringYesUnique instance ID
contextobjectNoInitial context (default: )
idempotency_keystringNoDeduplication key

Response

{
"status": "ok",
"result": {
"id": "order-001",
"machine": "order",
"version": 1,
"state": "pending",
"context": {
"customer": "alice",
"total": 99.99
},
"created": true,
"created_at": "2024-01-15T10:30:00Z"
}
}
FieldDescription
createdTrue if newly created, false if idempotent duplicate
stateInitial state from machine definition

Errors

CodeDescription
MACHINE_NOT_FOUNDMachine or version doesn't exist
INSTANCE_EXISTSInstance ID already in use

GET_INSTANCE

Retrieves an instance's current state and context.

Request

{
"op": "GET_INSTANCE",
"params": {
"id": "order-001"
}
}
ParameterTypeRequiredDescription
idstringYesInstance ID

Response

{
"status": "ok",
"result": {
"id": "order-001",
"machine": "order",
"version": 1,
"state": "paid",
"context": {
"customer": "alice",
"total": 99.99,
"payment_id": "pay-123"
},
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-01-15T11:00:00Z",
"wal_offset": 12345
}
}
FieldDescription
stateCurrent state
contextCurrent context
wal_offsetWAL position of last update

Errors

CodeDescription
INSTANCE_NOT_FOUNDInstance doesn't exist

LIST_INSTANCES

Lists instances with optional filtering.

Request

{
"op": "LIST_INSTANCES",
"params": {
"machine": "order",
"version": 1,
"state": "pending",
"limit": 100,
"offset": 0
}
}
ParameterTypeRequiredDescription
machinestringNoFilter by machine name
versionintegerNoFilter by machine version
statestringNoFilter by current state
limitintegerNoMax items (default: 100)
offsetintegerNoSkip items (default: 0)

Response

{
"status": "ok",
"result": {
"instances": [
{
"id": "order-001",
"machine": "order",
"version": 1,
"state": "pending",
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-01-15T10:30:00Z"
},
{
"id": "order-002",
"machine": "order",
"version": 1,
"state": "pending",
"created_at": "2024-01-15T10:35:00Z",
"updated_at": "2024-01-15T10:35:00Z"
}
],
"total": 50,
"has_more": true
}
}

Note: List results include summary info only. Use GET_INSTANCE for full context.


DELETE_INSTANCE

Soft-deletes an instance.

Request

{
"op": "DELETE_INSTANCE",
"params": {
"id": "order-001"
}
}
ParameterTypeRequiredDescription
idstringYesInstance ID

Response

{
"status": "ok",
"result": {
"deleted": true
}
}

Behavior

  • Instance is marked as deleted
  • Excluded from LIST_INSTANCES by default
  • GET_INSTANCE returns INSTANCE_NOT_FOUND
  • Events cannot be applied to deleted instances
  • Physically removed during compaction

Errors

CodeDescription
INSTANCE_NOT_FOUNDInstance doesn't exist

Examples

Create with Rich Context

{
"op": "CREATE_INSTANCE",
"params": {
"machine": "order",
"version": 1,
"id": "order-2024-001",
"context": {
"customer": {
"id": "cust-123",
"name": "Alice Smith",
"tier": "gold"
},
"items": [
{"sku": "ITEM-001", "qty": 2, "price": 29.99},
{"sku": "ITEM-002", "qty": 1, "price": 49.99}
],
"shipping": {
"method": "express",
"address": "123 Main St"
},
"total": 109.97
}
}
}

List with Filters

// All pending orders
{
"op": "LIST_INSTANCES",
"params": {
"machine": "order",
"state": "pending"
}
}

// First 10 instances
{
"op": "LIST_INSTANCES",
"params": {
"limit": 10
}
}

// Page 2 of results
{
"op": "LIST_INSTANCES",
"params": {
"limit": 100,
"offset": 100
}
}

Idempotent Creation

// First request - creates instance
{
"op": "CREATE_INSTANCE",
"params": {
"machine": "order",
"version": 1,
"id": "order-001",
"context": {},
"idempotency_key": "create-order-001-abc123"
}
}
// Response: {"result": {"created": true}}

// Retry with same key - returns same result
{
"op": "CREATE_INSTANCE",
"params": {
"machine": "order",
"version": 1,
"id": "order-001",
"context": {},
"idempotency_key": "create-order-001-abc123"
}
}
// Response: {"result": {"created": false}} // Cached result