Skip to main content

API Routes

Expo Router supports creating API endpoints using the +api convention. These routes run server-side and can handle HTTP requests.
API routes require server-side rendering setup with @expo/server or a compatible server environment.

Creating API Routes

Create an API route by adding +api to your filename:
From CLAUDE.md:210-216:

Basic API Route

Create a simple API endpoint:
app/api/hello+api.ts
Request:
Response:

HTTP Methods

Support different HTTP methods:
app/api/users+api.ts

Dynamic API Routes

Create API routes with parameters:
app/api/users/[id]+api.ts
Requests:

Request Handling

Reading Query Parameters

app/api/search+api.ts
Request:

Reading Request Body

app/api/posts+api.ts

Reading Headers

app/api/protected+api.ts

Response Types

JSON Response

Text Response

Redirect

Stream Response

Error Handling

app/api/data+api.ts

Middleware Pattern

Create reusable middleware:
lib/api-middleware.ts
Use middleware:
app/api/profile+api.ts

CORS

Handle cross-origin requests:
app/api/public+api.ts

Database Integration

app/api/posts+api.ts

Calling API Routes

From Client Code

app/posts/index.tsx

With Authentication

Testing API Routes

Unit Tests

__tests__/api/users.test.ts

Best Practices

Validate Input

Use Status Codes Correctly

Handle Errors Gracefully

Next Steps

Server Rendering

Learn about SSR and SSG

Dynamic Routes

Dynamic API endpoints

Typed Routes

Type-safe API routes

Deep Linking

API route URLs