Create a barcode API with javascript

I’m using Visual Studio code on a MacBook Pro M1, the following sample code in JavaScript uses a third-party library to generate a Code 128 barcode and return it as a PNG image.

The example is based on bwip-js available on github: https://github.com/metafloor/bwip-js

Note! The purpose of this guide is to show how to create a simple web server with node.js to generate barcodes as images, if you are to use bwip-js in production you should add security to the code for the web server.

If you don’t have visual studio code, you can downlaod it for free here

If you have never used node.js before, you can find it good example to get up and running here

Download node.js: https://nodejs.dev/en/download/

Start Visual Studio code

  1. Select ‘Open folder’ and create a new folder called ‘create-barcode’ to store your project.

Skapa ett streckkods-api med javascript - Create-barcode demo screenshot - empty folder created

2. Open a terminal either by tool in menu ‘Terminal – new terminal’ or by ‘Launchpad’ – Terminal.

(Make sure you are in the right folder by typing pwd and press enter, otherwise navigate to the right path where you stored the ‘create-barcode’ folder)

3. Type ‘Yarn init’ and press enter to initialize a new project and create package.json

Skapa ett streckkods-api med javascript - Create-barcode demo - init project and create package.json

4. Type yarn add nodemon http bwip-js and press enter.

Your package.json should look like this:

Skapa ett streckkods-api med javascript - Create-barcode demo - package.json verify

5. Create a new file called index.js and copy in the code:

Copy to Clipboard

6. Open package.json and create a new section called scripts like this:

Skapa ett streckkods-api med javascript - Create-barcode demo - package.json final edit

7. Save all files

8. In Terminal: ‘yarn start’ and press enter. It should look like this:

Skapa ett streckkods-api med javascript - Create-barcode demo - terminal nodemon running index.js

Use postman to try it out

Click the New button and select HTTP.

Skapa ett streckkods-api med javascript - Create-barcode demo - Use postman to test

Create a barcode api with javascript – Copy example URL and paste in input after GET

Example:

Copy to Clipboard
Skapa ett streckkods-api med javascript - Create-barcode demo - Postman GET paste to input

Create a barcode api with javascript – Click Send

You should get this result:

Skapa ett streckkods-api med javascript - Create-barcode demo - Postman result

Eller testa i en webbläsare

  1. Öppna en webbläsare och klistra in
Copy to Clipboard

Not everyone can code their own systems – there are ready-made solutions.

Although this is a good example of how you can create a barcode API with javascript and then build the functionality into your existing reporting systems or apps, not everyone has the knowledge, desire or time to create and maintain an in-house solution. Therefore, there is software that makes it easy to automatically create labels, barcodes, etc. without the knowledge or the need to operate systems. Cloudlabel does all this and more, book a demo to talk with us about how you can easily move forward with barcodes and traceability

Contact us

Say hello to better label management