My API call works locally but returns 405 after I deploy to Vercel
AxiosError: Request failed with status code 405Short 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.
Fix it fast
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 '{}'2Match what you see, then jump to its fix
- 1
The 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
There 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
GET to the same URL works, POST does notThe function has no handler for that HTTP methodFix
- 1
What does the error look like?
AxiosError: Request failed with status code 405POST 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
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 api2Fix
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.js3Verify
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
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
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
Mobile apps
A Dio request in Flutter fails with HTTP 405 Method Not Allowed
405 Method Not Allowed
405 means the server knows the URL but not the HTTP method you used on it.
2 causes, each with a fixWeb 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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix