Skip to content
Merged
Changes from 2 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
146 changes: 99 additions & 47 deletions src/content/docs/ai-gateway/providers/openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: OpenAI
pcx_content_type: get-started
---

import { Tabs, TabItem } from "~/components";

[OpenAI](https://openai.com/about/) helps you build with ChatGPT.

## Endpoint
Expand All @@ -11,6 +13,18 @@ pcx_content_type: get-started
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai
```

### Chat completions endpoint

```txt
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
```

### Responses endpoint

```txt
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
```

## URL structure

When making requests to OpenAI, replace `https://api.openai.com/v1` in the URL you’re currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai`.
Expand All @@ -26,56 +40,94 @@ When making requests to OpenAI, ensure you have the following:

## Examples

### cURL

```bash title="Request"
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
--header 'Authorization: Bearer {openai_token}' \
--header 'Content-Type: application/json' \
--data ' {
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "What is Cloudflare"
}
]
}
'
```

### Use OpenAI SDK with JavaScript

If you are using a library like openai-node, set the `baseURL` to your OpenAI endpoint like this:

```js title="JavaScript"
import OpenAI from "openai";

const apiKey = "my api key"; // defaults to process.env["OPENAI_API_KEY"]
<Tabs syncKey="apiExamples">
<TabItem label="cURL">
<div>
<h3>Chat completions endpoint</h3>
<pre>
{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \\
--header 'Authorization: Bearer {openai_token}' \\
--header 'Content-Type: application/json' \\
--data '{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'`}
</pre>
<h3>Responses endpoint</h3>
<pre>
{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \\
--header 'Authorization: Bearer {openai_token}' \\
--header 'Content-Type: application/json' \\
--data '{
"model": "gpt-4.1",
"input": [
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}'`}
</pre>
</div>
</TabItem>
<TabItem label="JavaScript">
<div>
<h3>Chat completions endpoint</h3>
<pre>
{`import OpenAI from "openai";
const apiKey = "my api key";
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;

const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
const openai = new OpenAI({
apiKey,
baseURL,
apiKey,
baseURL,
});

try {
const model = "gpt-3.5-turbo-0613";
const messages = [{ role: "user", content: "What is a neuron?" }];
const maxTokens = 100;

const chatCompletion = await openai.chat.completions.create({
model,
messages,
max_tokens: maxTokens,
});

const response = chatCompletion.choices[0].message;

return new Response(JSON.stringify(response));
const model = "gpt-3.5-turbo-0613";
const messages = [{ role: "user", content: "What is a neuron?" }];
const maxTokens = 100;
const chatCompletion = await openai.chat.completions.create({
model,
messages,
max_tokens: maxTokens,
});
const response = chatCompletion.choices[0].message;
console.log(response);
} catch (e) {
return new Response(e);
}
```
console.error(e);
}`}
</pre>
<h3>Responses endpoint</h3>
<pre>
{`import OpenAI from "openai";
const apiKey = "my api key";
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
const openai = new OpenAI({
apiKey,
baseURL,
});
try {
const model = "gpt-4.1";
const input = [
{ role: "user", content: "Write a one-sentence bedtime story about a unicorn." }
];
const response = await openai.responses.create({
model,
input,
});
console.log(response.output_text);
} catch (e) {
console.error(e);
}`}
</pre>
</div>
</TabItem>
</Tabs>
Loading