Downloading a Document PDF
Once a document has been created, you can generate and download its PDF using a two-step process: first trigger PDF generation, then obtain a short-lived download token.
Step 1: Generate the PDF
Call the {apiCode}GetPDF mutation with the companyId and documentId. This instructs the API to enqueue PDF generation server-side. It returns true when the request was accepted and queued, or false if it was rejected.
mutation GenerateInvoicePDF($companyId: Int!, $documentId: Int!) {
invoiceGetPDF(companyId: $companyId, documentId: $documentId)
}
Example with variables:
{
"companyId": 1,
"documentId": 1234
}
A successful response:
{
"data": {
"invoiceGetPDF": true
}
}
If the mutation returns false, the document may be in draft status, missing required fields, or otherwise not ready for export. A true response only confirms that generation was queued, not that the PDF is immediately available.
Step 2: Obtain a download token
After generating the PDF, query {apiCode}GetPDFToken to receive a short-lived token and the file path:
query GetInvoicePDFToken($documentId: Int!) {
invoiceGetPDFToken(documentId: $documentId) {
data {
token
path
filename
}
errors {
msg
field
}
}
}
Example response:
{
"data": {
"invoiceGetPDFToken": {
"data": {
"token": "<token>",
"path": "/privateassets/file/molonion/2026/03/10/11/<generated-filename>.pdf",
"filename": "<filename>.pdf"
},
"errors": []
}
}
}
Step 3: Build the download URL
Combine the Moloni media base URL, the path from the token response, and the token itself as a query parameter:
https://mediaapi.moloni.org{path}?jwt={token}
Full example:
https://mediaapi.moloni.org/privateassets/file/molonion/2026/03/10/11/<generated-filename>.pdf?jwt=<token>
This URL can be used to stream or download the PDF directly, for example by opening it in a browser, using fetch, or piping it through your server.
Downloading in JavaScript
async function downloadInvoicePDF(companyId, documentId) {
// Step 1: generate the PDF
await graphqlRequest(`
mutation {
invoiceGetPDF(companyId: ${companyId}, documentId: ${documentId})
}
`);
// Step 2: obtain the token (do this immediately after)
const { data } = await graphqlRequest(`
query {
invoiceGetPDFToken(documentId: ${documentId}) {
data { token path filename }
errors { msg field }
}
}
`);
const { token, path, filename } = data.invoiceGetPDFToken.data;
// Step 3: build the URL and download
const url = `https://mediaapi.moloni.org${path}?jwt=${token}`;
const response = await fetch(url);
const buffer = await response.arrayBuffer();
// buffer now contains the PDF, save to disk, send to client, etc.
return { buffer, filename };
}
Summary
| Step | Operation | Notes |
|---|---|---|
| 1 | {apiCode}GetPDF mutation | Triggers server-side PDF generation; returns true/false |
| 2 | {apiCode}GetPDFToken query | Returns a file path and a short-lived JWT token (~10 s TTL) |
| 3 | GET https://mediaapi.moloni.org{path}?jwt={token} | Download the PDF from the media server |
Next steps
- Querying Documents: Retrieve document IDs and metadata
- Document Types: Understand type-specific mutation and query names
- Creating an Invoice: Create documents to download