Installation
Install IdentityFlow using your preferred package manager.
Installation
Install IdentityFlow using your preferred package manager.
First Workflow
Create your first workflow with step-by-step guidance.
Best Practices
Learn essential patterns for reliable workflows.
Install IdentityFlow using your preferred package manager:
npm i @identity-flow/sdkpnpm add @identity-flow/sdkyarn add @identity-flow/sdkCreate a new workflow file:
import * as v from '@identity-flow/sdk/valibot';import { defineWorkflow } from '@identity-flow/sdk';
// Define input schemaconst OrderSchema = v.object({ orderId: v.string(), items: v.array(v.object({ productId: v.string(), quantity: v.number([v.positive()]) })),});Define the workflow with validation:
export default defineWorkflow( 'order-processing', { // Validate input against schema schema: OrderSchema, // Configure default retry policy retries: { limit: 3, delay: '30 seconds', backoff: 'exponential' }, }, async (flow) => { // Workflow implementation },);Add workflow steps:
// Validate orderconst validatedOrder = await flow.do('validate order', async () => { return validateOrder(flow.params);});
// Process payment and check inventory in parallelconst [payment, inventory] = await Promise.all([ flow.do('process payment', async () => { return processPayment(validatedOrder); }), flow.do('check inventory', async () => { return checkInventory(validatedOrder.items); }),]);
// Fulfill orderawait flow.do('fulfill order', async () => { return fulfillOrder({ order: validatedOrder, payment, inventory });});Add error handling:
try { await flow.do('process payment', async () => { return processPayment(validatedOrder); });} catch (error) { if (error.code === 'INSUFFICIENT_FUNDS') { throw new NonRetryableError(error); } throw error;}Return workflow result:
return { orderId: validatedOrder.orderId, status: 'COMPLETED', payment: payment.id};Here’s a complete workflow that processes orders:
import * as v from '@identity-flow/sdk/valibot';import { NonRetryableError, defineWorkflow } from '@identity-flow/sdk';
// Input schemaconst OrderSchema = v.object({ orderId: v.string(), items: v.array(v.object({ productId: v.string(), quantity: v.number([v.positive()]) })),});
// Result typeinterface OrderResult { orderId: string; status: 'COMPLETED' | 'failed'; payment?: string;}
export default defineWorkflow<typeof OrderSchema, OrderResult>( 'order-processing', { schema: OrderSchema, retries: { limit: 3, delay: '30 seconds', backoff: 'exponential' } }, async (flow) => { try { // Validate order const validatedOrder = await flow.do('validate order', async () => { return validateOrder(flow.params); });
// Process payment and check inventory in parallel const [payment, inventory] = await Promise.all([ flow.do('process payment', async () => { try { return await processPayment(validatedOrder); } catch (error) { if (error.code === 'INSUFFICIENT_FUNDS') { throw new NonRetryableError(error); } throw error; } }), flow.do('check inventory', async () => { return checkInventory(validatedOrder.items); }), ]);
// Fulfill order await flow.do('fulfill order', async () => { return fulfillOrder({ order: validatedOrder, payment, inventory }); });
// Return success result return { orderId: validatedOrder.orderId, status: 'COMPLETED', payment: payment.id }; } catch (error) { // Return failure result return { orderId: flow.params.orderId, status: 'failed' }; } },);Input Validation
Error Handling
NonRetryableError for permanent failuresStep Design