vercel-workflow
Build durable, resilient, and observable TypeScript workflows with Vercel Workflow DevKit. Use when creating workflows that need durability (pause/resume across deploys and crashes), reliability (automatic retries), long-running processes (minutes to months), AI agents, event-driven systems, webhooks, or multi-step async operations in Next.js/TypeScript applications.
Vercel Workflow DevKit
Build durable, resilient, and observable TypeScript workflows that can suspend, resume, and maintain state across deployments, crashes, and long time periods.
Overview
Vercel Workflow DevKit (WDK) transforms ordinary async TypeScript functions into durable workflows using two simple directives:
"use workflow"- Marks deterministic orchestrator functions"use step"- Marks side-effecting operations with automatic retries
Key capabilities:
- Durability: Workflows survive deployments and crashes, resuming exactly where they stopped
- Reliability: Automatic retry logic with configurable error handling
- Long-running: Pause for seconds, hours, days, or months without consuming resources
- Observability: Built-in traces, logs, and metrics for every run
- Portable: Runs locally, on Vercel, or any cloud via custom "Worlds"
When to Use This Skill
Use Vercel Workflow when building:
- AI agents: Multi-step reasoning with pauses between API calls
- User onboarding: Email sequences with delays (e.g., welcome, 1-week check-in, 1-month survey)
- E-commerce: Order processing with payment confirmations and inventory checks
- Approval flows: Pause for human approval before continuing
- Data pipelines: RAG ingestion, embedding, and indexing that can fail/resume
- Event-driven systems: React to webhooks and external events over time
- Scheduled tasks: Cron-like workflows with durability guarantees
Core Concepts
Workflows vs Steps
Workflows ("use workflow"):
- Deterministic orchestrators that coordinate steps
- Must be pure functions during replay (same inputs → same outputs)
- Can call steps, use control flow, and handle serializable data
- Cannot make direct API calls, database queries, or have side effects
Steps ("use step"):
- Side-effecting operations with full Node.js runtime access
- Can make API calls, database queries, file I/O, etc.
- Automatically retried on failure (unless
FatalErroris thrown) - Results are persisted and cached for workflow replay
Critical distinction: During workflow resumption, the workflow replays from the beginning using cached step results. This requires workflows to be deterministic (no Math.random(), Date.now(), etc. unless in steps).
Quick Start
1. Installation (Next.js)
2. Configure Next.js
Wrap next.config.ts with withWorkflow():
3. Create Your First Workflow
Create a workflow file (e.g., workflows/user-signup.ts):
4. Trigger the Workflow
From an API route (app/api/signup/route.ts):
Essential Patterns
Sleeping (Time-Based Pausing)
Use sleep() to pause workflows without consuming resources:
Error Handling
Retryable errors (default):
Non-retryable errors (use FatalError):
Hooks (Custom Pause/Resume)
Pause workflows and resume with arbitrary data:
Resume from external code:
Webhooks (HTTP-Based Pausing)
Create HTTP endpoints that automatically resume workflows:
Webhooks are automatically available at: /.well-known/workflow/v1/webhook/:token
Receiving Multiple Events
Use for await...of to handle multiple events:
Advanced Patterns
Type-Safe Hooks
Use defineHook() for type safety across files:
Dynamic Webhook Responses
Respond based on request content:
Control Flow
Standard JavaScript control flow works in workflows:
Best Practices
1. Keep Workflows Deterministic
DO:
DON'T:
2. Use FatalError for Invalid Input
Don't waste retries on errors that won't be fixed by retrying:
3. Use Custom Tokens for Determinism
Make tokens deterministic so external systems can resume the right workflow:
4. Serialize Only What You Need
Only pass serializable data between workflows and steps (JSON-compatible):
Good: Primitives, objects, arrays
Bad: Functions, class instances, symbols, undefined
Framework Integration
Next.js (Recommended)
Best support with App Router or Pages Router. See references/next_js_setup.md for detailed setup.
Other Frameworks
- Nitro: Supported (see WDK docs)
- SvelteKit: Coming soon
- Nuxt: Coming soon
- Hono/Bun: Coming soon
Custom Deployment
Workflows can run on any platform via custom "Worlds". See references/api_reference.md for details.
Observability
Every workflow run includes:
- Traces: Step-by-step execution path
- Logs: All console output from workflows and steps
- Metrics: Duration, retry counts, error rates
- Time Travel: Replay and debug past executions
Access via CLI or Web UI (automatically included with WDK).
Common Issues
Issue: Workflow doesn't resume after deployment
- Cause: Non-deterministic code in workflow
- Fix: Move side effects (API calls, Date.now(), Math.random()) into steps
Issue: Step keeps retrying forever
- Cause: Regular Error thrown for unrecoverable failure
- Fix: Use
FatalErrorfor invalid input or permanent failures
Issue: Webhook not responding
- Cause:
respondWith: "manual"butrespondWith()not called from step - Fix: Call
request.respondWith()from within a step function
Reference Documentation
For comprehensive API details, see:
- references/api_reference.md - Complete API reference
- references/examples.md - Common patterns and recipes
- references/next_js_setup.md - Next.js integration details
External Resources
- Official Documentation: https://useworkflow.dev
- GitHub Repository: https://github.com/vercel/workflow
- Example Projects: https://github.com/vercel/workflow-examples