Skip to main content
API routes in Medusa are file-based HTTP endpoints that follow Next.js-style conventions. They automatically integrate with authentication, validation, and the dependency injection container.

Route Basics

Routes are defined in the src/api directory with a specific file structure:

Creating a Basic Route

1

Create the Route File

Create a route.ts file with named exports for HTTP methods:
src/api/admin/brands/route.ts
Available HTTP methods: GET, POST, PUT, PATCH, DELETE
2

Use Dependency Injection

Access services and modules via req.scope:
3

Execute Workflows

Most routes should execute workflows instead of calling services directly:

Complete CRUD Example

Here’s a full example implementing CRUD operations:

List Brands

src/api/admin/brands/route.ts

Create Brand

src/api/admin/brands/route.ts

Get Single Brand

src/api/admin/brands/[id]/route.ts

Update Brand

src/api/admin/brands/[id]/route.ts

Delete Brand

src/api/admin/brands/[id]/route.ts

Request Types

AuthenticatedMedusaRequest

Use for admin routes that require authentication:

MedusaRequest

Use for public routes that don’t require authentication:

Type Parameters

Add type parameters for request body and query params:

Request Properties

req.scope

Dependency injection container:

req.params

URL path parameters:

req.validatedBody

Validated request body (when using validators):

req.validatedQuery

Validated query parameters:

req.filterableFields

Filters for querying:

req.queryConfig

Query configuration:

req.auth_context

Authentication context (on AuthenticatedMedusaRequest):

Response Methods

res.json()

Send JSON response:

res.status()

Set status code:

Using Query for Data Fetching

The Query service provides a powerful way to fetch related data:

Error Handling

Use standard HTTP status codes and error responses:

Best Practices

  • Use AuthenticatedMedusaRequest for admin routes requiring authentication
  • Execute workflows instead of calling services directly
  • Use the Query service for complex data fetching
  • Type your request and response for type safety
  • Return appropriate HTTP status codes (200, 201, 204, 404, etc.)
  • Use MedusaError for consistent error handling
  • Keep route handlers thin - business logic belongs in workflows
  • Access services via req.scope.resolve()

Next Steps

Create Workflows

Build business logic for your routes

Create Services

Implement module services