Skip to content

Curl 调用示例

这页主要给已经有 API Key 的用户一个最直接的调用方式:
你既可以先在页面里手动填 Key 测一下,也可以直接复制下面的 cURLPythonJavaScript 示例。

在线测试

先输入你的 API Key、模型和提示词,然后直接发送测试请求:

Base URL:https://www.open1.codes
Endpoint:https://www.open1.codes/v1/responses
在线测试:通过 /open1-api 同域转发,避免浏览器跨域限制
cURL 预览
curl https://www.open1.codes/v1/responses \
  -H "Authorization: Bearer {在这里填写_API_Key}" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-5.4",
  "input": [
    {
      "role": "user",
      "content": [
        {
          "type": "input_text",
          "text": "Reply with OK only."
        }
      ]
    }
  ]
}'

部署提醒

如果你把文档站部署在 doc.open1.codes 这类独立域名下,在线测试必须把:

text
/open1-api/*

反向代理到:

text
https://www.open1.codes/*

否则浏览器会因为跨域限制拦截请求。

可用测试模型

当前这页固定使用下面这组模型,默认是 gpt-5.4

模型名模型 ID输入价格输出价格状态
GPT-5.4gpt-5.4$2.50$15.00可用
GPT-5.3 Codexgpt-5.3-codex$1.75$14.00可用
GPT-5.3 Codex Sparkgpt-5.3-codex-spark--未匹配
GPT-5.2gpt-5.2$1.75$14.00可用
GPT-5.2 Codexgpt-5.2-codex$1.75$14.00可用
GPT-5.1 Codex Maxgpt-5.1-codex-max$1.25$10.00可用
GPT-5.1 Codexgpt-5.1-codex$1.25$10.00可用
GPT-5.1gpt-5.1$1.25$10.00可用
GPT-5.1 Codex Minigpt-5.1-codex-mini$0.2500$2.00可用
GPT-5gpt-5$1.25$10.00可用

示例代码

下面 3 种写法都只走 responses 接口。
其中 JavaScript 示例更适合在 Node.js 18+ 环境里跑。

bash
curl https://www.open1.codes/v1/responses \
  -H "Authorization: Bearer {在这里填写_API_Key}" \
  -H "Accept: application/json" \
  -H "User-Agent: curl/8.0" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "input": [
      {
        "role": "user",
        "content": [
          {
            "type": "input_text",
            "text": "Reply with OK only."
          }
        ]
      }
    ]
  }'
python
import json
import sys
import time
import urllib.error
import urllib.request

BASE_URL = "https://www.open1.codes"
API_KEY = "在这里填写你的_API_Key"
MODEL = "gpt-5.4"
PROMPT = "Reply with OK only."
TIMEOUT = 60
VERBOSE = True


def build_request(base_url, model, prompt, api_key):
    base_url = base_url.rstrip("/")
    endpoint = f"{base_url}/v1/responses"
    payload = {
        "model": model,
        "input": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "input_text",
                        "text": prompt,
                    }
                ],
            }
        ],
    }

    request = urllib.request.Request(
        endpoint,
        data=json.dumps(payload).encode("utf-8"),
        headers={
            "Authorization": f"Bearer {api_key}",
            "Accept": "application/json",
            "Content-Type": "application/json",
            "User-Agent": "curl/8.0",
        },
        method="POST",
    )
    return endpoint, payload, request


def extract_text(response_json):
    output_text = response_json.get("output_text")
    if isinstance(output_text, str) and output_text:
        return output_text

    output = response_json.get("output")
    if isinstance(output, list):
        texts = []
        for item in output:
            for content in item.get("content", []):
                if content.get("type") in {"output_text", "text"} and content.get("text"):
                    texts.append(content["text"])
        if texts:
            return "\n".join(texts)

    return ""


def main():
    endpoint, payload, request = build_request(
        base_url=BASE_URL,
        model=MODEL,
        prompt=PROMPT,
        api_key=API_KEY,
    )

    print(f"Model    : {MODEL}")
    print(f"Endpoint : {endpoint}")
    print("Payload  :")
    print(json.dumps(payload, ensure_ascii=False, indent=2))
    print("Sending request...")

    started_at = time.perf_counter()
    try:
        with urllib.request.urlopen(request, timeout=TIMEOUT) as response:
            raw_body = response.read().decode("utf-8")
            elapsed = time.perf_counter() - started_at
            response_json = json.loads(raw_body)
    except urllib.error.HTTPError as exc:
        error_body = exc.read().decode("utf-8", errors="replace")
        print(f"HTTP error: {exc.code}", file=sys.stderr)
        print(error_body, file=sys.stderr)
        return 2

    print(f"Elapsed  : {elapsed:.2f}s")
    text = extract_text(response_json)
    print("\nModel output:")
    print(text or "没有提取到输出文本")

    if VERBOSE:
        print("\nRaw response:")
        print(json.dumps(response_json, ensure_ascii=False, indent=2))

    return 0


if __name__ == "__main__":
    raise SystemExit(main())
javascript
// Node.js 18+ 示例
const BASE_URL = "https://www.open1.codes";
const API_KEY = "在这里填写你的_API_Key";
const MODEL = "gpt-5.4";

async function main() {
  const response = await fetch(`${BASE_URL}/v1/responses`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${API_KEY}`,
      Accept: "application/json",
      "Content-Type": "application/json",
      // 更建议在 Node.js 环境下使用
      "User-Agent": "curl/8.0",
    },
    body: JSON.stringify({
      model: MODEL,
      input: [
        {
          role: "user",
          content: [
            {
              type: "input_text",
              text: "Reply with OK only.",
            },
          ],
        },
      ],
    }),
  });

  const data = await response.json();
  console.log(data);
}

main().catch(console.error);

参数说明

  • BASE_URL:默认是 https://www.open1.codes
  • API_KEY:换成你自己的 Key
  • MODEL:建议先用 gpt-5.4
  • PROMPT:你要发送的内容

常见问题

1. 返回 401

通常是 API Key 不对,或者复制时多了空格。

2. 返回 403 / 权限相关错误

先检查下面几项:

  1. 当前 Key 的分组、订阅和用途有没有选错
  2. 请求里是否缺少 User-Agent
  3. 如果你是自己写脚本或服务端请求,建议显式补上:
text
User-Agent: curl/8.0

提醒

如果你不是直接用命令行 curl,而是自己写 Python / JavaScript 请求,出现 403 时优先检查是否缺少这个请求头。

3. 浏览器里直接跑 JavaScript 不通

如果你把 JavaScript 示例直接放在浏览器控制台里跑,常见问题是:

  • 跨域限制
  • 不能像服务端那样自由控制请求头

所以这份 JavaScript 示例更推荐放在 Node.js 18+ 环境里运行。

4. 返回 429

可能是并发限制、额度不足,或者当前分组不能继续使用。

5. 模型不存在

先到 渠道与模型 页面确认当前模型名。

面向 Codex、Claude Code、OpenCode 等工具的统一接入平台