UNPKG

gtin-lookup

Version:

Lookup GTIN, UPC, EAN or ISBN data using the API on ean-search.org

124 lines (112 loc) 4.27 kB
"use strict"; var GTINLookup = require("./gtin-lookup.js"); const apiToken = "83cbb52e6130e3c4484847bae64388"; var gtinLookup = new GTINLookup(apiToken); // lookup GTIN, prefer English (1) name const gtin = "5099750442227"; gtinLookup.gtinLookup(gtin, 1, function(response){ if (!response.serverError) { if (response.product != undefined) { console.log("GTIN " + gtin + " is " + response.product[0].name + " category: " + response.product[0].categoryName); } else { console.log("GTIN " + gtin + " not found in database"); } } else { console.log('Server error: ' + resp.statusCode); } }); const isbn = "1119578884"; gtinLookup.isbnLookup(isbn, function(response){ if (!response.serverError) { if (response.product != undefined) { console.log("ISBN " + isbn + " is " + response.product[0].name); } else { console.log("ISBN " + isbn + " not found in database"); } } else { console.log('isbnLookup: Server error: ' + resp.statusCode); } }); // search for product name, prefer English (1) let name = "Bananaboat"; gtinLookup.gtinSearch(name, 1, function(response){ if (!response.serverError) { if (response.product.productlist != undefined) { console.log("gtinSearch()"); for (let i = 0; i < response.product.productlist.length; i++) { console.log(response.product.productlist[i].ean + " is " + response.product.productlist[i].name); } } else { console.log("gtinSearch: Name " + name + " not found in database"); } } else { console.log('gtinearch: Server error: ' + resp.statusCode); } }); gtinLookup.gtinSimilarSearch("Apple iPhone 16GB robust", 1, function(response){ if (!response.serverError) { if (response.product.productlist != undefined) { console.log("gtinSimilarSearch()"); for (let i = 0; i < response.product.productlist.length; i++) { console.log(response.product.productlist[i].ean + " is " + response.product.productlist[i].name); } } else { console.log("gtinSimilarSearch: Name " + name + " not found in database"); } } else { console.log('gtinSimilarSearch: Server error: ' + resp.statusCode); } }); // search for name, but restrict to Fashion category (20), prefer English (1) gtinLookup.gtinCategorySearch(name, 20, 1, function(response){ if (!response.serverError) { if (response.product.productlist != undefined) { console.log("gtinCategorySearch()"); for (let i = 0; i < response.product.productlist.length; i++) { console.log("Fashion: " + response.product.productlist[i].ean + " is " + response.product.productlist[i].name); } } else { console.log("gtinCategorySearch: Name " + name + " not found in database"); } } else { console.log('gtinCategorySearch: Server error: ' + resp.statusCode); } }); // find all GTINs starting with "5099750442", prefer English (1), just fetch page 0 gtinLookup.gtinPrefixSearch("5099750442", 1, 0, function(response){ if (!response.serverError) { if (response.product.productlist != undefined) { console.log("gtinPrefixSearch()"); for (let i = 0; i < response.product.productlist.length; i++) { console.log(response.product.productlist[i].ean + " is " + response.product.productlist[i].name); } } else { console.log("gtinPrefixSearch: Name " + name + " not found in database"); } } else { console.log('Server error: ' + resp.statusCode); } }); gtinLookup.gtinIssuingCountryLookup(gtin, function(response){ if (!response.serverError) { console.log("GTIN " + gtin + " was issued in " + response.product[0].issuingCountry); } else { console.log('Server error: ' + resp.statusCode); } }); console.log("gtinVerifyChecksum()"); gtinLookup.gtinVerifyChecksum(gtin, function(response){ if (!response.serverError) { console.log("Checksum of GTIN " + gtin + " is " + (response.product[0].valid ? "OK" : "Invalid")); } else { console.log('Server error: ' + resp.statusCode); } }); console.log("gtinBarcode()"); gtinLookup.gtinBarcodeImage(gtin, 200, 100, function(response){ if (!response.serverError) { console.log("Base64 encoded PNG image of GTIN " + gtin + " barcode: " + response.product[0].barcode); } else { console.log('Server error: ' + resp.statusCode); } });