Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

My API call works locally but returns 405 after I deploy to Vercel

ErrorAxiosError: Request failed with status code 405

Short answer

A 405 on the live site but not on localhost usually means the request never reaches your backend. A dev-server proxy does not exist in production, an API file sits outside the root api folder, or the function has no handler for the HTTP method you send. Test with curl first.

  • Applies to Vite 6, Node.js 22, axios 1.x, Vercel
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the same request with curl against the live URL and read the status line and headers. This shows whether the request reaches a function or just the static site.

    curl -i -X POST https://your-project.vercel.app/api/login -H 'Content-Type: application/json' -d '{}'
  2. 2Match what you see, then jump to its fix

    1. 1The front end calls a relative path like /api/login; it works under npm run dev and fails only after deployThe request relied on a dev-server proxyFix
    2. 2There is a backend folder or src/api in the repo, and the URL answers with your HTML page or an error instead of JSONThe API file is not in the root api directoryFix
    3. 3GET to the same URL works, POST does notThe function has no handler for that HTTP methodFix

What does the error look like?

  • AxiosError: Request failed with status code 405
  • POST https://your-project.vercel.app/api/login 405 (Method Not Allowed)

Cause 1 of 3

The request relied on a dev-server proxy

1Check

Vite's server.proxy is a dev server option. A production build is plain static files, so nothing forwards /api anywhere.

grep -n -A6 "proxy" vite.config.*

2Fix

Add a rewrite in vercel.json that sends /api requests to your real backend, or call the backend's full URL from the client.

{
  "rewrites": [
    { "source": "/api/:path*", "destination": "https://api.example.com/:path*" }
  ]
}

3Verify

Redeploy and repeat the curl request from the first step.

curl -i -X POST https://your-project.vercel.app/api/login -d '{}'

Confirmed by vite.dev

Did this fix it?

Cause 2 of 3

The API file is not in the root api directory

1Check

Vercel builds one function for each supported source file in the api folder at the project root. Files elsewhere are not turned into functions.

ls api

2Fix

Put the handler in api/ at the root, or rewrite the public path to a function that lives there.

mkdir -p api
git mv backend/login.js api/login.js

3Verify

Redeploy and request the path again; it should now return the function's response.

curl -i -X POST https://your-project.vercel.app/api/login -d '{}'

Confirmed by vercel.com

Did this fix it?

Cause 3 of 3

The function has no handler for that HTTP method

1Check

A Node.js function can export a handler per HTTP method (GET, POST and so on) or one fetch handler for all of them. Make sure the method your client sends is handled.

grep -n "export" api/login.*

2Fix

Export a handler for the method, or use one fetch handler that routes by method.

// api/login.ts
export function POST(request: Request) {
  return Response.json({ ok: true });
}

3Verify

Redeploy and send the POST again.

curl -i -X POST https://your-project.vercel.app/api/login -d '{}'

Confirmed by vercel.com

Did this fix it?

How do I stop it coming back?

  • Read the base URL from an environment variable instead of leaning on a dev proxy.
  • Test the deployed API with curl before testing it through the UI.
  • Keep every API file in the root api folder, one shared handler where you can.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.