Skip to content
getbizprint.com

Use the BizPrint Node.js SDK to create and check print jobs

This page shows you how to use the BizPrint Node.js SDK, published on npm as @bizswoop/print-client, to call the BizPrint Print API from your own server. It is for developers who build their own app or store integration in Node.js or TypeScript, and it works with both the desktop app and the Android app as stations.

The SDK signs every request for you, so you do not need to follow Sign BizPrint Print API requests by hand.

  1. Open a terminal in your project folder.

  2. Run this command:

    Terminal window
    npm install @bizswoop/print-client
import PrintClient from '@bizswoop/print-client';
// Set these on your server to YOUR_PUBLIC_KEY and YOUR_SECRET_KEY.
const client = new PrintClient({
publicKey: process.env.BIZPRINT_PUBLIC_KEY,
secretKey: process.env.BIZPRINT_SECRET_KEY,
});

If your project uses require instead of import, load the client like this:

const PrintClient = require('@bizswoop/print-client').default;

The SDK includes TypeScript types, so you do not need to install them separately. This page covers client.Printers and client.Jobs.

Each print job goes to one printer, by its ID. List your printers to find it:

const printers = await client.Printers.list({ perPage: 20, page: 1 });
for (const printer of printers.data) {
console.log(printer.id, printer.name, printer.status, printer.station.name);
}

To get one printer, use client.Printers.retrieve(YOUR_PRINTER_ID). For the fields, see List printers with the BizPrint Print API.

const job = await client.Jobs.create({
printerId: YOUR_PRINTER_ID,
url: 'https://example.com/receipts/1001.pdf?token=LONG_RANDOM_TOKEN',
description: 'Order 1001',
});
console.log(job.id, job.status);
  • printerId is the printer’s ID, as a number.
  • url is the address the station downloads and prints. Send a PDF to printers on desktop stations. Printers on Android stations accept a PDF, an HTML page or plain text, and label printers also accept ZPL or TSPL label code in their own printer language. See Serve the document to print with the BizPrint Print API (print by URL).
  • description is a short name for the job, such as the order number. Make it unique to the job, so you can find the job again later.
  • printOption is optional. See Create a print job with the BizPrint Print API.

Save job.id. You need it to check the job later.

const job = await client.Jobs.retrieve(savedJobId);
console.log(job.status);

For what each status means, see Check print job status with the BizPrint Print API.

A list call returns one page, with data, hasMore, totalAll and totalPages. To go through every page, use for await. Each loop gives you one page:

let allJobs = [];
for await (const page of client.Jobs.list({ perPage: 50 })) {
allJobs = allJobs.concat(page.data);
}

Jobs are listed oldest first. To see your newest jobs:

  1. Ask for the first page, for example with client.Jobs.list({ perPage: 50 }).
  2. Read totalPages from it. If it is 0, there are no jobs to list.
  3. Ask for the last page only when totalPages is more than 1, with the same perPage, for example client.Jobs.list({ perPage: 50, page: totalPages }).

For more about pages, see Page through results with the BizPrint Print API.

Every accepted client.Jobs.create call creates a new job, and every new job prints. So never call it again automatically after an error or a timeout.

Check the error’s name:

name What happened What to do
JobCreatingInvalidRequestError A field is missing or wrong. Its errors property lists the messages for each field. Fix the request. No job was created.
JobCreatingInvalidPrinterError The printer ID is not one of your printers. Check the ID. No job was created.
JobCreatingPrintJobLimitError Your account reached its print job limit. See BizPrint pricing. No job was created.
UnauthorizedError The keys or the signature were not accepted. Check your keys. No job was created.
Another error with error.response?.data?.errorCode of ERR_USER_IS_BLOCKED Your account cannot create print jobs right now, for example because the printing service is paused. See BizPrint Print API error codes and how to fix them. No job was created.
Anything else, or no answer The SDK could not confirm what happened, for example because of a server or network problem. The job may or may not have been created. Do not create it again yet. Look up your recent jobs first.
try {
const job = await client.Jobs.create({
printerId: YOUR_PRINTER_ID,
url: 'https://example.com/receipts/1001.pdf?token=LONG_RANDOM_TOKEN',
description: 'Order 1001',
});
await saveJobId('order-1001', job.id);
} catch (error) {
if (error.name === 'JobCreatingInvalidRequestError') {
console.error('Fix these fields:', error.errors);
} else {
// The job may exist. Do not retry automatically.
// Look up recent jobs (for example, by description) before you create it again.
console.error('Could not confirm the print job:', error.message);
}
}

When you look up a job with client.Jobs.retrieve or client.Printers.retrieve, the SDK can also throw NotFoundError (the ID does not exist) or AccessForbiddenError (your application is not allowed to see it). See BizPrint Print API error codes and how to fix them.

saveJobId stands for your own code that remembers which job belongs to which order.

Before anyone prints the same order again, check all of these:

  1. Look at the paper at the printer.
  2. Look up your newest jobs with client.Jobs.list, and match the description, url and createdAt.
  3. Check the job’s status in the BizPrint dashboard.
  4. Let the station finish what it is printing, whatever status the job shows.

Never create a new job while a matching job is pending, connecting-to-printer or processing. If it really did not print, reprint the existing job from the dashboard. See Reprint a print job from the BizPrint dashboard.

Need help? Contact BizPrint support at https://getbizprint.com/support/.