JavaScript Examples
Complete JavaScript examples for integrating with the Influship API using modern async/await syntax and error handling.Setup
Install Dependencies
Copy
npm install node-fetch
# or
yarn add node-fetch
Environment Variables
Create a.env file:
Copy
INFLUSHIP_API_KEY=your_api_key_here
Basic API Client
Copy
// influship-client.js
const fetch = require('node-fetch');
class InflushipClient {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseURL = 'https://api.influship.com';
}
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
headers: {
'X-API-Key': this.apiKey,
'Content-Type': 'application/json',
...options.headers
},
...options
};
try {
const response = await fetch(url, config);
const data = await response.json();
if (!response.ok) {
throw new Error(`API Error: ${data.error?.message || 'Unknown error'}`);
}
return data;
} catch (error) {
console.error('Request failed:', error.message);
throw error;
}
}
// Health check
async health() {
return this.request('/health');
}
// Search creators
async searchCreators(query, filters = {}) {
return this.request('/v1/search', {
method: 'POST',
body: JSON.stringify({ query, ...filters })
});
}
// Get multiple creators by ID
async getCreators(creatorIds, mode = 'lite') {
return this.request('/v1/creators', {
method: 'POST',
body: JSON.stringify({ creator_ids: creatorIds, mode })
});
}
// Get multiple platform profiles
async getProfiles(profiles, mode = 'lite') {
return this.request('/v1/profiles', {
method: 'POST',
body: JSON.stringify({ profiles, mode })
});
}
// Analyze post
async analyzePost(platform, url, mode = 'lite') {
return this.request('/v1/posts/analyze', {
method: 'POST',
body: JSON.stringify({ platform, url, mode })
});
}
}
module.exports = InflushipClient;
Usage Examples
1. Health Check
Copy
const InflushipClient = require('./influship-client');
const client = new InflushipClient(process.env.INFLUSHIP_API_KEY);
async function checkHealth() {
try {
const health = await client.health();
console.log('API Status:', health);
} catch (error) {
console.error('Health check failed:', error.message);
}
}
checkHealth();
2. Search for Creators
Copy
async function searchFitnessInfluencers() {
try {
const results = await client.searchCreators(
'fitness influencers with high engagement',
{
filters: {
min_followers: 10000,
engagement_rate: { min: 0.03 },
platforms: ['instagram']
},
limit: 10
}
);
console.log(`Found ${results.items.length} creators`);
results.items.forEach(creator => {
console.log(`${creator.name} (@${creator.username}) - ${creator.follower_count} followers`);
});
return results;
} catch (error) {
console.error('Search failed:', error.message);
}
}
searchFitnessInfluencers();
3. Get Detailed Creator Information
Copy
async function getCreatorDetails(creatorId) {
try {
const response = await client.getCreators([creatorId], 'detailed');
const creator = response.creators[0];
console.log('Creator Details:');
console.log(`Name: ${creator.name}`);
console.log(`Username: ${creator.username}`);
console.log(`Followers: ${creator.follower_count.toLocaleString()}`);
console.log(`Engagement Rate: ${(creator.engagement_rate * 100).toFixed(2)}%`);
console.log(`Bio: ${creator.bio}`);
if (creator.contact?.email) {
console.log(`Email: ${creator.contact.email}`);
}
return creator;
} catch (error) {
console.error('Failed to get creator details:', error.message);
}
}
// Usage
getCreatorDetails('00000000-0000-0000-0000-000000000000');
4. Analyze Instagram Post
Copy
async function analyzePost(postUrl) {
try {
const analysis = await client.analyzePost('instagram', postUrl, 'detailed');
console.log('Post Analysis:');
console.log(`Likes: ${analysis.likes.toLocaleString()}`);
console.log(`Comments: ${analysis.comments.toLocaleString()}`);
console.log(`Engagement Rate: ${(analysis.engagement_rate * 100).toFixed(2)}%`);
if (analysis.content_analysis) {
console.log('Topics:', analysis.content_analysis.topics.join(', '));
console.log('Hashtags:', analysis.content_analysis.hashtags.join(', '));
}
return analysis;
} catch (error) {
console.error('Post analysis failed:', error.message);
}
}
// Usage
analyzePost('https://instagram.com/p/POST_ID');
5. Batch Creator Enrichment
Copy
async function enrichMultipleCreators(creatorIds) {
const results = [];
// Process in batches to avoid rate limits
const batchSize = 5;
for (let i = 0; i < creatorIds.length; i += batchSize) {
const batch = creatorIds.slice(i, i + batchSize);
const promises = batch.map(async (batchIds) => {
try {
const response = await client.getCreators(batchIds, 'detailed');
return { success: true, data: response.creators };
} catch (error) {
return { success: false, error: error.message };
}
});
const batchResults = await Promise.all(promises);
for (const result of batchResults) {
if (result.success) {
for (const creator of result.data) {
results.push({ id: creator.id, success: true, data: creator });
}
} else {
results.push({ success: false, error: result.error });
}
}
// Add delay between batches
if (i + batchSize < creatorIds.length) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
return results;
}
// Usage
const creatorIds = [
'00000000-0000-0000-0000-000000000000',
'00000000-0000-0000-0000-000000000001'
];
enrichMultipleCreators(creatorIds).then(results => {
results.forEach(result => {
if (result.success) {
console.log(`✅ ${result.id}: ${result.data.name}`);
} else {
console.log(`❌ ${result.id}: ${result.error}`);
}
});
});
6. Error Handling with Retry Logic
Copy
async function requestWithRetry(endpoint, options = {}, maxRetries = 3) {
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await client.request(endpoint, options);
} catch (error) {
lastError = error;
// Don't retry on authentication errors
if (error.message.includes('unauthorized') || error.message.includes('forbidden')) {
throw error;
}
// Don't retry on validation errors
if (error.message.includes('validation_error')) {
throw error;
}
if (attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
console.log(`Attempt ${attempt} failed, retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
throw lastError;
}
// Usage
async function searchWithRetry(query) {
try {
const results = await requestWithRetry('/v1/search', {
method: 'POST',
body: JSON.stringify({ query })
});
return results;
} catch (error) {
console.error('Search failed after retries:', error.message);
throw error;
}
}
7. Complete Workflow Example
Copy
async function findAndAnalyzeCreators() {
try {
// Step 1: Search for creators
console.log('🔍 Searching for fitness influencers...');
const searchResults = await client.searchCreators(
'fitness influencers with high engagement',
{ limit: 5 }
);
if (searchResults.items.length === 0) {
console.log('No creators found');
return;
}
// Step 2: Get detailed information for each creator
console.log(`📊 Analyzing ${searchResults.items.length} creators...`);
const creators = [];
// Get all creator IDs from search results
const creatorIds = searchResults.items.map(item => item.id);
// Get detailed information for all creators at once
try {
const creatorsResponse = await client.getCreators(creatorIds, 'detailed');
creators.push(...creatorsResponse.creators);
for (const creator of creatorsResponse.creators) {
console.log(`✅ Analyzed: ${creator.name} (${creator.follower_count} followers)`);
}
} catch (error) {
console.log(`❌ Failed to analyze creators: ${error.message}`);
return;
}
// Step 3: Sort by engagement rate
creators.sort((a, b) => b.engagement_rate - a.engagement_rate);
// Step 4: Display results
console.log('\n🏆 Top Creators by Engagement:');
creators.forEach((creator, index) => {
console.log(`${index + 1}. ${creator.name} (@${creator.username})`);
console.log(` Followers: ${creator.follower_count.toLocaleString()}`);
console.log(` Engagement: ${(creator.engagement_rate * 100).toFixed(2)}%`);
console.log(` Bio: ${creator.bio}`);
console.log('');
});
return creators;
} catch (error) {
console.error('Workflow failed:', error.message);
}
}
// Run the complete workflow
findAndAnalyzeCreators();
TypeScript Support
If you’re using TypeScript, here are the type definitions:Copy
// types.ts
export interface Creator {
id: string;
name: string;
username: string;
platform: string;
follower_count: number;
engagement_rate: number;
verified: boolean;
bio?: string;
contact?: {
email?: string;
website?: string;
};
}
export interface SearchResponse {
items: Creator[];
filtered_total: number;
has_more: boolean;
next_cursor?: string;
}
export interface ApiError {
error: {
code: string;
message: string;
request_id: string;
};
}
Related Examples
- Python Examples - Python SDK examples
- cURL Examples - Command-line examples
- Error Handling Guide - Comprehensive error handling