Last active
November 24, 2025 20:41
-
-
Save thebongy/b810b0da172bfa7a2dc8f23f0d9a7fd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import OpenAI from "openai"; | |
| // Cloudflare AI Gateway configuration | |
| // Replace these with your actual Cloudflare account details | |
| const CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID; | |
| const CLOUDFLARE_GATEWAY_ID = process.env.CLOUDFLARE_GATEWAY_ID; | |
| const CF_AI_GATEWAY_TOKEN = process.env.CF_AI_GATEWAY_TOKEN; | |
| // Construct the Cloudflare AI Gateway base URL | |
| // Format: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai | |
| const baseURL = `https://gateway.ai.cloudflare.com/v1/${CLOUDFLARE_ACCOUNT_ID}/${CLOUDFLARE_GATEWAY_ID}/compat`; | |
| const client = new OpenAI({ | |
| apiKey: CF_AI_GATEWAY_TOKEN, | |
| baseURL: baseURL, | |
| }); | |
| async function main() { | |
| console.log("Sending request through Cloudflare AI Gateway..."); | |
| console.log(`Base URL: ${baseURL}\n`); | |
| try { | |
| const response = await client.chat.completions.create({ | |
| model: "openai/gpt-5-nano", | |
| messages: [ | |
| { | |
| role: "system", | |
| content: "You are a helpful assistant.", | |
| }, | |
| { | |
| role: "user", | |
| content: "What is Cloudflare AI Gateway and what are its benefits?", | |
| }, | |
| ], | |
| }); | |
| console.log("Response received:\n"); | |
| console.log(response.choices[0].message.content); | |
| } catch (error) { | |
| console.error("Error:", error.message); | |
| if (error.status) { | |
| console.error("Status:", error.status); | |
| } | |
| } | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment