@@ -3,6 +3,8 @@ title: OpenAI
33pcx_content_type : get-started
44---
55
6+ import { Tabs , TabItem } from " ~/components" ;
7+
68[ OpenAI] ( https://openai.com/about/ ) helps you build with ChatGPT.
79
810## Endpoint
@@ -11,6 +13,18 @@ pcx_content_type: get-started
1113https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai
1214```
1315
16+ ### Chat completions endpoint
17+
18+ ``` txt
19+ https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
20+ ```
21+
22+ ### Responses endpoint
23+
24+ ``` txt
25+ https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
26+ ```
27+
1428## URL structure
1529
1630When 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 ` .
@@ -24,34 +38,31 @@ When making requests to OpenAI, ensure you have the following:
2438- An active OpenAI API token.
2539- The name of the OpenAI model you want to use.
2640
27- ## Examples
41+ ## Chat completions endpoint
2842
29- ### cURL
43+ ### cURL example
3044
31- ``` bash title="Request"
45+ ``` bash
3246curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
33- --header ' Authorization: Bearer {openai_token}' \
34- --header ' Content-Type: application/json' \
35- --data ' {
36- "model": "gpt-4o-mini",
37- "messages": [
38- {
39- "role": "user",
40- "content": "What is Cloudflare"
41- }
42- ]
43- }
44- '
47+ --header ' Authorization: Bearer {openai_token}' \
48+ --header ' Content-Type: application/json' \
49+ --data ' {
50+ "model": "gpt-4o-mini",
51+ "messages": [
52+ {
53+ "role": "user",
54+ "content": "What is Cloudflare?"
55+ }
56+ ]
57+ }'
4558```
4659
47- ### Use OpenAI SDK with JavaScript
48-
49- If you are using a library like openai-node, set the ` baseURL ` to your OpenAI endpoint like this:
60+ ### JavaScript SDK example
5061
51- ``` js title="JavaScript"
62+ ``` js
5263import OpenAI from " openai" ;
5364
54- const apiKey = " my api key" ; // defaults to process.env["OPENAI_API_KEY"]
65+ const apiKey = " my api key" ; // or process.env["OPENAI_API_KEY"]
5566const accountId = " {account_id}" ;
5667const gatewayId = " {gateway_id}" ;
5768const baseURL = ` https://gateway.ai.cloudflare.com/v1/${ accountId} /${ gatewayId} /openai` ;
@@ -65,17 +76,66 @@ try {
6576 const model = " gpt-3.5-turbo-0613" ;
6677 const messages = [{ role: " user" , content: " What is a neuron?" }];
6778 const maxTokens = 100 ;
68-
6979 const chatCompletion = await openai .chat .completions .create ({
7080 model,
7181 messages,
7282 max_tokens: maxTokens,
7383 });
74-
7584 const response = chatCompletion .choices [0 ].message ;
85+ console .log (response);
86+ } catch (e) {
87+ console .error (e);
88+ }
89+ ```
90+
91+ ## OpenAI Responses endpoint
92+
93+ ### cURL example
94+
95+ ``` bash
96+ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
97+ --header ' Authorization: Bearer {openai_token}' \
98+ --header ' Content-Type: application/json' \
99+ --data ' {
100+ "model": "gpt-4.1",
101+ "input": [
102+ {
103+ "role": "user",
104+ "content": "Write a one-sentence bedtime story about a unicorn."
105+ }
106+ ]
107+ }'
108+ ```
109+
110+ ### JavaScript SDK example
111+
112+ ``` js
113+ import OpenAI from " openai" ;
114+
115+ const apiKey = " my api key" ; // or process.env["OPENAI_API_KEY"]
116+ const accountId = " {account_id}" ;
117+ const gatewayId = " {gateway_id}" ;
118+ const baseURL = ` https://gateway.ai.cloudflare.com/v1/${ accountId} /${ gatewayId} /openai` ;
76119
77- return new Response (JSON .stringify (response));
120+ const openai = new OpenAI ({
121+ apiKey,
122+ baseURL,
123+ });
124+
125+ try {
126+ const model = " gpt-4.1" ;
127+ const input = [
128+ {
129+ role: " user" ,
130+ content: " Write a one-sentence bedtime story about a unicorn." ,
131+ },
132+ ];
133+ const response = await openai .responses .create ({
134+ model,
135+ input,
136+ });
137+ console .log (response .output_text );
78138} catch (e) {
79- return new Response (e);
139+ console . error (e);
80140}
81141```
0 commit comments