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
87 changes: 87 additions & 0 deletions typescript/fetch/src/basic/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Example: Using OpenRouter with raw fetch API
*
* This example demonstrates how to make direct HTTP requests to OpenRouter's API
* using the native fetch API without any additional libraries.
*/

import type { ChatCompletionResponse } from '@openrouter-examples/shared/types';

// OpenRouter API endpoint
const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';

// Request payload following OpenAI-compatible chat completions format
const requestBody = {
model: 'openai/gpt-4o-mini',
messages: [
{
role: 'user',
content: 'Write a haiku about TypeScript',
},
],
};

console.log('=== OpenRouter Raw Fetch Example ===\n');
console.log('Request:');
console.log(`URL: ${OPENROUTER_API_URL}`);
console.log('Model:', requestBody.model);
console.log('Message:', requestBody.messages[0]?.content);
console.log();

try {
Copy link

Choose a reason for hiding this comment

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

top-level fetch?

// Ensure API key is available
if (!process.env.OPENROUTER_API_KEY) {
throw new Error('OPENROUTER_API_KEY environment variable is not set');
}

// Make the HTTP POST request to OpenRouter
const response = await fetch(OPENROUTER_API_URL, {
method: 'POST',
headers: {
// Required: Authorization header with your API key
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
// Required: Content type for JSON payload
'Content-Type': 'application/json',
// Optional but recommended: Identify your app
'HTTP-Referer': 'https://github.com/openrouter/examples',
'X-Title': 'OpenRouter Fetch Example',
},
body: JSON.stringify(requestBody),
});

// Check if the request was successful
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);
}

// Parse the JSON response
const data = (await response.json()) as ChatCompletionResponse;

// Display the response
console.log('Response:');
console.log('Status:', response.status, response.statusText);
console.log('Model used:', data.model);
console.log('\nGenerated content:');
console.log(data.choices[0]?.message?.content);
console.log('\nUsage stats:');
console.log('- Prompt tokens:', data.usage.prompt_tokens);
console.log('- Completion tokens:', data.usage.completion_tokens);
console.log('- Total tokens:', data.usage.total_tokens);

// Optional: Show raw response structure
if (process.env.DEBUG) {
console.log('\nFull response object:');
console.log(JSON.stringify(data, null, 2));
}
} catch (error) {
console.error('Error making request to OpenRouter:');

if (error instanceof Error) {
console.error('Error message:', error.message);
} else {
console.error('Unknown error:', error);
}

process.exit(1);
}