Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created December 8, 2024 09:30
Show Gist options
  • Save tluyben/1e0c5f23be09573e9989d5abc83fac90 to your computer and use it in GitHub Desktop.
Save tluyben/1e0c5f23be09573e9989d5abc83fac90 to your computer and use it in GitHub Desktop.
Open router cors test
// Paste this in your browser's console
async function testOpenRouter() {
const API_KEY = 'YOUR_API_KEY_HERE'; // Replace with your actual API key
try {
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'HTTP-Referer': window.location.href, // OpenRouter requires this
'X-Title': 'API Test' // Optional - helps OpenRouter track usage
},
body: JSON.stringify({
model: 'anthropic/claude-3-opus', // Or any other supported model
messages: [{
role: 'user',
content: 'Say hello!'
}]
})
});
if (!response.ok) {
const error = await response.text();
throw new Error(`API error: ${error}`);
}
const data = await response.json();
console.log('Success!', data);
return data;
} catch (error) {
console.error('Error:', error);
return null;
}
}
// Run the test
testOpenRouter().then(result => {
if (result) {
console.log('Content:', result.choices[0]?.message?.content);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment