Skip to content

API Reference

Core API

Essential methods and types for defining and running workflows.

Step Options

Configuration options for individual workflow steps.

Retry Policies

Customizable retry behavior for handling failures.

Learn more in the Developing Workflows guide.

Creates a new workflow definition:

defineWorkflow(
name: string,
options?: WorkflowOptions,
fn: WorkflowFunction
): Workflow
export default defineWorkflow('hello-world', async (flow) => {
await flow.do('greet', async () => {
console.log('Hello, World!');
});
});

The context object passed to workflow functions:

interface StepOptions {
// Number of retry attempts and strategy
retries?: RetryPolicy;
// Maximum execution time
timeout?: string | number;
// Unique key for idempotency
idempotencyKey?: string;
// Step-level validation
schema?: Schema;
// Circuit breaker configuration
circuitBreaker?: CircuitBreakerOptions;
// Result caching
cache?: CacheOptions;
}

See Retry Policies for detailed configuration.

interface RetryPolicy {
// Maximum retry attempts
limit: number;
// Delay between retries
delay: string | number;
// Retry delay pattern
backoff: 'constant' | 'exponential' | 'linear';
}
interface CircuitBreakerOptions {
// Failures before opening
failureThreshold: number;
// Time before retry
resetTimeout: string | number;
}
interface CacheOptions {
// Cache duration
ttl: string | number;
// Cache key
key: string;
}
await flow.do('process payment', {
retries: {
limit: 3,
delay: '30 seconds',
backoff: 'exponential'
},
timeout: '5 minutes',
idempotencyKey: orderId
}, async () => {
return processPayment(flow.params);
});

See the main Error Handling guide for comprehensive strategies.

Use to indicate permanent failures that shouldn’t be retried:

await flow.do('process payment', async () => {
try {
return await processPayment(flow.params);
} catch (error) {
if (error.code === 'INVALID_CARD') {
throw new NonRetryableError(error);
}
throw error;
}
});

Thrown when input validation fails. See Data Validation.

await flow.do('validate input', { schema: InputSchema }, async (input) => {
// Input is validated against schema
return processInput(input);
});

Explore Observability Features and the Observability Deep Dive.

await flow.do('process order', async ({ span }) => {
// Add custom attributes
span.setAttribute('order.id', flow.params.orderId);
// Create nested span
const validateSpan = span.createSpan('validate');
try {
await validateOrder(flow.params);
validateSpan.setStatus({ code: SpanStatusCode.OK });
} catch (error) {
validateSpan.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
throw error;
} finally {
validateSpan.end();
}
});
// Note: Collecting and exporting metrics requires integrating with an
// OpenTelemetry collector and backend system separately.
// See OpenTelemetry documentation for setup details.
interface Workflow<TParams = any, TResult = any> {
id: string;
name: string;
version: string;
options?: WorkflowOptions;
run(params: TParams): Promise<TResult>;
}
interface WorkflowOptions {
retries?: RetryPolicy;
timeout?: string | number;
schema?: Schema;
recovery?: RecoveryHandlers;
}
interface WorkflowContext<TParams = any> {
params: TParams;
instance: WorkflowInstance;
definition: WorkflowDefinition;
do<T>(name: string, options?: StepOptions, fn: StepFunction<T>): Promise<T>;
use<T>(binding: Binding<T>): T;
sleep(name: string, duration: string | number): Promise<void>;
}
interface StepFunction<T> {
(context: StepContext): Promise<T>;
}
interface StepContext {
span: Span;
use<T>(binding: Binding<T>): T;
log: Logger;
}

Workflow Rules

Learn essential guidelines for building reliable workflows.