How to read barcode from uploaded file (node for barcode reader API in JavaScript using Web API

Barcodes are everywhere! Programming barcodes solutions give endless opportunies and gives convinient solution to endusers. provides endpoints to read and create almost all kind of barcode types. And it’s very easy to implement.

In this article, we’ll review a code which reads barcode from uploaded file using This Source code is also available at our GitHub repository at this location. Solution in other programming languages are also avaialble at GitHub repository at this location. Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)


/*jshint esversion: 6 */ var https = require("https"); var path = require("path"); var fs = require("fs"); // `request` module is required for file upload. // Use "npm install request" command to install. var request = require("request"); // The authentication key (API Key). // Get your own by registering at const API_KEY = "*********************************"; // Source file name const SourceFile = "./sample.pdf"; // Comma-separated list of barcode types to search. // See valid barcode types in the documentation const BarcodeTypes = "Code128,Code39,Interleaved2of5,EAN13"; // Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'. const Pages = ""; // 1. RETRIEVE THE PRESIGNED URL TO UPLOAD THE FILE. getPresignedUrl(API_KEY, SourceFile) .then(([uploadUrl, uploadedFileUrl]) => { // 2. UPLOAD THE FILE TO CLOUD. uploadFile(API_KEY, SourceFile, uploadUrl) .then(() => { // 3. READ BARCODES FROM UPLOADED FILE readBarcodes(API_KEY, uploadedFileUrl, Pages, BarcodeTypes); }) .catch(e => { console.log(e); }); }) .catch(e => { console.log(e); }); function getPresignedUrl(apiKey, localFile) { return new Promise(resolve => { // Prepare request to `Get Presigned URL` API endpoint let queryPath = `/v1/file/upload/get-presigned-url?contenttype=application/octet-stream&name=${path.basename(SourceFile)}`; let reqOptions = { host: "", path: encodeURI(queryPath), headers: { "x-api-key": API_KEY } }; // Send request https.get(reqOptions, (response) => { response.on("data", (d) => { let data = JSON.parse(d); if (data.error == false) { // Return presigned url we received resolve([data.presignedUrl, data.url]); } else { // Service reported error console.log("getPresignedUrl(): " + data.message); } }); }) .on("error", (e) => { // Request error console.log("getPresignedUrl(): " + e); }); }); } function uploadFile(apiKey, localFile, uploadUrl) { return new Promise(resolve => { fs.readFile(SourceFile, (err, data) => { request({ method: "PUT", url: uploadUrl, body: data, headers: { "Content-Type": "application/octet-stream" } }, (err, res, body) => { if (!err) { resolve(); } else { console.log("uploadFile() request error: " + e); } }); }); }); } function readBarcodes(apiKey, uploadedFileUrl, pages, barcodeTypes) { // Prepare request to `Barcode Reader` API endpoint let queryPath = `/v1/barcode/read/from/url`; // JSON payload for api request var jsonPayload = JSON.stringify({ types: barcodeTypes, pages: pages, url: uploadedFileUrl }); let reqOptions = { host: "", method: "POST", path: queryPath, headers: { "x-api-key": apiKey, "Content-Type": "application/json", "Content-Length": Buffer.byteLength(jsonPayload, 'utf8') } }; // Send request var postRequest = https.request(reqOptions, (response) => { response.on("data", (d) => { response.setEncoding("utf8"); // Parse JSON response let data = JSON.parse(d); if (data.error == false) { // Display found barcodes in console data.barcodes.forEach((element) => { console.log("Found barcode:"); console.log(" Type: " + element["TypeName"]); console.log(" Value: " + element["Value"]); console.log(" Document Page Index: " + element["Page"]); console.log(" Rectangle: " + element["Rect"]); console.log(" Confidence: " + element["Confidence"]); console.log(); }, this); } else { // Service reported error console.log("readBarcodes(): " + data.message); } }); }) .on("error", (e) => { // Request error console.log("readBarcodes(): " + e); }); // Write request data postRequest.write(jsonPayload); postRequest.end(); }


{ "name": "test", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { }, "keywords": [ "", "web", "api", "bytescout", "api" ], "author": "ByteScout &", "license": "ISC", "dependencies": { "request": "^2.88.2" } }


Though the code is straight-forward and easy to understand, let’s analyze it. We can logically divide code into three parts; Declarations, Uploading source file URL to server and reading barcode value from this public URL.

At the start of program necessary references such as https, path, fs, request are loaded into program. Required data for making this barcode reading request such as SourceFile, BarcodeTypes, Pages are also collected and stored in variables.

SourceFile Source file PDF path
BarcodeTypes Value of this variable contains comma seperated list of barcode types which needs to be searched. Please refer to this link to get supported barcode types.
Pages Contains comma-seperated list of page indices which needs to be processed. Please leave this field empty to process all pages.

In order to upload source file to cloud we’re first getting pre-signed URL and then uploading input file to this pre-signed URL using PUT request. API endpoint /v1/file/upload/get-presigned-url is used to get pre-signed URL. We’re passing file name as a parameter there. This request returns two URLs in resonse body, Pre-signed URL (data.presignedUrl) and public URL (data.url). As mentioned in function uploadFile, we’re using PUT request to upload source file contents. supports both PDF and Image as a input file to read barcode values.

Once file upload is completed, we’re preparing barcode request /v1/barcode/read/from/url to read barcodes from public URL. JSON payload containing barcode types, pages and Source file URL is prepared to pass as request input. API key is passed in header x-api-key for authentication purpose.

API response contains barcode information array with information such as barcode type, barcode value, page index on which barcode is found, location co-ordinates of found barcode, confidance of parse barcode and many other information.

console.log("  Type: " + element["TypeName"]);
console.log("  Value: " + element["Value"]);
console.log("  Document Page Index: " + element["Page"]);
console.log("  Rectangle: " + element["Rect"]);
console.log("  Confidence: " + element["Confidence"]);

In this sample, we’ve used basic parameters for demonstration purpose. Based on our requirement we can tweak input request. Please visit documentations for more information.

Thank you for reading!



Get 60 Day Free Trial

See also:


Get Your API Key

See also:

Related Samples: