@iris-technologies/api
A TypeScript client library for the Iris advertising API with automatic impression and click URL handling.
Features
- Smart Ad Retrieval: Get contextually relevant advertisements
- Automatic URL Handling: Impression and click tracking URLs included
- Error Handling: Graceful fallbacks and comprehensive error logging
- TypeScript Support: Full type safety with detailed type definitions
- Configurable Topics: Exclude unwanted ad categories
- Comprehensive Testing: 21 test cases covering all functionality
Installation
npm install @iris-technologies/api
Quick Start
import { IrisClient } from '@iris-technologies/api';
const client = new IrisClient('your-api-key', ['politics', 'gambling']);
const ad = await client.getAd({
messages: [
{ role: 'user', content: 'user looking for weather information' },
{ role: 'assistant', content: 'showing current weather conditions' }
],
user: { uid: 'user-123' },
device: { ip: '1.2.3.4', country: 'US', ua: 'UA' },
excludedTopics: ['politics', 'gambling']
});
if (ad) {
console.log('Ad text:', ad.adText);
console.log('Impression URL:', ad.impUrl);
console.log('Click URL:', ad.clickUrl);
console.log('Publisher payout:', ad.payout);
} else {
console.log('No ad available');
}
API Reference
IrisClient
Constructor
new IrisClient(apiKey: string, excludedTopics: string[])
Parameters:
apiKey: Your Iris API key for authentication
excludedTopics: Array of topic strings to exclude from ads (e.g., ['politics', 'adult', 'gambling'])
Methods
getAd(params: BidParams): Promise<BidResponse | null>
Retrieves a targeted advertisement with automatic tracking URL handling.
Parameters (BidParams):
messages: Array of { role: 'user' | 'assistant', content: string } representing conversation history
device (optional): { ip: string; country: string; ua: string; os?: string; ifa?: string }
user (optional): { uid?: string; gender?: 'male' | 'female' | 'other'; age?: string; keywords?: string[] }
excludedTopics (optional): string[] of topics to exclude
apiKey (optional): overrides the client API key for this call
Returns:
BidResponse | null: Advertisement object with tracking URLs, or null if no ad is available (e.g., 204)
Example:
const ad = await client.getAd({
messages: [
{ role: 'user', content: 'How do I learn guitar?' },
{ role: 'assistant', content: 'Here are some tips for learning guitar...' }
],
user: { uid: 'user-456' }
});
updateExcludedTopics(excludedTopics: string[]): void
Updates the list of excluded ad topics.
client.updateExcludedTopics(['politics', 'crypto', 'dating']);
getExcludedTopics(): string[]
Returns a copy of the current excluded topics array.
const currentExclusions = client.getExcludedTopics();
console.log('Excluding:', currentExclusions);
Types
BidParams
type Role = 'user' | 'assistant';
type Gender = 'male' | 'female' | 'other';
interface MessageObject { role: Role; content: string }
interface DeviceObject { ip: string; country: string; ua: string; os?: string; ifa?: string }
interface UserObject { uid?: string; gender?: Gender; age?: string; keywords?: string[] }
interface BidParams {
apiKey?: string;
messages: MessageObject[];
device?: DeviceObject;
user?: UserObject;
excludedTopics?: string[];
}
BidResponse
interface BidResponse {
adText: string;
impUrl?: string;
clickUrl?: string;
payout?: number;
}
Example Response
{
text: "Learn guitar online with interactive lessons!",
impUrl: "https://api.iris.tech/impression?id=abc123&price=0.15",
clickUrl: "https://api.iris.tech/click?id=abc123&redirect=https://guitarlessons.com/signup",
payout: 0.075
}
Advanced Usage
Error Handling
The client handles errors gracefully and logs detailed information:
const ad = await client.getAd({
messages: [
{ role: 'user', content: 'context' },
{ role: 'assistant', content: 'response' }
],
user: { uid: 'user' }
});
if (!ad) {
console.log('No ad available - could be network error, no inventory, or excluded topic');
}
Dynamic Topic Management
const client = new IrisClient('api-key', ['politics']);
const userPreferences = getUserAdPreferences();
if (userPreferences.excludeGambling) {
const current = client.getExcludedTopics();
client.updateExcludedTopics([...current, 'gambling', 'casino']);
}
const ad = await client.getAd({
messages: [
{ role: 'user', content: context },
{ role: 'assistant', content: response }
],
user: { uid: userId }
});
Integration with React Component
Perfect pairing with @iris-technologies/react:
import { IrisClient } from '@iris-technologies/api';
import { IrisAd } from '@iris-technologies/react';
const client = new IrisClient('your-api-key', ['politics']);
function AdContainer({ userInput, botResponse, userId }) {
const [ad, setAd] = useState(null);
useEffect(() => {
client.getAd(userInput, botResponse, userId)
.then(setAd);
}, [userInput, botResponse, userId]);
return ad ? <IrisAd ad={ad} /> : null;
}
Configuration
API Endpoint
The client connects to https://api.iristech.dev by default. The endpoint includes:
- Authentication via Bearer token
- 10-second request timeout
- JSON content type headers
- Error logging for debugging
Excluded Topics
Common topic exclusions:
const excludedTopics = [
'politics',
'adult',
'gambling',
'crypto',
'dating',
'religion',
'health',
'financial',
];
const client = new IrisClient('api-key', excludedTopics);
Testing
This package includes comprehensive test coverage (21 test cases):
npm test
npm run test:watch
npm run test:coverage
What's Tested
- ✅ API response parsing and URL extraction
- ✅ HTTP error handling (404, 500, timeouts)
- ✅ Network error handling
- ✅ Invalid response data handling
- ✅ Excluded topics management
- ✅ Configuration and authentication
- ✅ Edge cases (missing URLs, special characters)
Error Handling
The client provides comprehensive error handling:
Network Errors
const ad = await client.getAd(context, response, userId);
HTTP Errors
Invalid Data
Development
npm install
npm run build
npm run dev
npm run clean
npm test
Publishing
Version Management
npm run version:patch
npm run version:minor
npm run version:major
Publishing to NPM
npm run publish:dry-run
npm run publish:patch
npm run publish:beta
TypeScript
Full TypeScript support with exported types:
import {
IrisClient,
AdResponse,
GetAdParams,
IrisClientConfig
} from '@iris-technologies/api';
const client: IrisClient = new IrisClient('key', []);
const ad: AdResponse | null = await client.getAd('', '', '');
License
ISC