|
| 1 | +--- |
| 2 | +title: OpenRouter |
| 3 | +pcx_content_type: get-started |
| 4 | +sidebar: |
| 5 | + badge: |
| 6 | + text: Beta |
| 7 | +--- |
| 8 | + |
| 9 | +[OpenRouter](https://openrouter.ai/) is a platform that provides a unified interface for accessing and using large language models (LLMs). |
| 10 | + |
| 11 | +## Endpoint |
| 12 | + |
| 13 | +```txt |
| 14 | +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter |
| 15 | +``` |
| 16 | + |
| 17 | +## URL structure |
| 18 | + |
| 19 | +When making requests to [OpenRouter](https://openrouter.ai/), replace `https://openrouter.ai/api/v1/chat/completions` in the URL you are currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter`. |
| 20 | + |
| 21 | +## Examples |
| 22 | + |
| 23 | +### cURL |
| 24 | + |
| 25 | +```bash title="Request" |
| 26 | +curl -X POST https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter/v1/chat/completions \ |
| 27 | + --header 'content-type: application/json' \ |
| 28 | + --header 'Authorization: Bearer OPENROUTER_TOKEN' \ |
| 29 | + --data '{ |
| 30 | + "model": "openai/gpt-3.5-turbo", |
| 31 | + "messages": [ |
| 32 | + { |
| 33 | + "role": "user", |
| 34 | + "content": "What is Cloudflare?" |
| 35 | + } |
| 36 | + ] |
| 37 | +}' |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | +### OpenAI SDK with JavaScript |
| 42 | + |
| 43 | +If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: |
| 44 | + |
| 45 | +```js title="JavaScript" |
| 46 | +import OpenAI from 'openai'; |
| 47 | + |
| 48 | +const openai = new OpenAI({ |
| 49 | + apiKey: env.OPENROUTER_TOKEN, |
| 50 | + baseURL: "https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter" |
| 51 | +}); |
| 52 | + |
| 53 | +try { |
| 54 | + const chatCompletion = await openai.chat.completions.create({ |
| 55 | + model: "openai/gpt-3.5-turbo", |
| 56 | + messages: [{ role: "user", content: "What is Cloudflare?" }] |
| 57 | + }); |
| 58 | + |
| 59 | + const response = chatCompletion.choices[0].message; |
| 60 | + |
| 61 | + return new Response(JSON.stringify(response)); |
| 62 | +} catch (e) { |
| 63 | + return new Response(e); |
| 64 | +} |
| 65 | + |
| 66 | +``` |
0 commit comments