Next.js API Route Returns 404 on POST Request
Your Next.js API route returns 404 when making POST, PUT, DELETE requests, even though the route file exists. GET requests might work but other methods don't.
API routes fail when the handler function doesn't export HTTP method handlers, file structure doesn't match App Router conventions, or HTTP method routing is incorrect.
Error Messages You Might See
Common Causes
- Using page.ts instead of route.ts in API route directory
- Default export instead of named exports for HTTP methods (GET, POST, etc)
- Route file in pages directory (Pages Router) instead of app directory (App Router)
- Incorrect URL path matching route.ts file structure
- Missing HttpResponse wrapper in Vercel's Request/Response types
How to Fix It
Use route.ts: API routes must be named route.ts (not page.ts or api.ts) in app/api/your-route/ directory.
Export handlers: Export named async functions for each method:export async function GET(request: Request) { ... }
export async function POST(request: Request) { ... }
App Router pattern: For /api/users endpoint, create app/api/users/route.ts file.
Test with correct method: Verify fetch call uses correct method: fetch(url, { method: 'POST' })
Real developers can help you.
You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.
Get HelpFrequently Asked Questions
What's the difference between route.ts and page.ts?
page.ts renders UI components. route.ts handles HTTP requests and exports GET/POST/etc handlers.
How do I handle multiple HTTP methods?
Export named functions in route.ts: export async function GET() {...} export async function POST() {...}
Why does my POST work in Postman but not fetch?
Check fetch headers include 'Content-Type': 'application/json' and body is JSON.stringify(data).