Quick Start for Node.js
Integrate PDFBolt's REST API in Node.js to generate PDFs from URLs, HTML, or templates. The examples below cover all three conversion modes (Direct, Sync, Async).
1. Get Your API Key
Find your API key on the API Keys page in your Dashboard. If you don't have an account, sign up – the free plan includes 100 document conversions per month.
2. Make Your First Request
Any HTTP client works – adjust the request structure to match your library. Examples use native fetch (Node.js 18+).
Choose your endpoint:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
Choose your source:
- URL
- HTML
- Template
Convert a webpage to PDF:
const fs = require('fs');
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/direct', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com',
format: 'A4',
printBackground: true
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync('webpage.pdf', Buffer.from(pdfBuffer));
console.log('PDF generated successfully');
}
generatePdf().catch(console.error);
Convert HTML to PDF (HTML must be base64 encoded):
const fs = require('fs');
async function generatePdf() {
const htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
const base64Html = Buffer.from(htmlContent).toString('base64');
const response = await fetch('https://api.pdfbolt.com/v1/direct', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: base64Html,
format: 'A4',
printBackground: true,
margin: {
top: '30px',
left: '30px'
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync('document.pdf', Buffer.from(pdfBuffer));
console.log('PDF generated successfully');
}
generatePdf().catch(console.error);
Convert a template with data to PDF:
const fs = require('fs');
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/direct', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
templateId: 'your-template-id',
templateData: {
client_name: 'John Doe',
invoice_number: 'INV-001',
total_amount: '$299.99',
line_items: [
{ description: 'Web Development', unit_price: '$200.00' },
{ description: 'Design Services', unit_price: '$99.99' }
]
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync('invoice.pdf', Buffer.from(pdfBuffer));
console.log('PDF generated successfully');
}
generatePdf().catch(console.error);
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
The Sync endpoint returns a JSON response with a download URL for the PDF (valid for 24 hours).
Choose your source:
- URL
- HTML
- Template
Convert a webpage and get a download URL:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/sync', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com',
format: 'A4',
printBackground: true
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('PDF URL:', result.documentUrl);
}
generatePdf().catch(console.error);
Convert HTML and get a download URL (HTML must be base64 encoded):
async function generatePdf() {
const htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
const base64Html = Buffer.from(htmlContent).toString('base64');
const response = await fetch('https://api.pdfbolt.com/v1/sync', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: base64Html,
format: 'A4',
printBackground: true,
margin: {
top: '30px',
left: '30px'
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('PDF URL:', result.documentUrl);
}
generatePdf().catch(console.error);
Convert a template with data and get a download URL:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/sync', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
templateId: 'your-template-id',
templateData: {
client_name: 'John Doe',
invoice_number: 'INV-001',
total_amount: '$299.99',
line_items: [
{ description: 'Web Development', unit_price: '$200.00' },
{ description: 'Design Services', unit_price: '$99.99' }
]
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('PDF URL:', result.documentUrl);
}
generatePdf().catch(console.error);
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
The Async endpoint returns a requestId immediately and delivers the final result via webhook callback.
Choose your source:
- URL
- HTML
- Template
Convert a webpage and receive a webhook callback:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/async', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com',
format: 'A4',
printBackground: true,
webhook: 'https://your-app.com/webhook'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Request ID:', result.requestId);
console.log('PDF will be sent to webhook when ready');
}
generatePdf().catch(console.error);
Convert HTML and receive a webhook callback (HTML must be base64 encoded):
async function generatePdf() {
const htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
const base64Html = Buffer.from(htmlContent).toString('base64');
const response = await fetch('https://api.pdfbolt.com/v1/async', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: base64Html,
format: 'A4',
printBackground: true,
margin: {
top: '30px',
left: '30px'
},
webhook: 'https://your-app.com/webhook'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Request ID:', result.requestId);
console.log('PDF will be sent to webhook when ready');
}
generatePdf().catch(console.error);
Convert a template with data and receive a webhook callback:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/async', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
templateId: 'your-template-id',
templateData: {
client_name: 'John Doe',
invoice_number: 'INV-001',
total_amount: '$299.99',
line_items: [
{ description: 'Web Development', unit_price: '$200.00' },
{ description: 'Design Services', unit_price: '$99.99' }
]
},
webhook: 'https://your-app.com/webhook'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Request ID:', result.requestId);
console.log('PDF will be sent to webhook when ready');
}
generatePdf().catch(console.error);
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
Next Steps
📄️ API Endpoints
📄️ Conversion Parameters
📄️ Error Handling
📄️ Templates
- How to Convert HTML to PDF Using an API – complete Node.js tutorial with EJS templates and invoice example.
- How to Generate Invoice PDFs with an API – invoice automation with a Node.js example.