Reference
API Routes
Next.js Route Handler reference.
Overview
API routes are implemented as Next.js Route Handlers in src/app/api/. All routes call server-only services for business logic.
Base URL
- Development:
http://localhost:3000/api - Production:
https://yourdomain.com/api
Authentication Routes
Billing Routes
User Routes
Utility Routes
Error Responses
All errors follow this format:
{
"ok": false,
"redirectPath": "/path?error=message&error_description=details"
}Error information is encoded in the redirect path query parameters.
Adding New Routes
- Create a route file in
src/app/api/:
import { NextResponse } from 'next/server';
export async function GET() {
return NextResponse.json({ hello: 'world' });
}
export async function POST(request: Request) {
const body = await request.json();
// Process request
return NextResponse.json({ ok: true });
}- For complex logic, create a service:
import "server-only";
export async function processData(data: unknown) {
// Business logic
return { processed: true };
}- Import and use the service in your route.
Related
- Authentication - Auth flow details
- Payments - Billing flow details
- Project Structure - File organization
Was this page helpful?