Creating Your First REST API in Motia
Learn how to build a complete REST API with CRUD operations using Motia. This guide covers HTTP endpoints, request validation, and response handling.
If you haven't already, follow the Quick Start Guide to create a Motia project. This tutorial assumes you have a working project with the development server running.
REST APIs are one of the fundamental foundations of most modern web architectures, especially for client-server communication. They allow clients to interact with your backend through standard HTTP methods like GET, POST, PUT, and DELETE. In Motia, creating APIs is straightforward, you define endpoints as API Steps. This guide will walk you through building a complete REST API in Motia. You'll learn how to create HTTP endpoints, validate requests, handle responses, and implement full CRUD operations for a pet store application.
REST API Concepts You'll Use
Before we start building, let's cover the key concepts for creating REST APIs in Motia:
- API Step – A file that defines an HTTP endpoint with validation schemas and request handling logic.
- HTTP Methods – GET (retrieve), POST (create), PUT (update), DELETE (remove) operations on resources.
- Response Handling – Returning proper HTTP status codes and structured JSON responses.
- Path Parameters – Dynamic URL segments like
/pets/:id
to identify specific resources.
API Step Structure
Every API endpoint in Motia consists of two parts that work together:
Component | Type | Description |
---|---|---|
ApiRouteConfig | object | Configuration object that defines the HTTP endpoint structure, including the path, method, and validation schemas. This is where you specify what your API endpoint looks like and how it validates requests. |
Handlers[StepName] | function | The handler function that contains your business logic. It receives the HTTP request and returns an HTTP response with the appropriate status code and data. |
Building a Pet Store REST API
Now that you understand the core parts of an API endpoint, let's build a complete REST API for a pet store.
We'll create a Pet Store API with full CRUD operations:
GET /pet
- List all petsPOST /pets
- Create a new petGET /pets/:id
- Get a specific pet by IDPUT /pets/:id
- Update a pet's informationDELETE /pets/:id
- Remove a pet
This covers all the essential REST API patterns you'll use in real applications.
Create Your First API Endpoint
Let's start by creating a simple endpoint to add new pets to our store.
Creating the Add Pet API
We'll create a POST /pets
endpoint that accepts pet information and stores it. This endpoint will demonstrate key REST API concepts like request validation and response handling.
Create a new file for your first endpoint:
File: steps/create-pet.step.ts
The most fundamental part of any API endpoint is defining what HTTP method it accepts and where it lives:
This configuration tells Motia to create a POST endpoint at /pets. When your development server is running, users can send requests to http://localhost:3000/pets to create new pets. The POST method is the standard HTTP method for creating new resources in REST APIs.
Adding More CRUD Operations
Now that we have a working POST endpoint, let's build out the complete REST API with all CRUD operations. We'll add endpoints to retrieve, update, and delete pets.
Get All Pets - GET /pets
Create a new file to list all pets in the store:
File: steps/get-pets.step.ts
Get Pet by ID - GET /pets/:id
Create a new file to retrieve a specific pet:
File: steps/get-pet-by-id.step.ts
Update Pet - PUT /pets/:id
Create a new file to update existing pets:
File: steps/update-pet.step.ts
Delete Pet - DELETE /pets/:id
Create a new file to remove pets from the store:
File: steps/delete-pet.step.ts
Testing the Complete REST API
You now have a full CRUD REST API. Here are all the endpoints you can test:
POST /pets
- Create a new petGET /pets
- List all petsGET /pets/:id
- Get a specific petPUT /pets/:id
- Update a petDELETE /pets/:id
- Delete a pet
You can test each endpoint using the Workbench interface or with curl commands. This covers all the essential patterns you'll need for building REST APIs.
Build Your First Motia App
Build your first multi-language Motia app in minutes. This guide walks you through creating, running, and understanding a Motia app using JavaScript, TypeScript, and Python.
Core Concepts
Understand the fundamental concepts behind Motia - the unified platform for modern backend systems.