Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created December 8, 2024 09:30
Show Gist options
  • Select an option

  • Save tluyben/1e0c5f23be09573e9989d5abc83fac90 to your computer and use it in GitHub Desktop.

Select an option

Save tluyben/1e0c5f23be09573e9989d5abc83fac90 to your computer and use it in GitHub Desktop.

Revisions

  1. tluyben created this gist Dec 8, 2024.
    43 changes: 43 additions & 0 deletions openroutercors.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // 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);
    }
    });