Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions typescript/openrouter-sdk/src/basic/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Example usage of the @openrouter/sdk package
*
* This demonstrates the OpenRouter TypeScript SDK's idiomatic usage patterns:
* - Type-safe client initialization
* - Non-streaming chat completions
* - Streaming chat completions with async iteration
* - Automatic usage tracking
*/

import { OpenRouter } from '@openrouter/sdk';

// Initialize the OpenRouter SDK client
// The SDK automatically reads OPENROUTER_API_KEY from environment
const openRouter = new OpenRouter({
apiKey: process.env.OPENROUTER_API_KEY ?? '',
});

async function nonStreamingExample() {
console.log('=== Non-Streaming Example ===\n');

// Basic chat completion - returns the full response at once
const result = await openRouter.chat.send({
model: 'openai/gpt-4o-mini',
messages: [
{
role: 'user',
content: 'What is the capital of France?',
},
],
stream: false, // Explicitly set stream to false for non-streaming
});

// The SDK provides strong typing - result has 'choices' property
if ('choices' in result && result.choices[0]) {
console.log('Model:', result.model);
console.log('Response:', result.choices[0].message.content);
console.log('Usage:', result.usage);
console.log();
}
}

async function streamingExample() {
console.log('=== Streaming Example ===\n');

// Streaming chat completion - returns an async iterable
const stream = await openRouter.chat.send({
model: 'openai/gpt-4o-mini',
messages: [
{
role: 'user',
content: 'Write a haiku about TypeScript',
},
],
stream: true, // Enable streaming mode
streamOptions: {
includeUsage: true, // Include token usage in the final chunk
},
});

console.log('Streaming response:');
let fullContent = '';

// The SDK returns an async iterable that you can iterate with for-await-of
for await (const chunk of stream) {
// Each chunk contains partial data
if (chunk.choices?.[0]?.delta?.content) {
const content = chunk.choices[0].delta.content;
process.stdout.write(content); // Write without newline to see real-time streaming
fullContent += content;
}
Comment on lines +62 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love to pull this out into a function


// Usage stats are included in the final chunk when streamOptions.includeUsage is true
if (chunk.usage) {
console.log('\n\nStream usage:', chunk.usage);
}
}

console.log('\n\nFull response:', fullContent);
console.log();
}

async function main() {
try {
// Demonstrate both streaming and non-streaming modes
await nonStreamingExample();
await streamingExample();
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}

main();
Comment on lines +83 to +94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pick 1 pattern -- either try/catch or main().catch -- generally prefer just naked call or have an utils to wrap the main function