Data URI vs Raw Base64: Size Expansion & API Gateway Limits
Base64 encoding inflates every binary file by 33.3% before JSON wrappers or Data URI prefixes are even added. Here is the exact math behind Vercel 4.5 MB and AWS 6 MB payload failures—and how to stay green.
The Deterministic +33.33% Expansion Math & Gateway Hard Caps
Calculate exact encoded byte budgets beforehand so a 3.6 MB PDF never surprises production services with an HTTP 413 Payload Too Large rejection.
Read full technical details
Base64 encodes every 3 raw binary octets (24 bits) into 4 printable 6-bit ASCII characters. Mathematically, output length equals Math.ceil(rawBytes / 3) * 4—a deterministic +33.33% size inflation before adding optional RFC 2045 MIME-76 line breaks (+2.6%), Data URI prefixes (22–85 bytes), or JSON object keys.
Because serverless platforms evaluate the complete uncompressed HTTP body arriving at the edge, a modest 3.5 MB binary invoice turns into ~4.67 MB once wrapped in Base64 JSON. That immediately exceeds Vercel Serverless Functions’ hard 4.5 MB (4,718,592-byte) body cap and triggers HTTP 413 Payload Too Large before handler code runs.
AWS API Gateway REST endpoints with Lambda proxy integration cap payloads at 6 MB (~4.45 MB raw binary ceiling), while AWS HTTP APIs cap requests at 10 MB (~7.45 MB raw ceiling). Knowing the exact 0.74x raw-to-limit ratio prevents silent integration breakages.
Pre-Encode Slimming vs Pre-Signed Binary Direct Uploads
Shrink PDFs by page range or convert screenshots to WebP to stay green inside JSON webhooks—or switch cleanly to R2/S3 binary uploads past 5 MB.
Read full technical details
Crossing a red budget badge does not always require rewriting your API contract. Many 40-page scanned contracts only need pages 1–2 attached, and 4000 px phone photos shrink 70%–90% when downscaled to 1280 px WebP before encoding—bringing the entire Base64 JSON request safely under 1 MB.
Keep raw Base64 inside JSON when payloads stay below ~3 MB raw and benefit from atomic single-request webhook delivery (e.g., thermal labels, signed XML receipts, or tiny UI icons). Whenever documents regularly exceed 5–8 MB (videos, multi-megabyte archives, high-DPI print masters), bypass Base64 JSON entirely and issue short-lived pre-signed object-storage PUT URLs or multipart/form-data streams.
Also measure whether HTTP Gzip or Brotli helps your payload class: text formats like raw SVG, CSV, and JSON shrink meaningfully over compressed TLS, whereas already-compressed PDFs, PNGs, JPEGs, and .xlsx files gain under 2% over the wire.
Production Reference Code
// Exact encoded byte budget before API transport
const base64Bytes = Math.ceil(rawFileBytes / 3) * 4; // +33.3% expansion
const mimeWrappedBytes = base64Bytes + Math.floor(base64Bytes / 76) * 2; // +2.6% if MIME-76
const vercelLimitBytes = 4.5 * 1024 * 1024; // 4,718,592 B (max raw file ~3.37 MiB)Step-by-Step Local Workflow
Divide Gateway Limit by 1.34
Compute true raw binary capacity upfront (e.g., ~3.35 MB max raw file for a 4.5 MB Vercel Serverless Function).
Omit MIME-76 Line Breaks in APIs
Keep Base64 unwrapped on a single line for JSON bodies and Data URIs to avoid unnecessary +2.6% CRLF overhead.
Slim PDFs or Compress Images First
Trim unneeded PDF pages or convert bulky PNG/JPEG images to WebP in Base64Doc before encoding.
Move >5 MB Files to Binary Multipart
Switch to multipart/form-data or pre-signed S3/R2 uploads whenever compressed payloads remain in the red zone.
Test Directly in Browser Workbench
Frequently Asked Questions
Why does Base64 always add about 33.3% to file size?
Every 3 binary bytes (24 bits) become 4 ASCII characters carrying 6 bits each, producing a fixed mathematical expansion ratio of 4/3 = 1.3333.
What is the largest raw PDF I can safely send inside a Vercel 4.5 MB JSON body?
Keep raw PDFs at or below ~3.35 MB, because 3.35 MB x 1.333 plus JSON field names sits just under 4.5 MB (4,718,592 bytes).
Does Gzip-compressing an already compressed PDF or JPEG before Base64 reduce size?
No, PDF streams, PNGs, JPEGs, and ZIP archives are already compressed; pre-Gzipping saves meaningful bytes only on plain JSON, XML, CSV, or SVG text.
Is a Data URI larger than raw Base64?
Yes, by the length of the data:<mime>;base64, prefix (typically 22 to 85 bytes), which is negligible for PDFs but noticeable on tiny sub-kilobyte icons.
Where can I check if my file clears Vercel and AWS limits before deploying?
Encode your file on Base64Doc (/pdf-to-base64 or /image-to-base64) to view live green/yellow/red traffic lights for Vercel 4.5 MB, AWS 6 MB, and AWS 10 MB.