JavaScript Example
This example demonstrates how to use the WebClassifAI API with JavaScript using the axios
library.
Installation
npm install axios
Basic Example
const axios = require("axios"); // Node.js
// For browser: axios is already available from the CDN
/**
* Classify a list of URLs using the WebClassifAI API
* @param {Array<string>} urls - List of URLs to classify
* @param {string} apiKey - Your API key
* @param {string} [taxonomy="iab-1.0"] - Classification taxonomy to use
* @returns {Promise<Object>} - API response containing classification results
*/
async function classifyUrls(urls, apiKey, taxonomy = "iab-1.0") {
const endpoint = "https://api.classifai.com/v1";
const payload = {
urls: urls,
api_key: apiKey,
taxonomy: taxonomy,
};
try {
const response = await axios.post(endpoint, payload, {
headers: {
"Content-Type": "application/json",
},
});
return response.data;
} catch (error) {
console.error("Error making request:", error.message);
if (error.response) {
console.error("Response status:", error.response.status);
console.error("Response data:", error.response.data);
}
throw error;
}
}
// Example usage
async function main() {
try {
// Your API key
const apiKey = "your_api_key_here";
// List of URLs to classify
const urls = ["https://www.example.com/", "https://www.tech-blog.com/"];
// Get classifications
const results = await classifyUrls(urls, apiKey);
// Process results
results.results.forEach((result) => {
console.log(`\nURL: ${result.url}`);
console.log(`Scraped successfully: ${result.scraped_successfully}`);
result.predictions.forEach((prediction) => {
console.log("\nPrediction:");
if (prediction.tier1) {
const tier1 = prediction.tier1;
console.log(`Tier 1: ${tier1.name} (${tier1.code}) - Confidence: ${tier1.confidence.toFixed(2)}`);
}
if (prediction.tier2) {
const tier2 = prediction.tier2;
console.log(`Tier 2: ${tier2.name} (${tier2.code}) - Confidence: ${tier2.confidence.toFixed(2)}`);
}
});
});
} catch (error) {
console.error("Error in main:", error.message);
}
}
main();
Error Handling
Error Handling
The example includes basic error handling for network issues and invalid responses. For production use, you might want to add:
- Rate limiting
- Retry logic
- More detailed error handling
- Logging
Batch Processing
Important: For optimal performance, we strongly recommend sending multiple URLs in a single request (up to the 500 URL limit). Only split URLs into multiple requests if you have more than 500 URLs or your architecture specifically requires it.
// Process up to 500 URLs in a single request
const urls = Array(500).fill("https://example.com");
const results = await classifyUrls(urls, apiKey);
Handling Large URL Sets
If you need to process more than 500 URLs:
/**
* Process a large set of URLs by breaking them into batches
* @param {Array<string>} allUrls - All URLs to process
* @param {string} apiKey - Your API key
* @param {string} [taxonomy="iab-1.0"] - Classification taxonomy
*/
async function processLargeUrlSet(allUrls, apiKey, taxonomy = "iab-1.0") {
const batchSize = 500; // WebClassifAI API maximum batch size
const results = [];
// Process URLs in batches of 500
for (let i = 0; i < allUrls.length; i += batchSize) {
const batchUrls = allUrls.slice(i, i + batchSize);
console.log(
`Processing batch ${Math.floor(i / batchSize) + 1}/${Math.ceil(allUrls.length / batchSize)} with ${
batchUrls.length
} URLs`
);
try {
const batchResults = await classifyUrls(batchUrls, apiKey, taxonomy);
results.push(batchResults);
console.log(`Successfully processed batch with ${batchResults.results.length} URLs`);
} catch (error) {
console.error(`Error processing batch ${Math.floor(i / batchSize) + 1}:`, error.message);
}
// Add a small delay between batches to avoid rate limiting
if (i + batchSize < allUrls.length) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
return results;
}
// Example usage
const allUrls = Array(1500).fill("https://example.com");
const batchResults = await processLargeUrlSet(allUrls, apiKey);
console.log(`Processed ${batchResults.length} batches in total`);
Additional Resources
Last updated on