How to integrate PDF Generator API with n8n Cloud
From Data to Document in 5 Minutes: Your First n8n - PDF Generator API Workflow
Introduction
The true power of automation platforms like n8n is unlocked when you connect them to specialized external services like PDF Generator API. If you've ever needed to dynamically create invoices, reports, or certificates, the PDF Generator API is an excellent tool for the job. In this tutorial, we will build a simple but practical n8n workflow that authenticates and makes an HTTP request to the PDF Generator API, allowing you to create custom documents automatically. The process involves two key nodes: one to generate a JWT with JavaScript and another to make the final API call.
Code Node
1: Generating a JWT with JavaScript
Our first node in this n8n workflow will be dedicated to generating this required token. We'll use the versatile Code node to run a small JavaScript snippet that creates the JWT for us.
- On the n8n canvas, click the
+
icon to add the first node to your workflow.
- In the "Search nodes..." dialog, type
Code
and select the Code node from the list. This node is incredibly powerful as it lets you run custom JavaScript or Python code at any point in your workflow.
- With the Code node selected, ensure the Language parameter is set to
JavaScript
.
- Next, copy the complete JavaScript snippet for generating the JWT and paste it into the JavaScript Code editor panel.
(Paste your JavaScript snippet here in your article)
- The provided script is designed to perform the authentication logic and, most importantly, to return a JSON object. In n8n, the data returned by a node becomes its output, which can then be used as input by all subsequent nodes. In this case, the code will output an object containing our newly created token.
jwt_token
property.Code Sample for JSON Web Token (JWT) Generation in JavaScript
const crypto = require('crypto');
const apiKey = 'REPLCE_WITH_YOUR_API_KEY';
const apiSecret = 'REPLCE_WITH_YOUR_SECRET';
const workspace = 'REPLCE_WITH_YOUR_WORKSPACE_IDENTIFIER';
// 1. Create the JWT Header and Payload
const header = {
alg: 'HS256',
typ: 'JWT'
};
const payload = {
iss: apiKey,
sub: workspace,
exp: Math.floor(Date.now() / 1000) + (1 * 60) // Token expires in 60 second
};
// Helper function to Base64Url encode data
const base64UrlEncode = (data) => {
return Buffer.from(JSON.stringify(data))
.toString('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
};
const encodedHeader = base64UrlEncode(header);
const encodedPayload = base64UrlEncode(payload);
// 2. Create the signature using the correct crypto function
// This is what the Crypto node was failing to do.
const signatureInput = `${encodedHeader}.${encodedPayload}`;
// The 'crypto' module is globally available in n8n Function nodes
const signature = crypto
.createHmac('sha256', apiSecret)
.update(signatureInput)
.digest('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
// 3. Combine the parts into the final JWT
const finalJwt = `${signatureInput}.${signature}`;
// 4. Return the result for the next n8n node to use
return [{
json: {
jwt: finalJwt
}
}];
HTTP request
2: Making the HTTP Request to Generate the PDF
Now that our workflow can generate an authentication token, we need to use it to make a successful API call. This is where the HTTP Request node comes in. We will configure this node to send our document data and our JWT to the PDF Generator API, which will then return the finished PDF file.
- Click the
+
icon on the Code node you just created to add the second step to the workflow.
- Search for and select the HTTP Request node.
- Now, we need to configure the node's parameters:
- Method: Set this to
POST
, as we are sending data to the API to create a new resource (our PDF). - URL: Paste the API endpoint for the PDF Generator API's document generation service.
https://us1.pdfgeneratorapi.com/api/v4/documents/generate
- Next, we'll handle authentication. This is the most critical step.
- Under the Authentication dropdown, select
None
. - Switch the Send Headers toggle.
- In the Name field, enter
Authorization
. This is the standard header for sending authentication credentials. - In the Value field, we need to dynamically insert the token from our first node.
- Click the "Expressions" icon (
</>
). - In the expression editor, type
Bearer
(with a space at the end). - After the space, drag the
jwt_token
value from the Input Data > JSON section on the left. This links the output of the first node to the input of the second. - The final expression should look like this:
Bearer {{$json["jwt_token"]}}
- This tells n8n to construct an Authorization header with the word "Bearer" followed by the token generated in the Previous Step.
- In the expression editor, type
Content-Type: application/json
header is included to inform the API that the data we are sending in the request's body is structured in JSON format, ensuring the server can correctly interpret it.Finally, we need to specify the document we want to create.
At the bottom of the node, click on the Body tab.
Set the Body Content Type to JSON
.
In the Body field, paste the JSON payload that defines your document. This includes the template ID and the data you want to merge.
Example JSON payload
{
"template": {
"id": "REPLACE_WITH_TEMPLATE_ID",
"data": {
"id": 123,
"name": "John Smith",
"birthdate": "2000-01-01",
"role": "Developer"
}
},
"format": "pdf",
"output": "url",
"name": "My First Document from PDFGeneratorAPI",
"testing": false
}
Executing the Workflow and Viewing Your PDF
With both the Code node and the HTTP Request node fully configured, your automated document generation workflow is now complete. It's time to run the entire process from start to finish and view the generated document.
Click the Execute Workflow button located in the bottom-centre of the n8n canvas.
You will see n8n execute each node in sequence. First, the Code node will run, generating the JWT. Then, the HTTP Request node will take that token and the JSON data, make the POST request to the PDF Generator API, and receive a response. If everything was configured correctly, both nodes will have a green border, indicating a successful run.
Related articles you may find helpful
- How is the monthly usage calculated (merges)
- How to share templates with other workspaces?
- What is Expression Language?
Updated on: 12/06/2025
Thank you!