Framework
An order approval needs somewhere to wait between a sales rep's request and a supervisor's decision. A workflow definition records the possible paths, while each instance tracks one order's steps, context and outstanding work.
This section provides technical documentation for developers who want to integrate workflows into their applications, extend the engine with custom functionality, or understand the underlying architecture.
Start workflows, send signals, and complete tasks programmatically:
Extend the workflow engine with custom functionality:
Understand how the workflow engine works:
import { apiFetch } from '@open-mercato/ui/backend/utils/api'
async function startApprovalWorkflow(orderId: string, amount: number) {
const response = await apiFetch('/api/workflows/instances', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
workflowId: 'purchase-approval-v1',
initialContext: {
orderId,
amount,
requesterEmail: '[email protected]',
approverEmail: '[email protected]'
},
correlationKey: orderId
})
})
if (!response.ok) {
throw new Error('Failed to start workflow')
}
return await response.json()
}
import { ActivityDefinition, ActivityExecutionContext, ActivityResult } from '../types'
export async function executeSendSmsActivity(
definition: ActivityDefinition,
context: ActivityExecutionContext
): Promise<ActivityResult> {
const { to, message } = definition.config
try {
const smsService = context.container.resolve('smsService')
const result = await smsService.send({ to, message })
return {
success: true,
output: { messageId: result.messageId }
}
} catch (error: any) {
return {
success: false,
error: error.message,
retryable: true
}
}
}
For non-technical users and business users, see the User Guide.
POST /api/workflows/instances - Start workflowPOST /api/workflows/instances/{id}/signal - Send signalPATCH /api/workflows/instances/{id}/context - Merge a context patch and wake WAIT_FOR_CONDITION waiters (workflows.instances.update_context)GET /api/workflows/instances/{id} - Get instance detailsGET /api/workflows/instances - List instances (?attention=true lists parked runs)POST /api/workflows/tasks/{id}/complete - Complete taskGET /api/workflows/definitions/{id}/context-schema - Per-step context ledgerPOST /api/workflows/definitions/{id}/test-step - Mock-first single-activity test runGET /api/workflows/endpoints - Endpoint catalogue for the CALL_API pickerWorkflowService - Core workflow operationsWorkflowExecutor - State machine executionActivityExecutor - Activity executionTaskService - User task managementCreate a test definition with one human task. Start an instance, inspect the waiting state, open the task as its assignee and complete it. The instance history should now show both the wait and the transition that resumed the work.