openrouter-ts
v1.0.2
Published
A production-ready TypeScript client library for the OpenRouter API
Maintainers
Readme
OpenRouter TypeScript Client
A production-ready TypeScript client library for the OpenRouter API. This library provides comprehensive access to multiple AI models through OpenRouter's unified interface with full TypeScript support, streaming, error handling, and developer-friendly utilities.
Features
- ✅ Full TypeScript Support - Complete type definitions for all endpoints and parameters
- ✅ Streaming Support - Real-time streaming responses with async iterators
- ✅ Error Handling - Comprehensive error classes with proper categorization
- ✅ Retry Logic - Configurable exponential backoff for network resilience
- ✅ Tool Calling - Full support for function calling across providers
- ✅ Input Validation - Parameter validation with helpful error messages
- ✅ Helper Utilities - Builder patterns and convenience functions
- ✅ Request Cancellation - AbortController support for canceling requests
- ✅ Rate Limit Handling - Automatic retry with proper rate limit respect
- ✅ Multimodal Support - Text and image inputs for compatible models
Installation
npm install @openrouter/typescript-clientQuick Start
import { OpenRouterClient, MODELS } from '@openrouter/typescript-client';
const client = new OpenRouterClient({
apiKey: process.env.OPENROUTER_API_KEY!,
siteUrl: 'https://yoursite.com', // Optional: for rankings
siteName: 'Your App Name' // Optional: for rankings
});
// Basic chat completion
const response = await client.chat.completions.create({
model: MODELS.GPT_4O,
messages: [
{ role: 'user', content: 'Hello! How are you?' }
]
});
console.log(response.choices[0]?.message?.content);License
MIT License. See LICENSE for details.
