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 Workbench:
Your Workbench will be available at http://localhost:3000
.
Project Structure
Files like features.json
and 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 you can clone the repository and dive into our Interactive Tutorial to learn by doing directly in the Workbench.
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 |
type | Set to 'api' |
path | URL path for the endpoint |
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 using curl or the Workbench interface.
Using curl
Using Workbench
You can also test your endpoint directly in the Workbench, which provides an interactive interface to test your API endpoints with real requests and see the responses in real-time:
Adding GET Endpoints
List All Pets
View on GitHub:
Testing List All Pets
Test with curl:
Or use the Workbench interface:
Get Single Pet
View on GitHub:
Testing tip: When testing GET endpoints with path parameters like /pets/:id
, switch to the Params tab (not Body) to enter the ID value.
The :id
in the path creates a path parameter accessible via req.pathParams.id
.
Testing Get Single Pet
Test with curl:
Or use the Workbench interface:
Adding UPDATE Endpoint
View on GitHub:
Testing Update Pet
Test with curl:
Or use the Workbench interface:
Adding DELETE Endpoint
View on GitHub:
DELETE endpoints return 204 No Content
on success.
Testing Delete Pet
Test with curl:
Or use the Workbench interface:
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 Event 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 Event Steps and scheduled tasks with Cron Steps.