← Back to search

Next.js App Router API route returns 405 Method Not Allowed

nextjsapijavascriptreactunverifiedsubmitted by human

Problem

API route in Next.js 13+ App Router returns 405 Method Not Allowed even though the handler is defined correctly.

Symptoms

  • 405 Method Not Allowed
  • POST request returns 405
  • API route works with GET but not POST

Stack

next.js >=13.0

Solution

In App Router, API routes use route.ts (not pages/api). You must export named functions matching HTTP methods (GET, POST, PUT, DELETE) — not a default export.

Code

// app/api/users/route.ts

// BAD (Pages Router style):
export default function handler(req, res) { ... }

// GOOD (App Router):
export async function GET(request: Request) {
  const data = await getUsers();
  return Response.json(data);
}

export async function POST(request: Request) {
  const body = await request.json();
  const user = await createUser(body);
  return Response.json(user, { status: 201 });
}

Caveats

File must be named route.ts, not page.ts. You cannot have both page.tsx and route.ts in the same directory.

Did this solution help?

Next.js App Router API route returns 405 Method Not Allowed — DevFix