npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@thkdog/codex-openai-proxy

v1.0.1

Published

Use local Codex auth.json to expose an OpenAI-compatible proxy server

Readme

English | 简体中文

codex-openai-proxy

把本机 Codex 登录态代理成 OpenAI 兼容接口。

本服务会读取本地认证文件,然后把请求转发到 https://chatgpt.com/backend-api/codex/*,对外提供 OpenAI 风格接口,方便直接接入官方 OpenAI SDK 或现有 OpenAI 生态工具。

当前支持:

  • GET /health
  • GET /v1/models
  • POST /v1/responses
  • POST /v1/chat/completions

Quick Start

1. 准备前置条件

  • Node.js 18+
  • 本机已经登录 Codex / ChatGPT,并存在认证文件
  • 默认认证文件路径:~/.codex/auth.json

可以先确认认证文件是否存在:

ls ~/.codex/auth.json

2. 通过 npx 启动

npx @thkdog/codex-openai-proxy

默认监听地址:

http://127.0.0.1:8787

如果你是在本仓库里开发,也可以继续本地启动:

npm install
npm run dev

启动成功后,终端会打印:

  • 服务地址
  • 使用中的认证文件路径
  • 健康检查地址
  • 模型列表地址
  • OpenAI SDK baseURL
  • 可直接复制的 curl 验证命令

启动参数

本项目现在只支持命令行参数,不再读取环境变量。

查看帮助:

npx @thkdog/codex-openai-proxy --help

支持参数:

  • -H, --host <host>:监听地址,默认 127.0.0.1
  • -p, --port <port>:监听端口,默认 8787
  • -a, --auth-file <path>:认证文件路径,默认 ~/.codex/auth.json

示例:

npx @thkdog/codex-openai-proxy --port 9000
npx @thkdog/codex-openai-proxy --host 0.0.0.0 --port 9000
npx @thkdog/codex-openai-proxy --auth-file ~/.codex/auth.json
npx @thkdog/codex-openai-proxy --host 0.0.0.0 --port 9000 --auth-file ~/.codex/auth.json

也可以全局安装后使用:

npm install -g @thkdog/codex-openai-proxy
codex-openai-proxy --port 9000

启动后验证

健康检查:

curl http://127.0.0.1:8787/health

查看模型列表:

curl http://127.0.0.1:8787/v1/models

根路径说明页:

curl http://127.0.0.1:8787/

curl 示例

非流式 chat/completions

curl http://127.0.0.1:8787/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-5-codex",
    "messages": [
      { "role": "user", "content": "Reply with exactly ok" }
    ]
  }'

流式 chat/completions

curl -N http://127.0.0.1:8787/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-5-codex",
    "stream": true,
    "messages": [
      { "role": "user", "content": "Reply with exactly ok" }
    ]
  }'

非流式 responses

curl http://127.0.0.1:8787/v1/responses \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-5-codex",
    "input": [
      {
        "type": "message",
        "role": "user",
        "content": [
          { "type": "input_text", "text": "Reply with exactly ok" }
        ]
      }
    ]
  }'

OpenAI SDK 示例

先安装官方 SDK:

npm install openai

chat/completions 示例:

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "dummy",
  baseURL: "http://127.0.0.1:8787/v1",
});

const result = await client.chat.completions.create({
  model: "gpt-5-codex",
  messages: [
    { role: "user", content: "Reply with exactly ok" },
  ],
});

console.log(result.choices[0]?.message?.content);

responses 示例:

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "dummy",
  baseURL: "http://127.0.0.1:8787/v1",
});

const result = await client.responses.create({
  model: "gpt-5-codex",
  input: "Reply with exactly ok",
});

console.log(result.output_text);

常见问题

认证文件不存在:

  • 默认路径不是 ~/.codex/auth.json
  • 你可以通过 --auth-file 显式指定
  • --auth-file ~/.codex/auth.json 这种写法已支持 ~ 自动展开

认证文件格式不正确:

  • 文件内容不是合法 JSON
  • 或缺少 tokens.access_token
  • 或缺少 tokens.account_id

端口不可用:

  • 传入的 --port 不是 1 到 65535 的整数
  • 或端口已被其他进程占用

Codex 登录态失效:

  • /health 正常但 /v1/models 请求失败
  • 这种情况通常需要重新登录 Codex / ChatGPT 以刷新本地认证文件