Vercel 413 FUNCTION_PAYLOAD_TOO_LARGE on file uploads (and "Body exceeded 1 MB limit")
By AI App Medic · Updated · Vercel · Next.js · Uploads · Cursor
Avatar uploads work. Then a user uploads a 6 MB PDF or a phone video and nothing happens, or the app shows a generic error. In the Network tab the upload request comes back with:
413 Request Entity Too Large
FUNCTION_PAYLOAD_TOO_LARGE
Or, if the upload goes through a Next.js Server Action, the server log says:
Error: Body exceeded 1 MB limit.
To configure the body size limit for Server Actions, see:
https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit
In production the browser often gets only a generic Server Components error, because Next.js hides server error messages from the client. The function logs on Vercel may show nothing at all for the 413. That is a clue, not a logging bug: the request was rejected before your code ran.
Why it happens
Apps built with Cursor, v0 or Bolt usually upload files the simplest way: the browser posts the file to an API route or a Server Action, and that code saves it to storage. On a long-running server this is fine. On Vercel, every Route Handler, API route and Server Action runs as a Vercel Function, and Vercel caps the request body of a function at 4.5 MB. Anything bigger gets a 413 with FUNCTION_PAYLOAD_TOO_LARGE. There is no setting in vercel.json or next.config that changes it on a default project.
Next.js adds its own, lower limit for Server Actions: 1 MB by default, there to stop anyone from making your server parse huge bodies. It applies everywhere, including on your laptop.
Two details push files over the limit sooner than you expect:
- Base64. AI-generated code often reads the file with
FileReader.readAsDataURLand sends it inside JSON. Base64 makes it about 33 percent bigger, so a 3.5 MB photo arrives as roughly 4.7 MB. - Multipart overhead. Form boundaries and field headers count toward the limit too. A file of exactly 1 MB already fails the default Server Actions limit.
The response side has a limit as well. A function that sends a large file back to the browser, instead of streaming it or handing out a link, fails past 4.5 MB.
Step 1: find out which limit you hit
413withFUNCTION_PAYLOAD_TOO_LARGE: Vercel's 4.5 MB function body limit.Error: Body exceeded 1 MB limit.: the Next.js Server Actions limit. The number in the message follows your setting, so after raising it you would seeBody exceeded 5mb limit.413 Request Entity Too Largewith an nginx page: your own server, if you moved off Vercel to a VPS. nginx allows 1 MB by default. See Step 6.- Netlify: synchronous Netlify Functions have a 6 MB payload limit, so the same problem appears a little later.
Step 2: the small fix, if files stay small
If the only uploads are avatars or logos and you can promise they stay under about 4 MB, raising the Server Actions limit is enough:
// next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
serverActions: {
bodySizeLimit: '4mb',
},
},
}
export default nextConfig
Keep the value close to what you need. It applies to every Server Action in the app, and each one is a public POST endpoint that Next.js reads before your auth check runs. Setting it above 4.5 MB does nothing on a default Vercel project, because the platform limit rejects the request first. Also check the file size in the browser before sending, and tell the user the limit, so they see a message instead of a dead button.
Step 3: the real fix, upload straight to storage
For PDFs, video, audio or anything a user picks from their phone, the file should not go through your function at all. The browser uploads it directly to storage, and your server only decides who may upload what. With Supabase Storage that is a signed upload URL. First a small route that runs after checking the user:
// app/api/uploads/route.ts
import { NextResponse } from 'next/server'
import { createClient as createAdminClient } from '@supabase/supabase-js'
import { createClient } from '@/lib/supabase/server' // reads the user session from cookies
const MAX_BYTES = 50 * 1024 * 1024
const ALLOWED = ['application/pdf', 'image/jpeg', 'image/png', 'image/webp']
export async function POST(req: Request) {
const supabase = await createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) return NextResponse.json({ error: 'Not signed in' }, { status: 401 })
const { fileName, contentType, size } = await req.json()
if (!ALLOWED.includes(contentType) || typeof size !== 'number' || size > MAX_BYTES) {
return NextResponse.json({ error: 'File type or size not allowed' }, { status: 400 })
}
const ext = String(fileName).split('.').pop()?.toLowerCase().replace(/[^a-z0-9]/g, '') || 'bin'
const path = `${user.id}/${crypto.randomUUID()}.${ext}`
const admin = createAdminClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!
)
const { data, error } = await admin.storage.from('documents').createSignedUploadUrl(path)
if (error) return NextResponse.json({ error: error.message }, { status: 500 })
return NextResponse.json({ path: data.path, token: data.token })
}
This request carries a few bytes of JSON, so no limit applies. The service role key stays on the server; it must never get a NEXT_PUBLIC_ prefix. Then the browser uploads the file itself:
async function uploadDocument(file: File) {
const res = await fetch('/api/uploads', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ fileName: file.name, contentType: file.type, size: file.size }),
})
const info = await res.json()
if (!res.ok) throw new Error(info.error)
const { error } = await supabase.storage
.from('documents')
.uploadToSignedUrl(info.path, info.token, file, { contentType: file.type })
if (error) throw error
return info.path // save this path in your table with a normal, small request
}
The token works for that one path only and expires after two hours. The type and size the browser reports are only claims, so set the real limits on the bucket: in Supabase Storage open the bucket settings and set a file size limit and the allowed MIME types. The global file size limit on the free plan is 50 MB. For very large files or flaky mobile connections, Supabase also supports resumable uploads, which retry in chunks.
Step 4: the other two direct upload options
Upload with the user's own session. If the user is signed in with supabase-js in the browser, supabase.storage.from('documents').upload(path, file) also skips your function. Here the access check is a storage policy instead of your route:
create policy "Users upload to their own folder"
on storage.objects for insert to authenticated
with check (
bucket_id = 'documents'
and (storage.foldername(name))[1] = (select auth.uid()::text)
);
Without that policy the upload fails with new row violates row-level security policy. Keep the bucket private unless the files are meant to be public; a public bucket means anyone with the URL can read them.
Vercel Blob client uploads. If you store files in Vercel Blob, use upload() from @vercel/blob/client in the browser and handleUpload() in a route. Do the user check inside onBeforeGenerateToken and return allowedContentTypes and maximumSizeInBytes there. Without that check, anyone can upload to your store.
Step 5: process the file by path, not by body
If the app does something with the file after upload, such as extracting text from a PDF or sending it to an AI model, send the storage path to your route, not the file. The function downloads it from storage with the service role or a short signed URL. That keeps the request small and the logic in one place. Long processing then hits the time limit instead of the size limit, which is covered in Vercel 504 FUNCTION_INVOCATION_TIMEOUT.
For downloads, do the same in reverse: return a signed URL from createSignedUrl and let the browser fetch the file from storage, instead of piping it through a function.
Step 6: if you moved to your own server
On a VPS behind nginx the default client_max_body_size is 1 MB, and the error page says 413 Request Entity Too Large. Raise it in the server block for your app, test the config, then reload:
server {
server_name app.example.com;
client_max_body_size 25m;
# ...
}
sudo nginx -t && sudo systemctl reload nginx
The Next.js Server Actions limit still applies on your own server, so raise bodySizeLimit as well if uploads go through an action.
Prevention
- Make direct upload to storage the default for any file a user chooses. Route files through a function only when they are small and their size is under your control.
- Never send files as base64 inside JSON.
- Check size and type in the browser for a quick message, and enforce them on the bucket for real.
- Test uploads on a preview deployment with a 10 MB file before launch. Local tests do not hit the Vercel limit.
- Store the path in your database, not the file and not a public URL, and hand out signed URLs when someone needs to read it.
FAQ
Can I raise the 4.5 MB limit on Vercel?
By default, no. Vercel offers request bodies up to 20 MiB in public beta for Node.js, Python and Bun functions, but only with fluid compute on, Performance (4 GB) memory, which needs a Pro or Enterprise plan, and the VERCEL_SUPPORT_LARGE_REQUEST_BODY=1 environment variable. Edge runtime functions keep the 4.5 MB limit. For user uploads, direct upload to storage is still the better fix.
Why does the upload work on localhost?
next dev and next start do not apply Vercel's platform limit, so a 10 MB file goes through a Route Handler locally and fails only after deploy. The Server Actions limit of 1 MB is part of Next.js itself, so that one fails locally too.
What is the difference between Body exceeded 1 MB limit and FUNCTION_PAYLOAD_TOO_LARGE?
Body exceeded 1 MB limit comes from Next.js before your Server Action runs, and you can configure it with bodySizeLimit. FUNCTION_PAYLOAD_TOO_LARGE comes from Vercel's infrastructure before the request reaches your code at all, which is why your function logs show nothing for it.
Is a signed upload URL safe to hand to the browser?
Yes, if you create it only after checking the user. The token is tied to one bucket and one path and expires after two hours. Pick the path on the server, put the user ID in it, and set a file size limit and allowed MIME types on the bucket so the token cannot be used for something else.
Will compressing images in the browser fix it?
For photos, resizing before upload helps a lot and saves storage, but it does not fix PDFs, videos or documents. It also does nothing about base64: a file sent as a data URL inside JSON grows by about a third, so a 3.5 MB photo can already cross the 4.5 MB limit.
Related guides
- Vercel 504 FUNCTION_INVOCATION_TIMEOUT on an AI route: make the function finish in time
- Supabase storage bucket public by mistake: close it and switch to signed URLs
- Fix “new row violates row-level security policy” (Supabase error 42501)
- Next.js environment variables undefined on Vercel: NEXT_PUBLIC_, redeploys and "supabaseUrl is required"
- Cursor app Vercel build failed: fix the error that npm run dev never showed