Codex OAuth Python library with optional local OpenRouter-compatible server
Project description
Claw Codex Python Library + Local Server
Local tools for using Codex OAuth credentials (ChatGPT subscription) in two ways:
- As a reusable library for direct
chat.completions-style calls. - As a local FastAPI server with OpenRouter-like endpoints and a demo UI.
This project is for local testing/investigation. Make sure usage complies with provider terms.
Install
pip install claw-codex
For local development:
python -m venv .venv
source .venv/bin/activate
pip install -e '.[dev]'
Library Quickstart
from claw_codex import ClawCodexClient
client = ClawCodexClient()
# One-time auth flow
start = client.start_auth()
print(start.authorize_url)
client.exchange_code("PASTE_REDIRECT_URL_OR_CODE")
# OpenRouter-style chat call
resp = client.chat_completions(
model="claw/codex",
messages=[
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Say hello from Codex."},
],
)
print(resp["choices"][0]["message"]["content"])
Dynamic Redirect URI Integration
For downstream applications that need to redirect users back to their own callback URL after OAuth authentication, this library supports encoding the actual redirect URI in the OAuth state parameter.
How It Works
- Library's registered redirect: The OAuth authorization URL always uses the library's registered redirect URI (
http://localhost:1455/auth/callback) - State encoding: When you provide a custom
redirect_uri, it's base64-encoded into the OAuth state parameter - Callback handling: When OpenAI redirects back to the library's callback, the library decodes the state to extract your actual redirect URI
- Token exchange: The library uses your actual redirect URI when exchanging the code for tokens
This approach works because OpenAI only validates the redirect_uri parameter during token exchange, not during the callback.
Usage for Downstream Apps
from claw_codex import ClawCodexClient
client = ClawCodexClient()
# Start auth with your custom redirect URI
# This will redirect back to your app after OAuth
auth = client.start_auth(
redirect_uri="http://localhost:8001/api/settings/codex/callback"
)
# Redirect your user to auth.authorize_url
# After authentication, OpenAI will redirect to:
# http://localhost:1455/auth/callback?code=...&state=...
#
# The library's callback will decode the state and use your redirect URI
# for the token exchange
# Exchange the code (the library automatically uses the correct redirect_uri)
creds = client.exchange_code("PASTE_REDIRECT_URL_OR_CODE")
Server API with Custom Redirect
# Start auth with custom redirect
curl -X POST http://localhost:1455/auth/codex/start \
-H "Content-Type: application/json" \
-d '{"redirect_uri": "http://localhost:8001/api/settings/codex/callback"}'
# Response:
# {
# "authorize_url": "https://auth.openai.com/...",
# "redirect_uri": "http://localhost:8001/api/settings/codex/callback",
# "state": "..."
# }
Testing the Integration
- Start the server:
claw-codex serve - Open the demo:
http://localhost:1455/demo - Enter your custom redirect URI in the "Custom Redirect URI" field
- Click "Start OAuth" and complete the flow
- The library will automatically handle the state encoding/decoding
Important Notes
- The custom redirect URI is encoded in the state parameter using base64
- The library's callback URL (
http://localhost:1455/auth/callback) must be registered with OpenAI - Your custom redirect URI doesn't need to be registered with OpenAI
- This approach enables seamless integration without requiring multiple OAuth app registrations
CLI Authentication and Chat
- Start server (existing behavior):
claw-codex
- Interactive CLI auth:
claw-codex auth login --open-browser
- Check auth state:
claw-codex auth status
- Exchange pasted redirect URL/code:
claw-codex auth exchange 'http://localhost:1455/auth/callback?code=...&state=...'
- Send a prompt via library client:
claw-codex chat "Write a short haiku" --text-only
Credentials are stored in ~/.claw-codex/auth.json by default.
UI and Local Proxy
Run:
claw-codex serve
Then open:
- Demo UI:
http://<host-or-ip>:1455/demo - Start auth via API:
POST /auth/codex/start - Chat endpoint:
POST /v1/chat/completions
If you bind to all interfaces (CLAW_CODEX_HOST=0.0.0.0), open the demo with your machine IP:
http://<your-machine-ip>:1455/demo.
Test Mode (no real OAuth)
CLAW_CODEX_MOCK=1 pytest
Testing
pytest
Publishing
Release/publish steps are maintained in AGENTS.md (Contributor Guide).
First-time registry setup details are in docs/PUBLISHING.md.
Library API details and streaming examples are in docs/LIBRARY.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claw_codex-0.2.2.tar.gz.
File metadata
- Download URL: claw_codex-0.2.2.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56acd68e17f9e2a80402023ba990c9209443581977a30e3d5e9b2f01f208a425
|
|
| MD5 |
4d259cea7843b226a01243d4a473e7ff
|
|
| BLAKE2b-256 |
6fdc7f34eab4c2507496fb6020d3e95838bd18c44581db4a8da8acb663bed82c
|
Provenance
The following attestation bundles were made for claw_codex-0.2.2.tar.gz:
Publisher:
publish.yml on JayFarei/claw-codex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
claw_codex-0.2.2.tar.gz -
Subject digest:
56acd68e17f9e2a80402023ba990c9209443581977a30e3d5e9b2f01f208a425 - Sigstore transparency entry: 901204300
- Sigstore integration time:
-
Permalink:
JayFarei/claw-codex@a9b5a09a30c414526c5c59c411468ea94aef4d32 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/JayFarei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a9b5a09a30c414526c5c59c411468ea94aef4d32 -
Trigger Event:
release
-
Statement type:
File details
Details for the file claw_codex-0.2.2-py3-none-any.whl.
File metadata
- Download URL: claw_codex-0.2.2-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f67b8f800c55bf9f4984738e1025deaf3351a8a2ce95d87cc81d506e53f76dc9
|
|
| MD5 |
36aa21d045081551e98ba61be71b1ad7
|
|
| BLAKE2b-256 |
b6c767f9ef1b2283fb268f5710345f9258d8fff1f049212ab7068b4016795631
|
Provenance
The following attestation bundles were made for claw_codex-0.2.2-py3-none-any.whl:
Publisher:
publish.yml on JayFarei/claw-codex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
claw_codex-0.2.2-py3-none-any.whl -
Subject digest:
f67b8f800c55bf9f4984738e1025deaf3351a8a2ce95d87cc81d506e53f76dc9 - Sigstore transparency entry: 901204357
- Sigstore integration time:
-
Permalink:
JayFarei/claw-codex@a9b5a09a30c414526c5c59c411468ea94aef4d32 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/JayFarei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a9b5a09a30c414526c5c59c411468ea94aef4d32 -
Trigger Event:
release
-
Statement type: