Next.jsAPI RoutesError Fix2026 Updated

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

January 20266 min readNext.js 14/15 · App Router · REST API

⚡ The Error

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/api/users/route.ts
// 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.

1

Use Named Exports in App Router

Root cause — wrong export pattern
2 min
app/api/products/route.ts — correct App Router pattern
import { 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.

2

Fix Pages Router Method Check

Using pages/api/ directory — old pattern
2 min

If you are using the Pages Router (pages/api/ directory), export a single default function and check req.method explicitly. Return 405 for unsupported methods.

pages/api/users.ts — Pages Router pattern
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` })
}
3

Add OPTIONS Handler for CORS Preflight

Fetch from frontend blocked — CORS preflight failing
3 min

When 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.

app/api/data/route.ts — with CORS headers
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 })
}
4

Fix PUT and DELETE Returning 405

CRUD routes — update and delete not working
2 min

PUT 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.

app/api/posts/[id]/route.ts
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.

5

Dynamic Route Handlers

Route handler for /api/users/[id]
2 min

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.

Correct file structure for dynamic API routes
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 operations

Prevention

Frequently Asked Questions

Why does my Next.js API route return 405?+
In the App Router, HTTP methods are handled by named exports — GET, POST, PUT, DELETE. If you export a function with any other name (like 'handler' or 'default'), Next.js returns 405 for that method. Each method must be a named async export.
What is the difference between Pages Router and App Router API routes?+
In the Pages Router, you export a single default function and check req.method inside it. In the App Router (route.ts), you export separate named functions for each HTTP method: export async function GET(request) and export async function POST(request). Mixing these patterns causes 405 errors.
How do I handle multiple HTTP methods in a Next.js App Router route?+
Export a separate async function for each method from the same route.ts file. For example: export async function GET() { ... } and export async function POST() { ... } in the same file. Next.js routes the request to the correct function based on the HTTP method.
Why does my CORS preflight OPTIONS request return 405?+
CORS preflight requests use the OPTIONS method. In the App Router, you must explicitly export an OPTIONS handler that returns the correct CORS headers. Without it, Next.js returns 405 for OPTIONS, blocking cross-origin requests.
Can I have a catch-all method handler in App Router?+
No. You must export one function per method. There is no wildcard method handler in App Router. If you want to handle all methods in one function, use the Pages Router pattern with a default export and req.method checks.
What HTTP methods does Next.js App Router support?+
The App Router supports GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS as named exports. Any other method name will not be recognised and will return 405.

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