What you’ll build
A two-step application that:- Accepts HTTP requests at
/hello - Enqueues messages for background processing
- Stores results in state
- Logs processing details
Prerequisites
- Node.js 18+ installed
- Motia CLI installed (installation guide)
- A new Motia project created
Step 1: Create the HTTP endpoint
Create a filesteps/hello-api.step.ts that receives requests and enqueues them for processing:
steps/hello-api.step.ts
What’s happening here
- HTTP trigger: Listens for GET requests at
/hello - Response schema: Defines the structure of the 200 response using Zod
- Enqueue: Sends a message to the
process-greetingqueue for background processing - Logger: Records the request for observability
Step 2: Create the queue processor
Create a filesteps/process-greeting.step.ts that processes messages from the queue:
steps/process-greeting.step.ts
What’s happening here
- Queue trigger: Listens for messages on the
process-greetingtopic - Input schema: Validates incoming queue messages
- State management: Stores the processed greeting using
state.set() - Logger: Records processing steps
Step 3: Run your app
Start the iii engine:Step 4: Test it
Send a request to your endpoint:- The HTTP request being received
- The message being enqueued
- The background processor handling the greeting
- The result being stored in state
What you learned
HTTP triggers
Accept HTTP requests and return responses
Queue triggers
Process messages asynchronously in the background
State management
Store and retrieve data across Steps
Context API
Use enqueue, logger, and state from the context
Next steps
REST API example
Build a complete REST API with CRUD operations
Building APIs guide
Learn best practices for API development