Skip to content

Commit 8098399

Browse files
Add basic OpenRouter SDK example extracted from wrapper repo
1 parent 909ba4d commit 8098399

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Example usage of the @openrouter/sdk package
3+
*
4+
* This demonstrates the OpenRouter TypeScript SDK's idiomatic usage patterns:
5+
* - Type-safe client initialization
6+
* - Non-streaming chat completions
7+
* - Streaming chat completions with async iteration
8+
* - Automatic usage tracking
9+
*
10+
* Run with: bun examples/basic/example-basic-openrouter-sdk.ts
11+
*/
12+
13+
import { OpenRouter } from "@openrouter/sdk";
14+
15+
// Initialize the OpenRouter SDK client
16+
// The SDK automatically reads OPENROUTER_API_KEY from environment
17+
const openRouter = new OpenRouter({
18+
apiKey: process.env.OPENROUTER_API_KEY ?? "",
19+
});
20+
21+
async function nonStreamingExample() {
22+
console.log("=== Non-Streaming Example ===\n");
23+
24+
// Basic chat completion - returns the full response at once
25+
const result = await openRouter.chat.send({
26+
model: "openai/gpt-4o-mini",
27+
messages: [
28+
{
29+
role: "user",
30+
content: "What is the capital of France?",
31+
},
32+
],
33+
stream: false, // Explicitly set stream to false for non-streaming
34+
});
35+
36+
// The SDK provides strong typing - result has 'choices' property
37+
if ("choices" in result && result.choices[0]) {
38+
console.log("Model:", result.model);
39+
console.log("Response:", result.choices[0].message.content);
40+
console.log("Usage:", result.usage);
41+
console.log();
42+
}
43+
}
44+
45+
async function streamingExample() {
46+
console.log("=== Streaming Example ===\n");
47+
48+
// Streaming chat completion - returns an async iterable
49+
const stream = await openRouter.chat.send({
50+
model: "openai/gpt-4o-mini",
51+
messages: [
52+
{
53+
role: "user",
54+
content: "Write a haiku about TypeScript",
55+
},
56+
],
57+
stream: true, // Enable streaming mode
58+
streamOptions: {
59+
includeUsage: true, // Include token usage in the final chunk
60+
},
61+
});
62+
63+
console.log("Streaming response:");
64+
let fullContent = "";
65+
66+
// The SDK returns an async iterable that you can iterate with for-await-of
67+
for await (const chunk of stream) {
68+
// Each chunk contains partial data
69+
if (chunk.choices?.[0]?.delta?.content) {
70+
const content = chunk.choices[0].delta.content;
71+
process.stdout.write(content); // Write without newline to see real-time streaming
72+
fullContent += content;
73+
}
74+
75+
// Usage stats are included in the final chunk when streamOptions.includeUsage is true
76+
if (chunk.usage) {
77+
console.log("\n\nStream usage:", chunk.usage);
78+
}
79+
}
80+
81+
console.log("\n\nFull response:", fullContent);
82+
console.log();
83+
}
84+
85+
async function main() {
86+
try {
87+
// Demonstrate both streaming and non-streaming modes
88+
await nonStreamingExample();
89+
await streamingExample();
90+
} catch (error) {
91+
console.error("Error:", error);
92+
process.exit(1);
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)