Request body is rejected as too large in Next.js (1 MB default)
Body exceeded 1mb limitShort answer
Next.js limits request bodies to 1 MB by default, in both Pages Router API routes and Server Actions. Raise the limit with the setting that matches the code that receives the request. On Vercel a separate fixed 4.5 MB cap applies, so larger files must go straight from the browser to storage.
Fix it fast
1Find out which cause you have
Find out which kind of handler receives the request. The setting lives in a different place for each.
ls pages/api app/api 2>/dev/null; grep -rln "use server" app lib 2>/dev/null2Match what you see, then jump to its fix
- 1
The file is under pages/api and the request carries a large JSON or base64 bodyA Pages Router API route uses the default body parser limitFix - 2
A form with a file input posts to a function marked 'use server'A Server Action receives a body above its limitFix - 3
The response is 413 with FUNCTION_PAYLOAD_TOO_LARGE on Vercel, even after raising the Next.js limitThe request is above the 4.5 MB Vercel function limitFix
- 1
What does the error look like?
Body exceeded 1mb limit413 Payload Too LargeFUNCTION_PAYLOAD_TOO_LARGE
Cause 1 of 3
A Pages Router API route uses the default body parser limit
1Check
API routes parse the body for you and apply bodyParser.sizeLimit, which defaults to 1mb.
grep -rn "bodyParser" pages/api2Fix
Export a config object from that route file and set a larger sizeLimit, using a size string the bytes package understands.
// pages/api/upload.js
export const config = {
api: {
bodyParser: {
sizeLimit: '8mb',
},
},
}3Verify
Send a body larger than 1 MB and confirm the route answers normally.
head -c 3000000 /dev/zero | curl -s -o /dev/null -w "%{http_code}\n" -X POST --data-binary @- -H "Content-Type: application/octet-stream" http://localhost:3000/api/uploadConfirmed by nextjs.org
Cause 2 of 3
A Server Action receives a body above its limit
1Check
Server Actions also cap the request body at 1 MB by default. The limit counts the raw HTTP body, so multipart boundaries and field headers add roughly 10 to 20 KB on top of the file.
grep -n "serverActions" next.config.*2Fix
Set serverActions.bodySizeLimit in next.config, leaving some room above the largest file you accept.
// next.config.js
module.exports = {
experimental: {
serverActions: {
bodySizeLimit: '4mb',
},
},
}3Verify
Restart the server, since next.config is read at startup, and submit the form again.
npm run devConfirmed by nextjs.org
Cause 3 of 3
The request is above the 4.5 MB Vercel function limit
1Check
Vercel rejects function request bodies above 4.5 MB before your code runs, so no Next.js setting can lift it. Compare your file size with that number.
ls -lh ./sample-upload.pdf2Fix
Send large files from the browser straight to storage such as Vercel Blob, and let your route handle only a small token request or the resulting URL. Splitting the data into several requests also works.
// client component
import { upload } from '@vercel/blob/client'
const blob = await upload(file.name, file, {
access: 'public',
handleUploadUrl: '/api/blob/upload',
})3Verify
Upload a file above 5 MB and confirm no 413 appears in the network tab.
# browser DevTools, Network tab: the upload request goes to the storage host, status 200Confirmed by vercel.com
How do I stop it coming back?
- Set the limit on purpose when you add a file field, and check it against the largest file you allow.
- Validate file size in the browser before sending, so users see a clear message.
- For files above a few MB, plan for direct-to-storage uploads from the start.
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
Back end
create-next-app keeps creating a TypeScript project
Would you like to use TypeScript? No / Yes
create-next-app now makes a TypeScript project by default.
2 causes, each with a fixBack end
Show a custom sign-in error in NextAuth without leaving the page
http://localhost:3000/api/auth/error?error=...
By default a failed NextAuth credentials sign-in navigates to the error page with the message in the URL.
3 causes, each with a fixBack end
Next.js server code cannot connect to its own API on 127.0.0.1
Error: connect ECONNREFUSED 127.0.0.1:3003
ECONNREFUSED means nothing was listening at that address and port when the server-side fetch ran.
2 causes, each with a fixBack end
next build stops with: Failed to collect page data for a route
Error: Failed to collect page data for /dashboard
Failed to collect page data for a route is the last line of a failed build, not the cause.
3 causes, each with a fixBack end
TypeError: Failed to parse URL when a Next.js server fetches its own API
TypeError: Failed to parse URL from undefined/api/test
Server code has no page address, so fetch('/api/test') cannot be turned into a full URL and throws Failed to parse URL.
3 causes, each with a fixBack end
next start says it could not find a production build
next start fails before the server listens on any port
next start only serves a finished build, it does not create one.
3 causes, each with a fix