Skip to main content

Instances

A process instance is a running execution of a process definition. This guide covers how to start, monitor, suspend, resume, and terminate instances via the API.

Instance Status

StatusDescription
runningThe instance is actively executing
completedThe instance reached an end event
errorAn unhandled error occurred during execution
suspendedExecution is paused
terminatedStopped before completion

Starting an Instance

Launch a new instance of a process definition:

Request:

curl -X POST https://api.stateway.io/v1/instances \
-H "X-API-Key: sw_live_your_key" \
-H "Content-Type: application/json" \
-d '{
"definition_key": "expense-approval",
"variables": {
"amount": 1500,
"requester": "john@acme.com"
}
}'

Response:

{
"data": {
"id": "inst_01j...",
"definition_key": "expense-approval",
"definition_version": 1,
"status": "running",
"variables": {
"amount": 1500,
"requester": "john@acme.com"
},
"started_at": "2026-04-26T12:00:00.000Z"
}
}

Pass an optional correlation_id to link the instance to an external entity (e.g., an order ID). You can later filter by it:

Request:

curl -X POST https://api.stateway.io/v1/instances \
-H "X-API-Key: sw_live_your_key" \
-H "Content-Type: application/json" \
-d '{
"definition_key": "expense-approval",
"correlation_id": "order-789",
"variables": { "amount": 1500 }
}'

Response:

{
"data": {
"id": "inst_01j...",
"definition_key": "expense-approval",
"status": "running",
"correlation_id": "order-789",
"variables": { "amount": 1500 },
"started_at": "2026-04-26T12:00:00.000Z"
}
}

Listing Instances

Request:

curl https://api.stateway.io/v1/instances \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": [
{
"id": "inst_01j...",
"definition_key": "expense-approval",
"status": "running",
"started_at": "2026-04-26T12:00:00.000Z"
}
]
}

Available filters:

# Filter by status
curl "https://api.stateway.io/v1/instances?status=running" \
-H "X-API-Key: sw_live_your_key"

# Filter by definition key
curl "https://api.stateway.io/v1/instances?definition_key=expense-approval" \
-H "X-API-Key: sw_live_your_key"

# Filter by correlation_id
curl "https://api.stateway.io/v1/instances?correlation_id=order-789" \
-H "X-API-Key: sw_live_your_key"

Getting Instance Details

Request:

curl https://api.stateway.io/v1/instances/{instance_id} \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": {
"id": "inst_01j...",
"definition_key": "expense-approval",
"status": "running",
"variables": {
"amount": 1500,
"requester": "john@acme.com"
},
"started_at": "2026-04-26T12:00:00.000Z"
}
}

Instance Tokens

Get the current execution tokens — shows where the process is paused:

Request:

curl https://api.stateway.io/v1/instances/{instance_id}/tokens \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": [
{
"id": "tok_01j...",
"element_id": "review-expense",
"element_type": "userTask",
"status": "waiting",
"arrived_at": "2026-04-26T12:01:00.000Z"
}
]
}

Token status values:

StatusMeaning
activeToken is currently executing
waitingToken is paused (e.g., at a userTask or timerEvent)
completedToken has finished
deadToken was killed by an error or recovery operation and can no longer advance

Instance Variables

Request:

curl https://api.stateway.io/v1/instances/{instance_id}/variables \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": {
"amount": 1500,
"requester": "john@acme.com"
}
}

Updating Variables

Merge new or updated values into the instance variables:

Request:

curl -X POST https://api.stateway.io/v1/instances/{instance_id}/variables \
-H "X-API-Key: sw_live_your_key" \
-H "Content-Type: application/json" \
-d '{
"variables": {
"approved": true,
"approvedAmount": 1500
}
}'

Response:

{
"data": {
"amount": 1500,
"requester": "john@acme.com",
"approved": true,
"approvedAmount": 1500
}
}
warning

Updating variables does not automatically advance the process. Use this for data correction or external system updates.

Instance Lifecycle Operations

Suspend

Pause execution (tokens remain in place):

Request:

curl -X POST https://api.stateway.io/v1/instances/{instance_id}/suspend \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": {
"id": "inst_01j...",
"status": "suspended"
}
}

Resume

Resume a suspended instance:

Request:

curl -X POST https://api.stateway.io/v1/instances/{instance_id}/resume \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": {
"id": "inst_01j...",
"status": "running"
}
}

Terminate

Force-stop an instance before completion:

Request:

curl -X POST https://api.stateway.io/v1/instances/{instance_id}/terminate \
-H "X-API-Key: sw_live_your_key"

Response:

{
"data": {
"id": "inst_01j...",
"status": "terminated"
}
}
warning

Termination is irreversible. The instance status changes to terminated and no further execution occurs. All pending and claimed human tasks for that instance are automatically cancelled.

Instance Lifecycle Diagram

POST /v1/instances


┌─────────┐ suspend ┌───────────┐
│ running │────────────→ │ suspended │
│ │ ←────────────┤ │
└────┬────┘ resume └───────────┘

├─ end event ────→ ┌───────────┐
│ │ completed │
│ └───────────┘

├─ unhandled ────→ ┌───────────┐
│ error │ error │
│ └───────────┘

└─ terminate ─────→ ┌────────────┐
│ terminated │
└────────────┘

Best Practices

  • Poll tokens after starting — use /instances/{id}/tokens to track process position
  • Check status before acting — a task may already be completed by another system
  • Use variables for context — include all data needed by human tasks at start time
  • Suspend before bulk updates — prevents race conditions when updating variables
  • Set up webhooks — receive events instead of polling for status changes