Extract a PDF from API JSON Response: Base64 Workflow
Extract a PDF from an API JSON response when the file is returned as Base64. Includes validation steps, save patterns, and common field mistakes.
A surprising number of APIs do not return PDFs as direct file downloads. Instead, they wrap the document in JSON as a Base64 string. That makes transport easy for the backend — but adds work for the developer who needs the actual file.
TL;DR: Find the field containing the Base64 payload, decode only that value, then save the output as
Typical API shape
{
"documentId": "doc_123",
"fileName": "receipt.pdf",
"mimeType": "application/pdf",
"content": "JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+Pg=="
}
The critical part is identifying the correct field. It might be content, data, payload, file, or documentBase64.
Fastest manual workflow
- Copy the Base64 field only
- Paste it into GoGood.dev Base64 Converter
- Download the result
- Confirm it opens as a PDF
This is the fastest way to check whether the backend returned valid data.
JavaScript extraction example
const response = await fetch('/api/documents/123');
const payload = await response.json();
const base64Pdf = payload.content;
Then pass base64Pdf into your browser decode/download function.
Python extraction example
import base64
import requests
payload = requests.get('https://api.example.com/documents/123').json()
pdf_bytes = base64.b64decode(payload['content'])
with open(payload['fileName'], 'wb') as f:
f.write(pdf_bytes)
Common traps
You copied the whole JSON object
Only the Base64 field should be decoded.
The API returns a data URI, not raw Base64
If the field starts with data:application/pdf;base64,, strip the prefix first.
The MIME type does not match the actual file
Do not trust labels blindly. Validate by decoding and opening the file.
FAQ
Why do APIs return PDFs as Base64 in JSON?
Because it is easy to transport binary data through text-based APIs.
How do I know which field contains the PDF?
Look for large string fields with names like data, content, file, or values starting with JVBER....
Should I store the Base64 string or the PDF bytes?
Usually the PDF bytes or file object are better long term. Base64 is mostly for transport.
If an API gives you a PDF inside JSON, the job is simpler than it looks: isolate the right field, decode it, save it. For quick validation, start with GoGood.dev Base64 Converter.
Related: How to Convert Base64 to PDF Online · How to Convert Base64 to PDF in JavaScript · How to Convert Base64 to PDF in Python · How to Convert Base64 Back to a File · How to Convert an Image to Base64 · When to Use Base64