Barcodes API Wrapper for Node.js
Unlock the potential of barcode integrations in your Node.js applications with the Barcodes API. Our tailored Node.js SDK simplifies the integration process, allowing you to focus on building powerful applications. This guide provides a comprehensive walkthrough to set you on the right path.
Prerequisites
Ensure your Node.js environment is equipped with the https
module. If not, you can typically add it using npm or yarn.
npm install https
SDK Wrapper Code
Integrate the following Node.js SDK wrapper into your project for a seamless interaction with the Barcodes API.
const https = require('https');
class BarcodesAPI {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = 'https://barcodes.gg/api';
}
makeRequest(endpoint) {
return new Promise((resolve, reject) => {
const options = {
headers: {
'Authorization': `Bearer ${this.apiKey}`
}
};
https.get(this.baseUrl + endpoint, options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(JSON.parse(data));
});
}).on('error', (err) => {
reject(err);
});
});
}
searchProducts(query) {
return this.makeRequest(`/product/search?query=${query}`);
}
lookupBarcode(ean, showData = 1, fields = '*') {
return this.makeRequest(`/barcode/lookup/${ean}?show_data=${showData}&fields=${fields}`);
}
getImage(styleCode) {
return this.makeRequest(`/image/${styleCode}`);
}
reverseLookupBySKU(sku, isVerified = 1, page = 1) {
return this.makeRequest(`/barcode/lookup/sku/${sku}?is_verified=${isVerified}&page=${page}`);
}
}
SDK Integration
Initialization
Begin your integration journey by initializing the BarcodesAPI
class.
const api = new BarcodesAPI('YOUR_API_KEY');
Replace 'YOUR_API_KEY'
with your actual API key to guarantee genuine access.
Product Search
Explore a vast array of products with a straightforward query.
api.searchProducts('dd1503-100').then(console.log);
Barcode Lookup
Obtain comprehensive details about a product via its barcode.
api.lookupBarcode('195245980597').then(console.log);
Image Retrieval
Effortlessly obtain product images using the style code.
api.getImage('DH4756-100').then(console.log);
SKU-based Reverse Barcode Lookup
Reimagine your search strategy by employing the SKU to acquire barcodes.
api.reverseLookupBySKU('DD1391-100').then(console.log);
Harness the asynchronous capabilities of Node.js! Opt between handling responses using .then()
or the modern async/await
approach.