Overview
Triggers define how and when a step executes. Motia supports 5 trigger types:- HTTP - REST API endpoints
- Queue - Event-driven messaging
- Cron - Time-based scheduling
- State - State change reactions
- Stream - Real-time data stream events
HTTP Triggers
HTTP triggers expose steps as REST API endpoints.Basic HTTP Trigger
HTTP Trigger Options
GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD
Path Parameters: Use :param syntax for dynamic routes:
motia-js/packages/motia/src/triggers.ts:29-37
Queue Triggers
Queue triggers subscribe to event topics for asynchronous, event-driven processing.Basic Queue Trigger
Queue Configuration
- Standard: High throughput, at-least-once delivery, best-effort ordering
- FIFO: Exactly-once processing, strict ordering, lower throughput
messageGroupId to partition messages:
motia-js/packages/motia/src/types.ts:194-201
Cron Triggers
Cron triggers execute steps on a schedule using cron expressions.Basic Cron Trigger
Cron Expression Format
Motia uses extended cron syntax with optional seconds field:motia-js/playground/steps/cronExample/handlePeriodicJob.step.ts:8
State Triggers
State triggers react to changes in the state management system.Basic State Trigger
State Trigger Input
State triggers receive the following input structure:motia-js/packages/motia/src/types.ts:113-119, motia-py/playground/steps/state_example/on_state_change_step.py
Stream Triggers
Stream triggers react to real-time events from data streams.Basic Stream Trigger
Stream Trigger Input
Stream triggers receive the following input structure:motia-js/packages/motia/src/types.ts:121-133, motia-py/playground/steps/stream_example/on_todo_event_step.py
Trigger Conditions
All trigger types support optional conditions to filter invocations:motia-js/playground/steps/multi-trigger-example.step.ts:4-16
Trigger Registration Protocol
The III Engine uses a WebSocket protocol for trigger registration:engine/src/protocol.rs:39-51
Best Practices
Choose the right trigger type
Choose the right trigger type
- Use HTTP for synchronous requests requiring immediate responses
- Use Queue for asynchronous, decoupled event processing
- Use Cron for scheduled tasks
- Use State for reactive data change handling
- Use Stream for real-time data synchronization
Design for idempotency
Design for idempotency
Queue triggers may retry on failure. Ensure handlers are idempotent and can safely process the same message multiple times.
Use conditions wisely
Use conditions wisely
Conditions filter invocations at the trigger level, reducing unnecessary executions and improving performance.
Handle errors appropriately
Handle errors appropriately
- HTTP triggers: Return appropriate status codes (400, 500, etc.)
- Queue triggers: Throw errors to trigger retries via DLQ
- Cron triggers: Log errors and consider alerting mechanisms
Next Steps
Context API
Learn about ctx.enqueue(), ctx.state, and more
HTTP Triggers
Add middleware to HTTP triggers
State Management
Work with state triggers
Streams
Work with stream triggers