Sign in anywhere
This is the free lesson
Next: A first task in a safe workspace
The rest of this course is part of Pro, together with every other course and the ones added later.
$25 a month billed yearly, or $29 month to month
A free account keeps your progress across devices. The paid lessons need Pro.
Code Snippets
codex --version
codex login status
codex login --help | grep -E -A1 "device-auth|with-api-key"
codex login --device-auth
codex login status
printenv OPENAI_API_KEY | codex login --with-api-key
codex login status
stat -f '%Sp %N' ~/.codex/auth.json
codex logout
codex login status
grep -A7 "No device code" CHEATSHEET.md
codex login statusSign in anywhere. In this video you'll see the three ways to sign in to Codex: with a browser, with a device code on a machine that has no browser, and with an API key. You'll also check that the device-code page is the real one. Codex works with a stored login. On a machine with a browser, codex login opens a ChatGPT sign-in page, and the answer comes back on a local port. A remote server has no browser. There Codex prints a link and a one-time code, and you approve on another device. The third way is an API key, billed per use. All three end as a login stored in the Codex home. In this lesson you will read the login status, start a device-code login and check the page before you type the code, and store an API key. Before you start, you need Codex installed: codex, dash dash version, prints a version number. This is the Codex command-line agent, and its version. It keeps its state in one folder, the Codex home: by default a folder named dot codex in your home directory. The variable CODEX_HOME moves it. codex login status answers one question: is this machine signed in, and how? Here it says: not logged in. The login command has three forms. With no option, it opens a browser. With device auth, it prints a code for another device. And with the API key option, it reads a key from standard input. Plain codex login needs the browser and Codex on the same machine: the answer of the sign-in page comes back to a small server here, on local port one four five five. A remote server has no browser, so this form prints a link and a one-time code instead. The code expires in fifteen minutes. You open the link on another device, where you are signed in to ChatGPT. Before you type the code anywhere, three checks. One: you started this login yourself, a moment ago, in this terminal. Two: the address in the browser is exactly auth dot openai dot com, slash codex, slash device, on a secure connection. Three: nobody sent you this code or this link. Codex prints the same warning: if a website or another person gave you this code, cancel. When you enter the code on the other device and approve, this command ends with the words: successfully logged in. In this recording I cancel instead, with Control C, so this code is never used. A cancelled login stores nothing: still not logged in. If the page refuses your code, the cause is usually somewhere else: device code login must be switched on in the security settings of your ChatGPT account, or by the administrator of your workspace. The third way is an API key from the OpenAI platform. The key sits in an environment variable and is piped into the login command, so it never appears on the command line. Usage with a key is billed at API rates, apart from any ChatGPT plan. Read the answer: the key is saved, and authentication is checked when you send a request. The key in this recording is a placeholder, not a real key, and Codex stored it all the same. So a wrong key shows at the first request, not here. The status names the method, and shows only the start and the end of the key. Whatever the method, the result is a stored login: a file in the Codex home, unless your configuration uses the keychain of the operating system. This prints its permissions, not its content. Never print it, and never commit it. codex logout removes the stored login, and the status is back to: not logged in. When device code login is not available, the documentation gives two fallbacks. Forward the local port over SSH, and run the browser login through that tunnel. Or sign in on a machine with a browser and copy the file auth dot json to the server. Treat that file like a password: it holds access tokens. Off camera, I used the second fallback: a login that was made in a browser is now the file auth dot json in this Codex home. The status says: logged in using ChatGPT. From now on, usage counts against the plan of that account. So: a browser when you have one, a device code when you do not, a key where no person is there to sign in. And the status command tells you which one is active. In this lesson you learned to read the login status, to enter a device code only when you started the login yourself and only on the OpenAI sign-in page, and to use an API key where no person is there to sign in.