Doing GTIN Lookup in JavaScript
13 Sep 2024
JavaScript is one of the most popular programming languages and we often get asked how to use our GTIN database from JavaScript environments (e.g. Node JS etc.).
While it is not very difficult to hand craft HTTP queries to our API, the easiest way is to use the gtin-lookup module on npm.
Just install it with
npm install gtin-lookup
If, for example, you want to look up the product for a barcode that you scanned and display the name along with the GTIN barcode image, just call the gtinLookup() and gtinBarcodeImage() methods.
Generating and displaying PNG barcode images
Our API function barcode-image returns the PNG image of your barcode in base64 encoded form that you can decode and save into an image file. But if you are generating web pages, it's much easier to simply include it directly into your HTML code as inline image and let the browser do all the work, while you only have to deal with a single HTML page and there is no extra image file to take care of.
<img src="data:image/png;base64,' + base64_image + '">
Here is the short example:
var GTINLookup = require("gtin-lookup");
const apiToken = "abcdef"; // replace with your API token
var gtinLookup = new GTINLookup(apiToken);
const gtin = "5099750442227"; // <-- this is the GTIN you scanned
var html = '<!DOCTYPE html><html><body><h1>GTIN ' + gtin + '</h1>'; // HTML header
gtinLookup.gtinLookup(gtin, 1, function(response) { // get the product name
if (response.product != undefined) {
html += response.product[0].name;
gtinLookup.gtinBarcodeImage(gtin, 200, 100, function(response){
if (!response.serverError) {
html += '<p><img src="data:image/png;base64,'
+ response.product[0].barcode + '">'; // the barcode image
html += '</body></html>'; // HTML footer
}
});
}
});
In real life, you would want to do a bit more error checking.
This process works with both server-side and client side. Just make sure you protect your API access token if you do run your code client-side.
PS: Of course, we also support many other programming languages.
Update 16 Dec 2025:
All programming libraries can now be found on this page.