This code makes a POST request to the DocsFold API with the required headers and request body to generate a PDF file from the specified template and overrides.
You need to replace YOUR_API_KEY
with your actual API key and YOUR_TEMPLATE_ID
with your actual template ID.
This code example includes parsing the JSON response and downloading the PDF file from the specified URL. The PDF file is then saved to disk with the name "invoice.pdf".
Note that you'll need to adjust the file path as needed for the output PDF.
Code Example using https
const https = require('https');const fs = require('fs');const API_KEY = 'YOUR_API_KEY';const TEMPLATE_ID = 'YOUR_TEMPLATE_ID';const data = JSON.stringify({ template: TEMPLATE_ID, output_format: 'url', overrides: { invoice_nr: '123', created_date: 'January 1, 2022', due_date: 'February 1, 2022', company_name: 'Acme Corp', company_address: 'Acme road, 1', company_postal_code: 'Sunnyville, 1000', client_company_name: 'Doe Corp', client_name: 'John Doe', client_email: 'john@doe.com', payment_method: 'Check', payment_method_value: 1000, items: [ { name: 'Website design', value: 300 }, { name: 'Hosting (3 months)', value: 75 }, { name: 'Domain name (1 year)', value: 10 } ] }});const options = { hostname: 'app.docsfold.com', path: '/api/v1/generate-pdf', method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${API_KEY}`, 'Content-Length': data.length }};const req = https.request(options, (res) => { let response = ''; res.on('data', (d) => { response += d; }); res.on('end', () => { if (res.statusCode !== 200) { console.error(`Request failed with status code ${res.statusCode}, message "${res.statusMessage}" and response body "${response}"`); return; } const json = JSON.parse(response); if (json.status === 'SUCCESS') { const pdfUrl = json.pdf_url; https.get(pdfUrl, (res) => { res.pipe(fs.createWriteStream('invoice.pdf')); }); } else { console.error(`Error generating PDF: ${json.message}`); } });});req.on('error', (error) => { console.error(error);});req.write(data);req.end();
Code Example using axios
You can also use the axios library to make the request.
Note that you'll need to install the axios library first:
npm install axios
const https = require('https');const fs = require('fs');const API_KEY = 'YOUR_API_KEY';const TEMPLATE_ID = 'YOUR_TEMPLATE_ID';const url = 'https://app.docsfold.com/api/v1/generate-pdf';const data = JSON.stringify({ template: TEMPLATE_ID, output_format: 'url', overrides: { invoice_nr: '123', created_date: 'January 1, 2022', due_date: 'February 1, 2022', company_name: 'Acme Corp', company_address: 'Acme road, 1', company_postal_code: 'Sunnyville, 1000', client_company_name: 'Doe Corp', client_name: 'John Doe', client_email: 'john@doe.com', payment_method: 'Check', payment_method_value: 1000, items: [ { name: 'Website design', value: 300 }, { name: 'Hosting (3 months)', value: 75 }, { name: 'Domain name (1 year)', value: 10 } ] }});axios.post(url, data, { headers: { 'Authorization': `Bearer ${API_KEY}`, 'Content-Type': 'application/json' }, responseType: 'json'}) .then(response => { if (response.status === 200) { const pdfUrl = response.data.pdf_url; axios.get(pdfUrl, { responseType: 'arraybuffer' }) .then(response => { fs.writeFile('invoice.pdf', response.data, err => { if (err) { console.error(err); } else { console.log('PDF saved to invoice.pdf'); } }); }) .catch(error => { console.error(error); }); } else { console.error(`Error generating PDF: ${response.data.message}`); } }) .catch(error => { console.error(error); });