Workflows & State Machines
Detail workflow modeling, transition guards, explain endpoints, and migration patterns relevant to long-lived processes.
Workflow State Machine Example
Workflow Transition Flow
What are Workflows?
Workflows are finite-state machines that manage the lifecycle of objects. Each workflow defines:
- States – Possible states an object can be in (e.g., draft, review, approved, published)
- Transitions – Allowed state changes
- Guards – Rules that must be satisfied before a transition can occur
- Initial state – The starting state for new objects
Creating a Workflow
Via API
curl -X POST \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"name": "document-approval",
"displayName": "Document Approval Workflow",
"states": [
{"id": "draft", "displayName": "Draft"},
{"id": "review", "displayName": "Under Review"},
{"id": "approved", "displayName": "Approved"},
{"id": "rejected", "displayName": "Rejected"},
{"id": "published", "displayName": "Published"}
],
"transitions": [
{
"from": "draft",
"to": "review",
"guard": "has_permission"
},
{
"from": "review",
"to": "approved",
"guard": "approval_rule"
},
{
"from": "review",
"to": "rejected",
"guard": "rejection_rule"
},
{
"from": "approved",
"to": "published",
"guard": "publish_rule"
},
{
"from": "rejected",
"to": "draft",
"guard": "revise_rule"
}
],
"initialState": "draft"
}' \
https://core.example.com/v1/workflows
Via Admin Console
- Navigate to Workflows in the Admin Console
- Click Create New Workflow
- Define states and transitions
- Configure guards for each transition
- Save and publish
Transition Guards
Guards are rules that must be satisfied before a transition can occur. They can check:
- Permissions – User roles and capabilities
- Object attributes – Current values of object fields
- Spatial conditions – Geometry-based rules
- External conditions – Results from API calls or database queries
Example Guards
{
"guard": {
"type": "and",
"rules": [
{
"type": "permission",
"requiredRole": "admin"
},
{
"type": "attribute",
"path": "$.status",
"operator": "equals",
"value": "ready"
}
]
}
}
Triggering Transitions
Via API
curl -X POST \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"targetState": "review",
"metadata": {
"reason": "Ready for review"
}
}' \
https://core.example.com/v1/objects/{objectId}/workflow/transitions
Explaining Transitions
Before attempting a transition, you can check why it might fail:
curl -H "Authorization: Bearer <token>" \
https://core.example.com/v1/objects/{objectId}/workflow/explain-transitions
This returns:
- Available transitions
- Required guards for each transition
- Current guard evaluation results
- Reasons why transitions are blocked
Workflow Versioning
Workflows follow the same versioning lifecycle as object types:
- Draft – Can be modified freely
- Sandbox – Test with sample objects
- Published – Immutable, used in production
Migrating Objects
When a workflow is updated, existing objects continue using their original workflow version. To migrate:
- Create a migration script
- Read objects with the old workflow
- Transition objects to compatible states in the new workflow
- Update objects to use the new workflow version
Best Practices
- Start simple – Begin with basic states and add complexity as needed
- Test thoroughly – Use sandbox mode to test workflow logic
- Document guards – Clearly document why each guard exists
- Handle edge cases – Consider what happens when guards fail
- Version carefully – Published workflows are immutable
- Monitor transitions – Track transition success/failure rates
- Use explain endpoints – Help users understand why transitions fail