Defining Steps
Learn how to create powerful, type-safe steps in TypeScript, Python, and JavaScript with automatic validation and observability.
Defining Steps
Steps are the core building blocks of Motia - isolated, composable functions that handle specific pieces of business logic. Each step is automatically discovered, type-safe, and observable across multiple programming languages.
The Motia Step Pattern
Every step follows a simple, consistent pattern:
- 📁 File Naming:
*.step.*
or*_step.*
(e.g.,user-api.step.ts
,process-data.step.py
,data_processor_step.py
) - ⚙️ Configuration: Export a
config
object defining step behavior - 🔧 Handler: Export a
handler
function containing business logic - 🤖 Auto-Discovery: Motia automatically finds and registers your steps
🌍 Multi-Language Support
Write each step in the best language for the job - TypeScript for APIs, Python for data processing, JavaScript for quick scripts. Motia handles type safety and communication automatically.
Step Capabilities
Event-Driven Architecture
- Subscribe to events from other steps
- Emit events to trigger subsequent steps
- Chain steps together into powerful workflows
Type Safety Across Languages
- Auto-generated TypeScript definitions
- Schema validation with Zod, Pydantic, JSDoc
- Runtime validation for data consistency
Built-in Observability
- Distributed tracing across all steps
- Centralized logging with step context
- Visual debugging in Motia Workbench
Step Configuration
Each step exports a config
object that tells Motia how to handle the step. The configuration varies by step type but shares common properties:
Universal Config Properties
Property | Type | Description | Required |
---|---|---|---|
type | 'api' | 'event' | 'cron' | 'noop' | The step type | ✅ |
name | string | Unique identifier for the step | ✅ |
description | string | Documentation for the step | - |
subscribes | string[] | Topics this step listens to | - |
emits | string[] | Topics this step can emit | - |
flows | string[] | Flow identifiers this step belongs to | - |
Type-Specific Properties
Each step type has additional properties:
- API Steps:
path
,method
,bodySchema
,responseSchema
- Event Steps: Event-specific configuration
- Cron Steps:
schedule
, cron expressions
Configuration Examples
Step Handlers
The handler
function contains your step's business logic. Motia automatically calls the handler with validated input data and a context object containing powerful utilities.
Handler Signature
Every handler receives two parameters:
- Input Data - Validated data from the triggering event (API request, subscription, etc.)
- Context Object - Tools for interacting with the Motia runtime
Context Object Features
Tool | Description | Usage |
---|---|---|
emit | Send events to other steps | await emit({ topic, data }) |
logger | Centralized logging with trace context | logger.info('Processing user', { userId }) |
state | Persistent data storage across steps | await state.set(traceId, key, value) |
traceId | Unique identifier for request tracing | Flow isolation and debugging |
utils | Helper utilities (dates, crypto, etc.) | Various utility functions |
Handler Examples with Type Safety
Type Safety Benefits
Automatic Type Generation
Motia automatically generates TypeScript definitions based on your step configurations:
Cross-Language Validation
Even in multi-language workflows, Motia ensures data consistency:
- TypeScript API validates input with Zod schemas
- Python step receives validated data via Pydantic models
- JavaScript step gets type hints via JSDoc annotations
🚀 Ready to Build?
Check out API Endpoints to see a complete REST API tutorial in action, or explore specific step types:
- API Steps - HTTP endpoints with validation
- Event Steps - Async event processing
- Cron Steps - Scheduled tasks