Next.js API Route 405 Method Not Allowed— Fixed [Complete Guide 2026]
Also covers: App Router named exports · Pages Router · OPTIONS handler · CORS preflight · PUT DELETE
⚡ The Error
POST /api/users 405 Method Not Allowed
// or in the browser console:
Failed to load resource: the server responded with a status of 405✅ App Router Fix — named exports
// App Router: each HTTP method = a named export
export async function GET(request: Request) {
const users = await db.user.findMany()
return Response.json(users)
}
export async function POST(request: Request) {
const body = await request.json()
const user = await db.user.create({ data: body })
return Response.json(user, { status: 201 })
}Why Next.js Returns 405 Method Not Allowed
The App Router changed how API routes work. In the Pages Router, you export a single default function and check req.method inside it. In the App Router, you must export a separate named async function for each HTTP method — GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
If you use the old Pages Router pattern in an App Router route.ts file — or misname your exports — Next.js returns 405 for the unrecognised method.
Use Named Exports in App Router
Root cause — wrong export patternimport { NextRequest } from "next/server"
// ✅ Each HTTP method = its own named async export
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const category = searchParams.get("category")
const products = await db.product.findMany({ where: { category } })
return Response.json(products)
}
export async function POST(request: NextRequest) {
const body = await request.json()
const product = await db.product.create({ data: body })
return Response.json(product, { status: 201 })
}
export async function DELETE(request: NextRequest) {
const { id } = await request.json()
await db.product.delete({ where: { id } })
return new Response(null, { status: 204 })
}
// ❌ This does NOT work in App Router — 405 for every request
export default function handler(req: any, res: any) {
if (req.method === "GET") { ... }
}The file must be named route.ts (or route.js) — not handler.ts or api.ts. Any other filename is treated as a regular Server Component and will not receive HTTP requests.
Fix Pages Router Method Check
Using pages/api/ directory — old patternIf you are using the Pages Router (pages/api/ directory), export a single default function and check req.method explicitly. Return 405 for unsupported methods.
import type { NextApiRequest, NextApiResponse } from "next"
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "GET") {
const users = await db.user.findMany()
return res.status(200).json(users)
}
if (req.method === "POST") {
const user = await db.user.create({ data: req.body })
return res.status(201).json(user)
}
// Explicitly return 405 for unsupported methods
res.setHeader("Allow", ["GET", "POST"])
return res.status(405).json({ error: `Method ${req.method} not allowed` })
}Add OPTIONS Handler for CORS Preflight
Fetch from frontend blocked — CORS preflight failingWhen your frontend JavaScript calls an API on a different origin, the browser sends a preflight OPTIONS request first. Without an OPTIONS handler, Next.js returns 405, blocking the main request.
const CORS_HEADERS = {
"Access-Control-Allow-Origin": process.env.FRONTEND_URL ?? "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
}
// Handle CORS preflight
export async function OPTIONS() {
return new Response(null, { status: 204, headers: CORS_HEADERS })
}
export async function GET() {
const data = await db.item.findMany()
return Response.json(data, { headers: CORS_HEADERS })
}
export async function POST(request: Request) {
const body = await request.json()
const item = await db.item.create({ data: body })
return Response.json(item, { status: 201, headers: CORS_HEADERS })
}Fix PUT and DELETE Returning 405
CRUD routes — update and delete not workingPUT and DELETE must also be named exports in App Router. A common pattern is to put the resource ID in a dynamic segment and access it via params.
import { NextRequest } from "next/server"
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
const post = await db.post.findUnique({ where: { id } })
if (!post) return new Response("Not found", { status: 404 })
return Response.json(post)
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
const body = await request.json()
const post = await db.post.update({ where: { id }, data: body })
return Response.json(post)
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
await db.post.delete({ where: { id } })
return new Response(null, { status: 204 })
}In Next.js 15, params is now a Promise. Always await it: const { id } = await params — this changed from Next.js 14 where params was a plain object.
Dynamic Route Handlers
Route handler for /api/users/[id]Dynamic API segments use the same bracket syntax as pages. The folder structure app/api/users/[id]/route.ts handles requests like /api/users/abc123.
app/
└── api/
├── users/
│ ├── route.ts → handles GET /api/users, POST /api/users
│ └── [id]/
│ └── route.ts → handles GET /api/users/123, PUT /api/users/123
# ❌ Wrong — query param approach from Pages Router
# app/api/users/route.ts trying to handle /api/users?id=123 for PUT
# Use [id]/route.ts instead for resource-specific operationsPrevention
- In App Router: use named exports (GET, POST, PUT, DELETE, PATCH, OPTIONS) — never a default export
- Name the file route.ts not handler.ts or api.ts — only route.ts receives HTTP requests
- Add an OPTIONS handler whenever your API will be called from a different origin (CORS preflight)
- In Next.js 15, await the params argument — it is now a Promise, not a plain object
- Use dynamic segments [id]/route.ts for resource-specific routes — not query parameters
- In Pages Router, always include a final res.status(405) fallthrough for unsupported methods
Frequently Asked Questions
Why does my Next.js API route return 405?+−
What is the difference between Pages Router and App Router API routes?+−
How do I handle multiple HTTP methods in a Next.js App Router route?+−
Why does my CORS preflight OPTIONS request return 405?+−
Can I have a catch-all method handler in App Router?+−
What HTTP methods does Next.js App Router support?+−
Need Expert Help?
We Build Production REST APIs with Next.js
Softplix engineers build scalable APIs using Next.js App Router, Prisma, and Supabase. Need help designing your API layer? Let us help.
Talk to an Engineer