Create Barcodes in JavaScript / NodeJS

This JavaScript / NodeJS sample code shows how to create barcodes with the McAPI Barcode REST API service.

Requirements: A RapidAPI account. Replace YOUR_API_KEY in the snippets below with your RapidAPI key.

The sample works with the free tier of the API, see RapidAPI McAPI Barcode Listing for available plans.

See the overview page for a reference that lists all available parameters and error codes.

Create a Barcode using Node JS with request module

Although deprecated, the request module is still in wide use. Shown is the creation of a Code 128 (code type: 7) with JavaScript encoding the text "123456". The module width is set to 0.5mm, the height is set to 20mm, the size of the human readable text under the barcode is set to 10 points. The code is returned as a base64 encoded PDF.

The source code:

// Listing: Create barcodes in JavaScript
  
const request = require('request');

const options = {
  method: 'POST',
  url: 'https://mcapi-barcode.p.rapidapi.com/',
  headers: {
    'content-type': 'application/json',
    'x-rapidapi-key': 'YOUR_API_KEY',
    'x-rapidapi-host': 'mcapi-barcode.p.rapidapi.com',
  },
  body: {
    data: '123456',
    type: 7,
    moduleWidth: 0.5,
    moduleHeight: 20,
    hrSize: 10,
    format: 'pdf'
  },
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

The data will be delivered as a JSON object in body, e.g.:

{
  "service":"McAPI Barcode Generator, https://mcapi.io",
  "version":"V1",
  "format":"pdf",
  "code":"data:application/pdf;base64,JVBE ... Cg=="
}

Sample barcode with JavaScript

The returned code from the Node JS request:

Image of Sample Barcode Node JS

This is just an introductory example. We provide more JavaScript sample code for barcode creation here.