Barcodes.GG API Wrapper for TypeScript
Empower your TypeScript projects with the Barcodes.GG API. Harness the capabilities of our TypeScript SDK to facilitate seamless barcode integrations. Here's a step-by-step guide to get you started.
Prerequisites
Ensure your TypeScript environment is equipped with the https
module.
SDK Wrapper Code
Incorporate the following TypeScript SDK wrapper code into your project to seamlessly interact with the Barcodes API.
import * as https from 'https';
class BarcodesAPI {
private apiKey: string;
private readonly baseUrl: string = 'https://barcodes.gg/api';
constructor(apiKey: string) {
this.apiKey = apiKey;
}
private async makeRequest(endpoint: string): Promise<any> {
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);
});
});
}
async searchProducts(query: string) {
return this.makeRequest(`/product/search?query=${query}`);
}
async lookupBarcode(ean: string, showData: number = 1, fields: string = '*') {
return this.makeRequest(`/barcode/lookup/${ean}?show_data=${showData}&fields=${fields}`);
}
async getImage(styleCode: string) {
return this.makeRequest(`/image/${styleCode}`);
}
async reverseLookupBySKU(sku: string, isVerified: number = 1, page: number = 1) {
return this.makeRequest(`/barcode/lookup/sku/${sku}?is_verified=${isVerified}&page=${page}`);
}
}
SDK Integration
Embark on a seamless barcode integration journey with TypeScript.
Initialization
Kick things off by setting up the BarcodesAPI
class in your TypeScript project.
const api = new BarcodesAPI('YOUR_API_KEY');
Replace 'YOUR_API_KEY'
with your actual API key to ensure genuine access.
Product Search
Delve deep into the product database with a simple query.
api.searchProducts('dd1503-100').then(console.log);
Barcode Lookup
Retrieve detailed information about a product using its barcode.
api.lookupBarcode('195245980597').then(console.log);
Image Retrieval
Fetch product images effortlessly using the style code.
api.getImage('DH4756-100').then(console.log);
SKU-based Reverse Barcode Lookup
Shift gears by using SKU to derive barcodes.
api.reverseLookupBySKU('DD1391-100').then(console.log);
Leverage the asynchronous nature of TypeScript and handle responses using .then()
or the async/await
paradigm.