Skip to content

Commit 3aefea8

Browse files
Run biome format
1 parent dbc5a38 commit 3aefea8

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
22
* Example: Using OpenRouter with raw fetch API
3-
*
3+
*
44
* This example demonstrates how to make direct HTTP requests to OpenRouter's API
55
* using the native fetch API without any additional libraries.
6-
*
6+
*
77
* To run: bun examples/basic/example-basic-fetch.ts
88
*/
99

1010
// Make this a module
1111
export {};
1212

1313
// OpenRouter API endpoint
14-
const OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions";
14+
const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';
1515

1616
// Type definitions for the API response
1717
interface ChatCompletionResponse {
@@ -34,78 +34,76 @@ interface ChatCompletionResponse {
3434

3535
// Request payload following OpenAI-compatible chat completions format
3636
const requestBody = {
37-
model: "openai/gpt-4o-mini",
37+
model: 'openai/gpt-4o-mini',
3838
messages: [
3939
{
40-
role: "user",
41-
content: "Write a haiku about TypeScript",
40+
role: 'user',
41+
content: 'Write a haiku about TypeScript',
4242
},
4343
],
4444
};
4545

46-
console.log("=== OpenRouter Raw Fetch Example ===\n");
47-
console.log("Request:");
46+
console.log('=== OpenRouter Raw Fetch Example ===\n');
47+
console.log('Request:');
4848
console.log(`URL: ${OPENROUTER_API_URL}`);
49-
console.log("Model:", requestBody.model);
50-
console.log("Message:", requestBody.messages[0]?.content);
49+
console.log('Model:', requestBody.model);
50+
console.log('Message:', requestBody.messages[0]?.content);
5151
console.log();
5252

5353
try {
5454
// Ensure API key is available
5555
if (!process.env.OPENROUTER_API_KEY) {
56-
throw new Error("OPENROUTER_API_KEY environment variable is not set");
56+
throw new Error('OPENROUTER_API_KEY environment variable is not set');
5757
}
5858

5959
// Make the HTTP POST request to OpenRouter
6060
const response = await fetch(OPENROUTER_API_URL, {
61-
method: "POST",
61+
method: 'POST',
6262
headers: {
6363
// Required: Authorization header with your API key
64-
"Authorization": `Bearer ${process.env.OPENROUTER_API_KEY}`,
64+
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
6565
// Required: Content type for JSON payload
66-
"Content-Type": "application/json",
66+
'Content-Type': 'application/json',
6767
// Optional but recommended: Identify your app
68-
"HTTP-Referer": "https://github.com/openrouter/examples",
69-
"X-Title": "OpenRouter Fetch Example",
68+
'HTTP-Referer': 'https://github.com/openrouter/examples',
69+
'X-Title': 'OpenRouter Fetch Example',
7070
},
7171
body: JSON.stringify(requestBody),
7272
});
7373

7474
// Check if the request was successful
7575
if (!response.ok) {
7676
const errorText = await response.text();
77-
throw new Error(
78-
`HTTP error! status: ${response.status}, body: ${errorText}`
79-
);
77+
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);
8078
}
8179

8280
// Parse the JSON response
83-
const data = await response.json() as ChatCompletionResponse;
81+
const data = (await response.json()) as ChatCompletionResponse;
8482

8583
// Display the response
86-
console.log("Response:");
87-
console.log("Status:", response.status, response.statusText);
88-
console.log("Model used:", data.model);
89-
console.log("\nGenerated content:");
84+
console.log('Response:');
85+
console.log('Status:', response.status, response.statusText);
86+
console.log('Model used:', data.model);
87+
console.log('\nGenerated content:');
9088
console.log(data.choices[0]?.message?.content);
91-
console.log("\nUsage stats:");
92-
console.log("- Prompt tokens:", data.usage.prompt_tokens);
93-
console.log("- Completion tokens:", data.usage.completion_tokens);
94-
console.log("- Total tokens:", data.usage.total_tokens);
89+
console.log('\nUsage stats:');
90+
console.log('- Prompt tokens:', data.usage.prompt_tokens);
91+
console.log('- Completion tokens:', data.usage.completion_tokens);
92+
console.log('- Total tokens:', data.usage.total_tokens);
9593

9694
// Optional: Show raw response structure
9795
if (process.env.DEBUG) {
98-
console.log("\nFull response object:");
96+
console.log('\nFull response object:');
9997
console.log(JSON.stringify(data, null, 2));
10098
}
10199
} catch (error) {
102-
console.error("Error making request to OpenRouter:");
103-
100+
console.error('Error making request to OpenRouter:');
101+
104102
if (error instanceof Error) {
105-
console.error("Error message:", error.message);
103+
console.error('Error message:', error.message);
106104
} else {
107-
console.error("Unknown error:", error);
105+
console.error('Unknown error:', error);
108106
}
109-
107+
110108
process.exit(1);
111109
}

0 commit comments

Comments
 (0)