Document Generation API: How to Generate PDFs Programmatically
Every product eventually needs to spit out a document — an invoice when a subscription renews, a contract when a deal closes, a certificate when a course completes, a report when a job finishes. The naive answer is "render some HTML and run a headless browser to make a PDF." Then you're maintaining a Chromium binary, fighting page-break CSS, hosting fonts, and rebuilding a templating layer your non-developers can't touch. A document generation API replaces all of that with one HTTP call: you POST your data, you get back a finished, branded PDF (or DOCX, HTML, Markdown, JSON). This guide explains what a document generation API is, when to use one instead of rolling your own, and exactly how to call the GJSDocs API — auth, request shape, response, and working code.
[Hero image alt text: A code editor with a POST request on the left and a rendered branded PDF on the right, illustrating programmatic document generation.]
What a document generation API does
A document generation API is an endpoint that takes a template ID plus a bag of variables and returns a finished document. The template — layout, branding, fonts, page rules — is designed once in a visual editor by whoever owns the design. Your code never deals with HTML-to-PDF rendering, page breaks, or font embedding. It sends structured data; the service returns the file.
That separation is the whole point. Designers and ops people maintain the template without touching code; engineers maintain the data pipeline without touching design. A change to the invoice footer is a template edit, not a deploy.
When to use an API vs rolling your own
Building PDF generation in-house looks cheap until you list what it actually involves: a headless browser or a PDF library, a templating engine, font management, page-break handling, image optimization, a way for non-engineers to edit templates, and an ongoing maintenance burden every time Chromium or the library changes. Reach for an API when:
- Non-developers need to own the document design and change it without a deploy.
- You output more than one or two document types and don't want a bespoke renderer for each.
- Branding and pixel-accurate layout matter (invoices, contracts, certificates that represent your company).
- You need multiple output formats — PDF for sending, DOCX for editing, HTML for embedding — from one source.
- You'd rather not own the security and scaling of a headless-browser fleet.
One thing worth calling out about GJSDocs specifically: the full REST API ships on every paid plan, starting at the Starter tier — it isn't gated behind an enterprise contract the way many document tools gate their API. See the pricing page for the per-tier rate limits.
Step 1 — Authenticate with an API key
Generate an API key in your account settings and send it as a Bearer token on every request. Treat it like a password — keep it server-side, in an environment variable, never in client-side code or a committed file.
Authorization: Bearer gjs_live_xxxxxxxxxxxxxxxxxxxx
Content-Type: application/json
Step 2 — Call the generate endpoint
The core endpoint takes a template_id, a target format, and a variables object whose keys match the variables you placed in the template. See the documentation for the full endpoint reference.
// Request
POST https://gjsdocs.com/api/documents/generate
{
"template_id": "tpl_invoice_v1",
"format": "pdf",
"variables": {
"client": { "name": "Northwind Trading Ltd", "email": "[email protected]" },
"invoice": { "number": "INV-2026-0473", "total": 5040.00 }
}
}
A successful response returns the document URL and metadata:
// 200 OK
{
"success": true,
"data": {
"document_id": "doc_9f2a...",
"format": "pdf",
"url": "https://gjsdocs.com/files/doc_9f2a....pdf",
"bytes": 48213
},
"error": null
}
The supported format values are pdf, docx, html, md, and json — same template, different output, one parameter.
Step 3 — Call it from your code
From the command line with cURL:
curl -X POST https://gjsdocs.com/api/documents/generate \
-H "Authorization: Bearer $GJSDOCS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "template_id": "tpl_invoice_v1", "format": "pdf",
"variables": { "client": { "name": "Northwind Trading Ltd" } } }'
From Node.js with fetch:
async function generateInvoice(invoice) {
const res = await fetch(
"https://gjsdocs.com/api/documents/generate",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.GJSDOCS_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
template_id: "tpl_invoice_v1",
format: "pdf",
variables: invoice,
}),
}
);
if (!res.ok) throw new Error(`Generate failed: ${res.status}`);
const { data } = await res.json();
return data.url;
}
That's the whole integration. No headless browser, no PDF library, no font hosting — your code is responsible for the data and nothing else.
3 ways teams use the API
1. Generate on a webhook
A subscription renews, a deal moves to "won," a form is submitted — your webhook handler fires one generate call and attaches the resulting PDF to the email or record. No-code teams do the same thing through Zapier without writing the handler.
2. Batch generation
Loop over a dataset — every active customer, every cohort graduate — and fire generate calls concurrently with sensible rate limiting. End-of-month invoicing or a whole cohort's certificates in one job.
3. Embed in your product
Give your own users a "Download PDF" button. The click hits your backend, your backend calls generate, and the user gets a branded document — built on templates your team controls, not a renderer you maintain.
Errors, rate limits, and idempotency
Responses follow a consistent envelope — success, data, error — so you check one field. Handle the common failure cases explicitly: 401 (bad or missing key), 404 (unknown template_id), 422 (a variable the template requires is missing), and 429 (rate limit — back off and retry). Each paid tier carries a different document allowance and request rate, so design batch jobs to respect the ceiling rather than hammering it.
FAQ
Do I need the API to use GJSDocs?
No. You can build templates and generate documents entirely in the visual editor, or drive generation from a spreadsheet with no code at all (see how to generate a PDF from a template). The API is for when generation needs to be triggered by your own systems.
Which output formats does the API support?
PDF, DOCX, HTML, Markdown, and JSON — set by the format field. The same template can produce any of them, so you can return a PDF to send and a DOCX to edit from one definition.
Is the API available on the cheapest paid plan?
Yes. The full REST API surface — documents, variables, templates, and generation — ships on every paid plan starting with Starter. Higher tiers raise the rate limits and document allowance rather than unlocking new endpoints.
Related reading:
Get an API key on the free trial
Every new account starts on a 14-day Pro trial. Build a template, grab your key, and generate your first document in under an hour. No credit card.
Get started