|
| 1 | +# OpenRouter TypeScript SDK |
| 2 | + |
| 3 | +The OpenRouter TypeScript SDK is a type-safe toolkit for building AI applications with access to 300+ language models through a unified API. |
| 4 | + |
| 5 | +## Why use the OpenRouter SDK? |
| 6 | + |
| 7 | +Integrating AI models into applications involves handling different provider APIs, managing model-specific requirements, and avoiding common implementation mistakes. The OpenRouter SDK standardizes these integrations and protects you from footguns. |
| 8 | + |
| 9 | +```typescript |
| 10 | +import OpenRouter from '@openrouter/sdk'; |
| 11 | + |
| 12 | +const client = new OpenRouter({ |
| 13 | + apiKey: process.env.OPENROUTER_API_KEY |
| 14 | +}); |
| 15 | + |
| 16 | +const response = await client.chat.completions.create({ |
| 17 | + model: "minimax/minimax-m2", |
| 18 | + messages: [ |
| 19 | + { role: "user", content: "Explain quantum computing" } |
| 20 | + ] |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +The SDK provides three core benefits: |
| 25 | + |
| 26 | +### Auto-generated from API specifications |
| 27 | + |
| 28 | +The SDK is automatically generated from OpenRouter's OpenAPI specs and updated with every API change. New models, parameters, and features appear in your IDE autocomplete immediately. No manual updates. No version drift. |
| 29 | + |
| 30 | +```typescript |
| 31 | +// When new models launch, they're available instantly |
| 32 | +const response = await client.chat.completions.create({ |
| 33 | + model: "minimax/minimax-m2", |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +### Type-safe by default |
| 38 | + |
| 39 | +Every parameter, response field, and configuration option is fully typed. Invalid configurations are caught at compile time, not in production. |
| 40 | + |
| 41 | +```typescript |
| 42 | +const response = await client.chat.completions.create({ |
| 43 | + model: "minimax/minimax-m2", |
| 44 | + messages: [ |
| 45 | + { role: "user", content: "Hello" } |
| 46 | + // ← Your IDE validates message structure |
| 47 | + ], |
| 48 | + temperature: 0.7, // ← Type-checked |
| 49 | + stream: true // ← Response type changes based on this |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +**Actionable error messages:** |
| 54 | + |
| 55 | +```typescript |
| 56 | +// Instead of generic errors, get specific guidance: |
| 57 | +// "Model 'openai/o1-preview' requires at least 2 messages. |
| 58 | +// You provided 1 message. Add a system or user message." |
| 59 | +``` |
| 60 | + |
| 61 | +**Type-safe streaming:** |
| 62 | + |
| 63 | +```typescript |
| 64 | +const stream = await client.chat.completions.create({ |
| 65 | + model: "minimax/minimax-m2", |
| 66 | + messages: [{ role: "user", content: "Write a story" }], |
| 67 | + stream: true |
| 68 | +}); |
| 69 | + |
| 70 | +for await (const chunk of stream) { |
| 71 | + // Full type information for streaming responses |
| 72 | + const content = chunk.choices[0]?.delta?.content; |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Installation |
| 77 | + |
| 78 | +```bash |
| 79 | +npm install @openrouter/sdk |
| 80 | +``` |
| 81 | + |
| 82 | +Get your API key from [openrouter.ai/settings/keys](https://openrouter.ai/settings/keys). |
| 83 | + |
| 84 | +## Quick start |
| 85 | + |
| 86 | +```typescript |
| 87 | +import OpenRouter from '@openrouter/sdk'; |
| 88 | + |
| 89 | +const client = new OpenRouter({ |
| 90 | + apiKey: process.env.OPENROUTER_API_KEY |
| 91 | +}); |
| 92 | + |
| 93 | +const response = await client.chat.completions.create({ |
| 94 | + model: "minimax/minimax-m2", |
| 95 | + messages: [ |
| 96 | + { role: "user", content: "Hello!" } |
| 97 | + ] |
| 98 | +}); |
| 99 | + |
| 100 | +console.log(response.choices[0].message.content); |
| 101 | +``` |
0 commit comments