Using SuperAgent in Node
SuperAgent is a feature-rich HTTP client for Node.js and the browser. It provides an intuitive API for making HTTP requests and handling responses. Superagent supports features like request chaining, form data encoding, and streaming responses.
We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and Node:
const superagent = require('superagent');
async function convert(api_key, params, endpoint='pdf') {
if (!['pdf', 'png', 'jpg', 'webp'].includes(endpoint)) {
throw new Error('Invalid endpoint');
}
let response = await superagent
.post(`https://api.pdfshift.io/v3/convert/${endpoint}`)
.auth('api', api_key)
.send(params);
if ('filename' in params || 'webhook' in params) {
// Return in JSON in that case
return response.body;
}
// Returns the binary content
return response.body;
}
Here's how you can use the above code:
We've tested this code with the latest version of SuperAgent and it's ready to be used in your project.
But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.