What is a Step?
A Step is a file with two exports:config- Defines the Step’s name, triggers, and behaviorhandler- The function that executes when the Step is triggered
steps/ directory and connects them to the iii engine.
Steps must have the
.step.ts, .step.js, or _step.py suffix to be auto-discovered.Step Anatomy
Let’s break down a simple Step:The Config Object
Theconfig object defines your Step’s metadata and behavior:
| Property | Type | Description |
|---|---|---|
name | string | Unique identifier for the Step |
description | string (optional) | Human-readable description |
triggers | array | How the Step is invoked |
enqueues | array (optional) | Queue topics this Step can publish to |
flows | array (optional) | Logical groupings for organization |
Triggers
Triggers determine when and how your Step runs. Here are the main types:- HTTP
- Queue
- Cron
- State
- Stream
Triggers on HTTP requests:Use cases: REST APIs, webhooks, form submissions
Multiple Triggers
Steps can have multiple triggers:The Handler Function
The handler receives two arguments:1. Input (First Argument)
The input type depends on the trigger:- HTTP Trigger
- Queue Trigger
- Cron Trigger
- State Trigger
2. Context (Second Argument)
The context provides APIs for interacting with the Motia system:| Property | Type | Description |
|---|---|---|
logger | Logger | Structured logging |
enqueue | Function | Publish to queues |
state | State | Key-value state storage |
streams | Streams | Real-time data streams |
traceId | string | Distributed tracing ID |
invoke | Function | Call other Steps directly |
Logger
Enqueue
State
Streams
Complete Example: Todo API
Let’s build a complete todo API with create, list, and process Steps:Testing Your Steps
With the iii engine running, test your Steps:Advanced Patterns
Conditional Triggers
Error Handling
Calling Other Steps
Best Practices
Keep Handlers Focused
Each Step should do one thing well. Use enqueue() to chain multiple Steps together.
Use Type Safety
Define Zod schemas for inputs and outputs to catch errors early.
Log Liberally
Use structured logging to track execution and debug issues.
Handle Errors Gracefully
Always handle errors and provide meaningful responses.
Use State Wisely
State is for small, frequently accessed data. Use a database for large datasets.
Name Steps Clearly
Use descriptive names that indicate what the Step does (e.g., ‘CreateUser’, ‘ProcessPayment’).
Common Patterns
API + Queue Pattern
Accept requests quickly, process in the background:State Audit Pattern
Track all changes to a resource:Cron + Queue Pattern
Scheduled tasks that fan out to workers:Next Steps
Triggers Deep Dive
Learn about all trigger types and options
State Management
Master state storage and retrieval
Real-time Streams
Build real-time features with streams
Examples
View 20+ production-ready examples