1# Use API/provider credentials through a gateway1# Connect to a gateway
2 2
3> For the complete documentation index, see [llms.txt](https://learn.chatgpt.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.3> For the complete documentation index, see [llms.txt](https://learn.chatgpt.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.
4 4
5Connect Codex to an LLM gateway using the gateway URL, model alias, and credential5Connect Codex to your organization's model gateway using the provider settings and authentication method your gateway team supplies. The gateway routes model requests to your backend, such as Amazon Bedrock.
6or token resolver your organization provides.
7
8To keep the gateway while signing in with a ChatGPT workspace, use
9[Sign in with ChatGPT through a gateway](https://learn.chatgpt.com/docs/enterprise/sign-in-with-chatgpt-through-a-gateway).
10 6
11For an organization-wide rollout, see [Roll out a7For an organization-wide rollout, see [Roll out a
12 gateway](https://learn.chatgpt.com/docs/enterprise/roll-out-a-gateway). For the required API behavior,8 gateway](https://learn.chatgpt.com/docs/enterprise/roll-out-a-gateway). For the required API behavior,
35 31
36- The HTTPS gateway base URL, including its API path, such as `https://gateway.example.com/v1`.32- The HTTPS gateway base URL, including its API path, such as `https://gateway.example.com/v1`.
37- The model name and provider ID to use.33- The model name and provider ID to use.
38- Your scoped gateway credential and its environment variable, or an installed34- The authentication method: [native gateway OAuth](https://learn.chatgpt.com/docs/enterprise/connect-to-a-gateway?gateway-auth=oauth#use-native-gateway-oauth),
39 token resolver and its configuration.35 a scoped API key and its environment variable, or an installed credential helper.
36- For OAuth, the authorization URL, token URL, public client ID, required scopes,
37 and the cookie or custom header the gateway accepts.
40- Any required model catalog URL, or a catalog file and its absolute local path.38- Any required model catalog URL, or a catalog file and its absolute local path.
41 39
40<a id="without-chatgpt-sign-in"></a>
41
42## Configure the provider42## Configure the provider
43 43
44Open `config.toml` at `~/.codex/config.toml` on macOS or Linux, or44Open `config.toml` at `~/.codex/config.toml` on macOS or Linux, or
63name = "Organization Gateway"63name = "Organization Gateway"
64base_url = "https://gateway.example.com/v1"64base_url = "https://gateway.example.com/v1"
65wire_api = "responses"65wire_api = "responses"
66env_key = "CODEX_GATEWAY_API_KEY"66requires_openai_auth = false
67```67```
68 68
69If your administrator supplies a catalog URL, add it to the existing69Then add the [authentication settings](#choose-an-authentication-method) your
70gateway team supplies.
71
72For API-key or helper authentication, if your administrator supplies a catalog
73URL, add it to the existing
70`[model_providers.enterprise-gateway]` table:74`[model_providers.enterprise-gateway]` table:
71 75
72```toml76```toml
89add a catalog path unless the file exists at that location.93add a catalog path unless the file exists at that location.
90 94
91`enterprise-gateway` is an illustrative provider ID. Use the same ID in95`enterprise-gateway` is an illustrative provider ID. Use the same ID in
92`model_provider`, `[model_providers.<id>]`, and `[model_providers.<id>.auth]`.96`model_provider` and its provider and authentication tables.
93This example disables web search97This example disables web search
94for the initial connection test; your administrator should verify feature support98for the initial connection test; your administrator should verify feature support
95before enabling it.99before enabling it.
96 100
101## Choose an authentication method
102
103Use native gateway OAuth for browser sign-in, an API key for a scoped credential, or a credential helper if your organization supplies a command.
104
105{/* prettier-ignore */}
106<Tabs
107 id="gateway-authentication"
108 ariaLabel="Gateway authentication method"
109 param="gateway-auth"
110 defaultTab="oauth"
111 tabs={[
112 { id: "oauth", label: "Gateway OAuth" },
113 { id: "api-key", label: "API key" },
114 { id: "helper", label: "Credential helper" },
115 ]}
116>
117
118
119
120<a id="option-b-use-native-gateway-oauth"></a>
121
122### Use native gateway OAuth
123
124Gateway OAuth works independently of ChatGPT sign-in. Use it when your gateway accepts access tokens issued by your
125organization's identity provider, such as Microsoft Entra ID. Codex handles the
126browser sign-in, encrypted credential storage, and token refresh when the
127authorization server issues a refresh token.
128
129Your gateway team must register a public OAuth client that supports the
130authorization code flow with PKCE. Add this table to the provider configuration
131in `config.toml`, replacing the URLs, client ID, and cookie name with their values:
132
133```toml
134[model_providers.enterprise-gateway.gateway_oauth]
135authorization_url = "https://login.example.com/oauth/authorize"
136token_url = "https://login.example.com/oauth/token"
137client_id = "YOUR_PUBLIC_CLIENT_ID"
138delivery = { kind = "cookie", name = "auth-openid" }
139# redirect_port = 43127
140```
141
142Add `scopes` and, if required, `resource` to this table using the values your
143identity provider and gateway require. Request the scopes needed for refresh
144tokens if your provider requires them. Codex uses an available loopback port by default.
145If your identity provider requires a fixed redirect URI, set `redirect_port` and
146register `http://127.0.0.1:43127/callback` for this example.
147
148The gateway must validate the OAuth access token received in the configured
149cookie. If it accepts a custom header instead, replace `delivery` with the header
150your administrator provides, for example:
151
152```toml
153delivery = { kind = "header", name = "X-Gateway-Token", scheme = "Bearer" }
154```
155
156Codex reserves `Authorization`; you can't use it as the `gateway_oauth` delivery header.
157A gateway that accepts only a static API key must add support for the OAuth token
158and its delivery method before you can use this flow. The gateway continues to
159manage its own upstream credentials, such as its AWS credentials for Bedrock.
160
161When OAuth is your only credential, keep `requires_openai_auth = false` and remove the
162previous `env_key`, `experimental_bearer_token`, or `auth` settings used for the
163gateway credential. Use a local `model_catalog_json` file if you need custom model
164metadata; OAuth alone doesn't enable remote catalog discovery.
165
166Restart the desktop app and follow its gateway sign-in prompt to open the browser.
167In the CLI, start `codex` and follow the browser sign-in when Codex first accesses
168the gateway. Complete your organization's sign-in and MFA, then return to the
169client. If the grant expires or Codex can't refresh it, sign in again when prompted.
170
171Native gateway OAuth requires a browser and a reachable loopback callback. It has
172no device-code fallback. If the CLI runs over SSH, set `redirect_port = 43127` in
173the OAuth table and register the matching redirect URI described above. From the
174machine with your browser, forward that port before starting Codex:
175
176```shell
177ssh -L 43127:127.0.0.1:43127 user@remote
178```
179
180In that SSH session, start `codex` and open the gateway sign-in URL in your local
181browser. A CLI inside a container also needs a callback reachable from the
182browser. If you can't make the callback reachable, use an API key or credential
183helper instead.
184
185
186
187
188
189
190### Use an API key
191
192Use this setting when the gateway expects an API key in the `Authorization` header.
193
194Add the following setting inside the existing
195`[model_providers.enterprise-gateway]` table:
196
197```toml
198env_key = "CODEX_GATEWAY_API_KEY"
199```
200
97Make your gateway credential available as `CODEX_GATEWAY_API_KEY` in the201Make your gateway credential available as `CODEX_GATEWAY_API_KEY` in the
98environment of the process that launches Codex, using your organization's secret202environment of the process that launches Codex, using your organization's secret
99delivery mechanism. Don't put the credential in TOML or a repository. A variable203delivery mechanism. Don't put the credential in TOML or a repository. A variable
100set in a terminal may not be available to an app launched from the desktop.204set in a terminal may not be available to an app launched from the desktop.
101 205
206<a id="option-a-reuse-your-credential-command-with-a-helper"></a>
207
102### Use a custom authentication header208### Use a custom authentication header
103 209
104If your gateway requires a header such as `X-API-Key` instead of a bearer token,210If your gateway requires a header such as `X-API-Key` instead of a bearer token,
113See the [configuration reference](https://learn.chatgpt.com/docs/config-file/config-reference) for219See the [configuration reference](https://learn.chatgpt.com/docs/config-file/config-reference) for
114`model_providers.<id>.env_http_headers`.220`model_providers.<id>.env_http_headers`.
115 221
222With ChatGPT sign-in, use the custom header for the gateway key so Codex can send the user's OpenAI credential in `Authorization`.
223
116If the custom header is your only authentication mechanism, use224If the custom header is your only authentication mechanism, use
117`model_catalog_json`; remote catalog discovery won't run.225`model_catalog_json`; remote catalog discovery won't run.
118 226
121If your administrator provides command-backed authentication, use their installed234If your administrator provides command-backed authentication, use their installed
122helper and configuration instead of `env_key`. Don't configure both mechanisms.235helper and configuration instead of `env_key`. Don't configure both mechanisms.
123The helper must exist on your machine; Codex doesn't install it. For example,236The helper must exist on your machine; Codex doesn't install it. For example,
124replace the example `env_key` setting with this table, using the resolver path and237add this table without `env_key`, using the resolver path and
125arguments your administrator supplies:238arguments your administrator supplies:
126 239
127```toml240```toml
137contract. Ask your administrator how to renew your sign-in if the helper can no250contract. Ask your administrator how to renew your sign-in if the helper can no
138longer retrieve a token.251longer retrieve a token.
139 252
253With ChatGPT sign-in, run the helper before launching Codex and pass its output through a [custom gateway header](https://learn.chatgpt.com/docs/enterprise/connect-to-a-gateway?gateway-auth=api-key#use-a-custom-authentication-header) instead of configuring the `auth` table. When the key expires, get a new key and relaunch Codex.
254
140Use resolved absolute paths for helper executables and catalog files.255Use resolved absolute paths for helper executables and catalog files.
141 256
257
258
259</Tabs>
260
261<a id="why-you-would-want-this"></a>
262<a id="how-to-set-it-up"></a>
263
264<a id="sign-in-with-chatgpt-through-a-gateway"></a>
265<a id="enable-chatgpt-sign-in-optional"></a>
266
267## Enable ChatGPT sign-in (recommended)
268
269If your organization uses a ChatGPT workspace, signing in gives users access to eligible features such as first-party plugins. Your workspace controls apply, and your gateway still handles model requests. [Compare feature availability](https://learn.chatgpt.com/docs/pricing#feature-availability).
270
271Add the gateway provider to your workspace's [managed `requirements.toml`](https://learn.chatgpt.com/docs/enterprise/managed-configuration). These settings apply when users sign in to that workspace.
272
273```toml
274# requirements.toml
275model_provider = "enterprise-gateway"
276
277[model_providers.enterprise-gateway]
278name = "Enterprise gateway"
279base_url = "https://gateway.example.com/backend-api/codex"
280requires_openai_auth = true
281```
282
283Configure [gateway authentication](#choose-an-authentication-method) as described above. Codex sends the user's OpenAI credential to your gateway in `Authorization`. Use a separate header for the gateway credential. With native gateway OAuth, `gateway_oauth.delivery` can specify a header or a cookie.
284
285<a id="keep-the-credentials-on-their-own-paths"></a>
286
287### Handle credentials at the gateway
288
289The gateway receives two credentials on each model request. For example, when Codex sends the gateway OAuth token in a cookie:
290
291```http
292POST /backend-api/codex/responses
293Authorization: Bearer <ChatGPT token>
294ChatGPT-Account-ID: <workspace ID, when available>
295Cookie: auth-openid=<gateway token>
296```
297
298Validate the gateway credential and strip its cookie or custom header. Redact credentials and sensitive request data from logs. Choose which credential to use upstream:
299
300- **Forward the user's credential for direct OpenAI inference (recommended).** This lets OpenAI attribute usage and apply inference settings for that user. For this route, the gateway must proxy `/models` and `/responses` to `https://chatgpt.com/backend-api/codex` and forward `Authorization` and any `ChatGPT-Account-ID` header unchanged.
301- **Let the gateway authenticate to the inference provider.** Use the credentials configured on the gateway for that provider. Leave the user's OpenAI token and any `ChatGPT-Account-ID` header out of the upstream request.
302
303These diagrams show the recommended route for OpenAI inference.
304
305<details>
306<summary>How a gateway key works with ChatGPT sign-in</summary>
307
308
309
310
311
312
313
314
315
316
317
318
319
320</details>
321
322<details>
323<summary>How gateway OAuth works with ChatGPT sign-in</summary>
324
325
326
327
328
329
330
331
332
333
334
335
336
337</details>
338
339### Add routing headers (optional)
340
341To route requests by configuration or tool, add headers to the same `enterprise-gateway` provider in your cloud-managed `requirements.toml`:
342
343```toml
344[model_providers.enterprise-gateway.http_headers]
345x-portkey-config = "<gateway-routing-config-id>"
346x-portkey-metadata = '{"tool_id":"codex"}'
347```
348
349Use `env_http_headers` for values supplied by the client environment. Send a test request and confirm that the gateway selects the intended route. See [Custom model providers](https://learn.chatgpt.com/docs/config-file/config-advanced#custom-model-providers) for more header options.
350
351<a id="caveats-and-limitations"></a>
352<a id="voice-through-a-gateway"></a>
353
354Only model-provider requests use this gateway route. [Voice sessions](https://learn.chatgpt.com/docs/features/voice) use a separate route. Workspace sign-in, MCP servers, and plugins can also use other network paths.
355
356## Start the client
357
358With ChatGPT sign-in, start the client and sign in to the workspace that provides the managed configuration. If you use native gateway OAuth, also complete your organization's gateway sign-in when prompted. Then [verify the connection](#verify-the-connection).
359
360For a locally configured provider, follow the instructions for your client.
361
142### Configure the CLI362### Configure the CLI
143 363
144The CLI reads `~/.codex/config.toml` by default on macOS or Linux. After saving364The CLI reads `~/.codex/config.toml` by default on macOS or Linux. After saving
189your administrator to confirm that the gateway recorded your user, model alias,409your administrator to confirm that the gateway recorded your user, model alias,
190and intended upstream route. Don't identify the model by asking it its name.410and intended upstream route. Don't identify the model by asking it its name.
191 411
412If the gateway serves the model catalog, check that it returns a Codex-compatible `models` array. For OpenAI inference using the user's credential, verify that the gateway forwards the ChatGPT headers to the Codex backend.
413
192This verifies an initial connection. Administrators should also complete the414This verifies an initial connection. Administrators should also complete the
193[rollout checks](https://learn.chatgpt.com/docs/enterprise/roll-out-a-gateway#test-the-client-and-gateway)415[rollout checks](https://learn.chatgpt.com/docs/enterprise/roll-out-a-gateway#test-the-client-and-gateway)
194for streaming, tools, and follow-up turns.416for streaming, tools, and follow-up turns.
198| Symptom | What to check |420| Symptom | What to check |
199| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |421| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
200| The expected provider isn't active. | Check the selected profile and configuration precedence. Confirm that top-level keys aren't inside a provider table. |422| The expected provider isn't active. | Check the selected profile and configuration precedence. Confirm that top-level keys aren't inside a provider table. |
201| Authentication fails. | Check that the credential variable reaches the client process, or that the installed helper can retrieve a current token. Ask the administrator to distinguish gateway authentication from upstream authentication. |423| OAuth sign-in or token delivery fails. | Check the public client registration, redirect URI, scopes, and configured cookie or header. Confirm that the gateway accepts the issued access token. |
424| API-key or helper authentication fails. | Check that the credential variable reaches the client process, or that the installed helper can retrieve a current token. Ask the administrator to distinguish gateway authentication from upstream authentication. |
202| The model isn't found. | Confirm the supplied model name and ask the administrator to check its route. |425| The model isn't found. | Confirm the supplied model name and ask the administrator to check its route. |
203| The model uses unexpected capabilities. | Ask the administrator to check that the catalog metadata matches the model behind the alias. |426| The model uses unexpected capabilities. | Ask the administrator to check that the catalog metadata matches the model behind the alias. |
204| Streaming stalls or follow-ups fail. | Ask the gateway owner to check proxy buffering, the terminal `response.completed` event, and [Gateway compatibility](https://learn.chatgpt.com/docs/enterprise/gateway-compatibility). |427| Streaming stalls or follow-ups fail. | Ask the gateway owner to check proxy buffering, the terminal `response.completed` event, and [Gateway compatibility](https://learn.chatgpt.com/docs/enterprise/gateway-compatibility). |
2172. Ask your gateway team to confirm that the gateway supports the4402. Ask your gateway team to confirm that the gateway supports the
218 [API behavior Codex requires](https://learn.chatgpt.com/docs/enterprise/gateway-compatibility) and to441 [API behavior Codex requires](https://learn.chatgpt.com/docs/enterprise/gateway-compatibility) and to
219 configure a Codex model route.442 configure a Codex model route.
2203. Obtain a scoped gateway credential or credential helper, the model name, and4433. Obtain the OAuth settings, scoped gateway credential, or credential helper,
221 any required model catalog from your gateway team.444 along with the model name and any required catalog from your gateway team.
2224. [Configure Codex](#configure-the-provider) with those values.4454. [Configure Codex](#configure-the-provider) with those values.
2235. [Verify the connection](#verify-the-connection) in the CLI or desktop app you4465. [Verify the connection](#verify-the-connection) in the CLI or desktop app you
224 plan to use. Have your gateway team complete the447 plan to use. Have your gateway team complete the