Node.js Example
First, sign up for the basic plan for a free development API key. Please replace API_KEY in the code example below with your own API key.
This example requires Node.js 18 or later (fetch is built in). Follow these steps to create a sample project:
mkdir ZplTest
cd ZplTest
npm init --yes
Create a new file zpl.js with the following content:
const fs = require('fs');
const API_KEY = 'API_KEY';
async function main() {
const html =
'<h1 style="font-size:40pt;margin:0.5in">Hello World!</h1>' +
'<p style="font-size:25pt;margin:0.8in">ZPL label generated by <u>htmltozpl.com</u></p>';
const response = await fetch('https://html-to-zpl.p.rapidapi.com/html2zpl', {
method: 'POST',
headers: {
'x-rapidapi-host': 'html-to-zpl.p.rapidapi.com',
'x-rapidapi-key': API_KEY,
'content-type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
width: '4',
height: '2',
html,
}),
});
if (!response.ok) {
throw new Error(`API error ${response.status}: ${await response.text()}`);
}
const zpl = await response.text();
fs.writeFileSync('node-html-to-zpl-example.zpl', zpl);
console.log(zpl);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
Run it by typing:
node zpl.js
You should see the resulting ZPL written on the console, and also saved to the file node-html-to-zpl-example.zpl.