JavaScript SDK
Printivo JavaScript SDK
Generate QR codes, render ZPL labels, and create barcodes with a Promise-based API. Full TypeScript support. Works in Node.js and browser environments.
Installation
Install the Printivo JavaScript SDK via npm or yarn.
npm
yarn
pnpm
# npm
npm install printivo
# yarn
# yarn add printivo
# pnpm
# pnpm add printivoRequirements
- Node.js 16+ or modern browser with Fetch API
- A Printivo API key (get one free)
Quick Start
Initialize the client and make your first API call.
ES Modules
CommonJS
import { PrintivoClient } from 'printivo';
// Initialize the client
const client = new PrintivoClient({
apiKey: 'your_api_key',
baseUrl: 'https://api.printivo.cloud', // optional, this is the default
});
// Or with minimal config
const client = new PrintivoClient('your_api_key');
// CommonJS
// const { PrintivoClient } = require('printivo');Generate QR Codes
Create QR codes with async/await and full TypeScript type safety.
Basic
Styled
WiFi
import { PrintivoClient } from 'printivo';
import { writeFile } from 'fs/promises';
const client = new PrintivoClient('your_api_key');
// Generate a URL QR code
const qrBuffer = await client.qrCodes.generate({
content: 'https://example.com',
contentType: 'Url',
size: 400,
format: 'png',
errorCorrection: 'M',
});
// Save to file (Node.js)
await writeFile('qrcode.png', qrBuffer);
// Generate a styled QR code
const styledQr = await client.qrCodes.generate({
content: 'https://example.com',
contentType: 'Url',
size: 600,
format: 'svg',
foregroundColor: '#1e40af',
backgroundColor: '#ffffff',
moduleShape: 'Rounded',
errorCorrection: 'H',
});
await writeFile('styled-qr.svg', styledQr);Render ZPL Labels
Convert ZPL code to PNG or PDF with a single method call.
Render Label
// Render a ZPL label
const labelImage = await client.labels.render({
zpl: `^XA
^FO50,50^A0N,40,40^FDOrder #12345^FS
^FO50,120^BY3^BCN,100,Y,N,N^FDSHIP-2024-001^FS
^FO50,260^FDJane Smith^FS
^FO50,310^FD456 Oak Avenue^FS
^FO50,360^FDSan Francisco, CA 94102^FS
^XZ`,
dpi: 203,
format: 'png',
width: 4,
height: 6,
});
await writeFile('shipping-label.png', labelImage);
// Use in an Express.js route
app.get('/label/:orderId', async (req, res) => {
const zpl = await getZplForOrder(req.params.orderId);
const image = await client.labels.render({
zpl,
dpi: 203,
format: 'png',
width: 4,
height: 6,
});
res.contentType('image/png').send(image);
});Generate Barcodes
Create barcodes of any supported symbology.
EAN-13
Code 128
// Generate an EAN-13 barcode
const ean13 = await client.barcodes.generate({
data: '1234567890128',
type: 'EAN13',
width: 300,
height: 150,
showText: true,
format: 'png',
});
await writeFile('ean13.png', ean13);
// Generate a Code 128 barcode
const code128 = await client.barcodes.generate({
data: 'SHIP-2024-001',
type: 'Code128',
width: 400,
height: 100,
showText: true,
});
await writeFile('code128.png', code128);Error Handling
The SDK provides typed error classes for predictable error handling.
Error Handling
import {
PrintivoAuthError,
PrintivoRateLimitError,
PrintivoValidationError,
PrintivoApiError,
} from 'printivo';
try {
const qr = await client.qrCodes.generate(request);
} catch (error) {
if (error instanceof PrintivoAuthError) {
// Invalid or missing API key (401)
console.error('Check your API key.');
} else if (error instanceof PrintivoRateLimitError) {
// Rate limit exceeded (429)
console.error(`Rate limited. Retry after ${error.retryAfter}s`);
} else if (error instanceof PrintivoValidationError) {
// Invalid request (400)
error.errors.forEach(e =>
console.error(`${e.field}: ${e.message}`)
);
} else if (error instanceof PrintivoApiError) {
// Other API errors
console.error(`API error: ${error.statusCode} - ${error.message}`);
}
}Browser Usage
The SDK works in the browser too. Display generated images directly in the DOM.
Browser
import { PrintivoClient } from 'printivo';
const client = new PrintivoClient('your_api_key');
// Generate QR code and display in an img tag
async function displayQrCode(content) {
const qrBuffer = await client.qrCodes.generate({
content,
contentType: 'Url',
size: 300,
format: 'png',
});
// Convert to blob URL
const blob = new Blob([qrBuffer], { type: 'image/png' });
const url = URL.createObjectURL(blob);
document.getElementById('qr-image').src = url;
}
// Call on button click
document.getElementById('generate-btn')
.addEventListener('click', () => {
displayQrCode('https://example.com');
});Start Building with JavaScript
Install the npm package and start generating QR codes, labels, and barcodes in minutes.