API Endpoints
Learn how to create HTTP API endpoints with Motia
What You'll Build
A pet management API with these endpoints:
- POST
/pets- Create a new pet - GET
/pets- List all pets - GET
/pets/:id- Get a specific pet - PUT
/pets/:id- Update a pet - DELETE
/pets/:id- Delete a pet
Getting Started
Clone the example repository:
Install dependencies:
Start the dev server:
Project Structure
Project organization - This example uses the src/ directory. Motia discovers step files from the src/ directory automatically.
Files like features.json and tutorial/tutorial.tsx are only for the interactive tutorial and are not part of Motia's project structure.
All code examples in this guide are available in the build-your-first-app repository.
You can follow this guide to learn how to build a REST API with Motia step by step, or clone the repository and get started immediately.
Creating Your First Endpoint
This tutorial focuses on Motia's capabilities to create complete backend system from APIs to Streaming AI agents step-by-step. Here, we're showcasing writing APIs with Motia Steps - For data persistence, we use a simple JSON file store in the examples. In a real application, you would use a database like PostgreSQL, MongoDB, or any other data store of your choice. The complete store implementation is available in the GitHub repository.
Configuration
Every API endpoint has two parts:
Config - Defines when and how the step runs:
| Property | Description |
|---|---|
name | Unique identifier |
triggers | Array with a trigger of type: 'api' |
triggers[].path | URL path for the endpoint |
triggers[].method | HTTP method (GET, POST, PUT, DELETE) |
Handler - The function that executes your business logic.
View on GitHub:
Testing Your API
You can test your endpoints by sending requests directly to the API:
Adding GET Endpoints
List All Pets
View on GitHub:
Testing List All Pets
Test with curl:
Get Single Pet
View on GitHub:
The :id in the path creates a path parameter accessible via req.pathParams.id.
Testing Get Single Pet
Test with curl:
Adding UPDATE Endpoint
View on GitHub:
Testing Update Pet
Test with curl:
Adding DELETE Endpoint
View on GitHub:
DELETE endpoints return 204 No Content on success.
Testing Delete Pet
Test with curl:
As you can see in this example, Motia handles routing, validation, and error handling automatically. With just a few lines of code, you've built a complete REST API with:
- Automatic routing based on your step configuration
- Path parameter extraction (
/pets/:id→req.pathParams.id) - HTTP method handling (GET, POST, PUT, DELETE)
- Response formatting with proper status codes
- Built-in error handling and validation
Congratulations! You've successfully created your first API endpoints with Motia. Your pet store API is now ready to handle all CRUD operations.
What's Next?
You now have a working REST API for your pet store! But a complete backend system needs more than just API endpoints. In the next guide, we'll add background jobs using Queue Steps and scheduled tasks with Cron Steps to handle tasks like:
- SetNextFeedingReminder - Queue jobs that automatically schedule feeding reminders when pets are added or updated
- Deletion Reaper - Cron jobs that run daily to clean up soft-deleted records and expired data
Let's continue building your complete backend system by adding these background jobs with Queue Steps and scheduled tasks with Cron Steps.