- CRUD operations
- Request validation with Zod schemas
- Background order processing
- State persistence
- Event-driven architecture
Architecture overview
The API consists of three Steps:- API trigger: Handles POST requests and validates input
- Order processor: Processes food orders in the background
- State audit: Periodic job that checks for overdue orders
Step 1: Define types
Createsteps/services/types.ts for shared types:
steps/services/types.ts
Step 2: Create the API endpoint
Createsteps/api.step.ts to handle pet creation and order placement:
steps/api.step.ts
Key features
- Input validation: Zod schemas validate request body and prevent invalid data
- Conditional enqueuing: Only creates order if
foodOrderis present - Trace ID: Automatically included for request tracking
- Type safety: Response schema ensures type-safe responses
Step 3: Process orders in background
Createsteps/process-food-order.step.ts for background order processing:
steps/process-food-order.step.ts
Advanced features
- Multi-trigger: Handles both queue and HTTP triggers
- ctx.getData(): Gets data regardless of trigger type
- ctx.match(): Returns response only for HTTP triggers
- State persistence: Stores orders for later retrieval
- Event chaining: Enqueues notification after processing
Step 4: Add periodic audit job
Createsteps/state-audit-cron.step.ts to check for overdue orders:
steps/state-audit-cron.step.ts
Cron features
- Scheduled execution: Runs automatically every 5 minutes
- State querying: Lists all orders from state
- Conditional logic: Only sends alerts for overdue orders
- Automated monitoring: No manual intervention needed
Testing the API
Create a pet with food order
Process order manually
What you learned
Request validation
Use Zod schemas to validate requests and responses
Multi-trigger Steps
Handle both HTTP and queue triggers in one Step
State management
Store and query data with state.set() and state.list()
Cron jobs
Schedule periodic tasks with cron expressions
Next steps
Background worker
Build complex workflows with multiple workers
State management guide
Learn advanced state patterns