How to send a PDF to an API as Base64
An e-invoice, e-signature or shipping API wants the document in a JSON field. Here is how to turn the PDF into the right kind of text, check it and avoid the errors that make the request fail.
By Metin Ersoy · Updated November 10, 2026
What the API usually expects
Most document APIs describe a JSON body with a field called content, file, data or document. Its value is the Base64 code of the PDF, often next to a file name and a type:
{
"fileName": "invoice-1024.pdf",
"mimeType": "application/pdf",
"content": "JVBERi0xLjcKJ..."
}
Before writing any code, read the documentation for three things: the field name, whether a data: prefix is wanted (usually not) and the size limit.
1. Turn the PDF into Base64
The quickest way to get a test value is PDF to Base64. Choose the PDF, pick Base64 code only and copy the result. If you want a whole request body to start from, pick JSON instead — it adds the file name and type for you. The PDF is encoded in your browser and is not uploaded, which matters when the test document is a real invoice.
Check the result: it should start with JVBERi0, contain no spaces and, apart from one or two = signs at the end, only letters, digits, + and /.
2. Send it from your code
The same step in three common languages. Each one reads the file, encodes it and posts the JSON:
# curl (Linux / macOS): base64 -w 0 keeps it on one line
curl -X POST https://api.example.com/documents \
-H "Content-Type: application/json" \
-d "{\"fileName\":\"invoice.pdf\",\"content\":\"$(base64 -w 0 invoice.pdf)\"}"
// Node.js 18+
import { readFile } from 'node:fs/promises';
const content = (await readFile('invoice.pdf')).toString('base64');
await fetch('https://api.example.com/documents', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ fileName: 'invoice.pdf', content }),
});
# Python 3 with requests
import base64, requests
with open("invoice.pdf", "rb") as f:
content = base64.b64encode(f.read()).decode("ascii")
requests.post("https://api.example.com/documents",
json={"fileName": "invoice.pdf", "content": content})
On macOS the curl line uses base64 -i invoice.pdf instead of -w 0. The address and field names are examples; use the ones from your service's documentation.
3. Check the round trip
If the service returns the document or a stored copy as Base64, paste that answer into Base64 to PDF. Seeing the first page and the page count is the fastest proof that nothing was lost on the way. The detailed steps for that direction are in How to save a Base64 string as a PDF file.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| "Invalid Base64" | Line breaks (76-character lines) or a data: prefix | Send the bare code on one line |
| "Payload too large" (413) | The PDF is too big after encoding | Shrink it with Compress PDF |
| The PDF opens blank or broken | The string was cut off | Encode again and send the whole value |
| Accented names look wrong | File name sent in the wrong encoding | Keep the JSON body in UTF-8 |
Ready to try it? It is free and your file stays on your device.
Open PDF to Base64