> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ruapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Codex: “Token exchange failed” — why you can't sign in

> “Token exchange failed” when signing in to Codex: how “error sending request” differs from “403 Forbidden”, what to check, and how to use an API key without a ChatGPT login.

**Short answer:** `Token exchange failed` appears at the end of a ChatGPT login: you have already signed in in the browser, but Codex itself couldn't get a token from `auth.openai.com`. Either it couldn't reach that address, or the address answered with a 403. The browser and Codex use the network in different ways, so “everything opens in the browser” doesn't prove much. You can fix the network and proxy with the steps below — or skip the login: with a key from RuAPI (an independent API gateway that works from Russia), Codex needs neither a ChatGPT account nor a VPN.

## Which error do you have

| Text | What it means |
| - | - |
| `Token exchange failed: error sending request for url (https://auth.openai.com/oauth/token)` | Codex couldn't connect to the sign-in address: network, proxy or certificate |
| `Token exchange failed: token endpoint returned status 403 Forbidden` | The connection works, but the sign-in address refused. The cause may be a proxy or a VPN, or the country the request came from |
| `unexpected status 403 Forbidden: …` | This isn't the login any more: an ordinary request to the model was refused. Read the text after the colon — the reason is there |

In the app, the same sign-in error may be called `token_exchange_failed`. The texts were checked against the Codex source code ([server.rs](https://github.com/openai/codex/blob/main/codex-rs/login/src/server.rs), [oauth/error.rs](https://github.com/openai/codex/blob/main/codex-rs/login/src/oauth/error.rs), [protocol/error.rs](https://github.com/openai/codex/blob/main/codex-rs/protocol/src/error.rs)) on 25 November 2026.

## Why it happens

* **Codex requests the token, not the browser.** The login opens in the browser, but the last step — exchanging the code for a token — is done by Codex. If your proxy settings apply only to the browser, the browser part succeeds and Codex gets an error.
* **Old proxy settings are still there.** The proxy changed its port or is off, but `HTTP_PROXY` / `HTTPS_PROXY` still point to the old address. That's what happened to users in [this GitHub thread](https://github.com/openai/codex/issues/26764).
* **The proxy intercepts localhost.** After the login, the browser returns to a local address on your computer, and that request must not go through a proxy: in [a similar thread](https://github.com/openai/codex/issues/2414), excluding localhost helped some users. A Codex developer suspected there that the remaining cases mostly involve proxies and VPNs.
* **The country is not supported.** OpenAI doesn't work in every country. The list for the API is on [Supported countries and territories](https://developers.openai.com/api/docs/supported-countries); the same page warns that access from other countries may get the account blocked.

## What to try

**1. Update Codex**

`codex update` installs the latest version (if Codex was installed by the RuAPI command, update it [as described here](https://docs.ruapi.ai/en/integrations/codex.md#troubleshooting)). `codex doctor` checks the installation, configuration and login.

**2. Remove old proxy settings**

Check the `HTTP_PROXY`, `HTTPS_PROXY` and `ALL_PROXY` variables and the `~/.codex/.env` file: delete addresses that no longer work.

**3. Keep localhost out of the proxy**

Add the variable `NO_PROXY=localhost,127.0.0.1,::1` and start the login in a new terminal window. If `NO_PROXY` is already set, don't replace it; append these addresses after a comma.

**4. Check certificates**

If your antivirus or work network inspects HTTPS, Codex may not trust the replaced certificate. Turn inspection off during the login, or check whether `SSL_CERT_FILE` points to an outdated file.

**5. Sign in from the terminal**

If the error is in the app, run `codex login` in a terminal and return to the app: it [helped](https://github.com/openai/codex/issues/26764) one user.

## Codex without a ChatGPT login

Codex CLI can be connected with an API key, and then the token exchange step doesn't exist. With RuAPI, requests go to `https://www.ruapi.ai/v1`, no VPN is needed, and you pay per token.

**1. Account and key**

Sign up at [www.ruapi.ai](https://www.ruapi.ai/?lng=en\&utm_source=docs\&utm_campaign=en-codex-token-exchange-failed) with email, Google or Yandex. Key: Console → **API Keys** → **Create API key**.

**2. Balance**

Console → **Wallet**: USDT or USDC, from 5 USDT. No USDT yet? [Buy it with rubles via SBP](https://docs.ruapi.ai/en/buy-usdt.md). Cards from foreign banks (Visa / Mastercard) are accepted too.

**3. Codex**

With one command or manually in `config.toml` — see [Codex CLI](https://docs.ruapi.ai/en/integrations/codex.md). Then start `codex`: it won't ask you to sign in any more.

How this differs from a ChatGPT subscription: [Codex in Russia](https://www.ruapi.ai/codex-v-rossii?lng=en\&utm_source=docs\&utm_campaign=en-codex-token-exchange-failed).

## FAQ

> [accordion] The browser login worked. Why does Codex show an error?
>
> Because Codex itself requests the token, and its network settings may differ from the browser's.

> [accordion] I get “unexpected status 403 Forbidden”, but I'm already signed in
>
> That is a request error, not a login error. Read the text after the colon: for example, the account may have no access to the selected workspace. If Codex is connected to RuAPI, a 403 usually means the balance is too low or the key isn't allowed to use the model — see [Error codes](https://docs.ruapi.ai/en/errors.md).

> [accordion] Will a new ChatGPT account help?
>
> If the cause is the network, a proxy or the country, a new account will get the same error.

> [accordion] Can I sign in to Codex with an OpenAI key?
>
> Yes, but that takes an OpenAI API key with a paid balance and access to OpenAI's servers from your address. If the request is rejected because of the country, you'll get [unsupported\_country\_region\_territory](https://docs.ruapi.ai/en/openai-unsupported-country-region-territory.md).

## Next

> [card] [Codex CLI setup](https://docs.ruapi.ai/en/integrations/codex.md)
>
> With one command or config.toml.

> [card] [Codex: stream disconnected](https://docs.ruapi.ai/en/codex-stream-disconnected.md)
>
> The reply breaks off midway.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.