申请和测试 OpenAI API Key 的完整指南
February 12th, 2025

申请流程

准备工作

在申请 OpenAI API Key 之前,您需要准备以下三件套:

  1. VPN:用于访问 Google 和 OpenAI 网站。

  2. Google Gmail 邮箱账号:用于登录 OpenAI。

  3. 国外手机号:用于接收验证码,可以使用接码平台替代。

申请步骤

  1. 开启 VPN 并设置为香港以外的国家,以避免地区限制提示如“OpenAI’s services are not available in your country.”。

  2. 访问 OpenAI API 页面,点击“Log in”按钮,选择“Continue with Google”使用 Google 邮箱账号登录。

  3. 填写姓名和生日(First name, Last name, and Birthday),点击“Continue”进入手机验证页面。

  4. 输入能接收短信验证码的国外手机号(可以使用 SMS-Activate 接码平台),点击“Send code”发送验证码。

  5. 填写收到的验证码,完成注册。注册成功后,系统会询问您使用 OpenAI 的主要用途,选择任一选项即可。

创建 API Key

注册成功后,回到 OpenAI API 页面,点击“+ Create new secret key”按钮创建 API Key。重要提示:API Key 仅在创建时完整显示,请立即复制或截图保存,因为之后无法再查看或修改。

👉 野卡 | 一分钟注册,轻松订阅海外线上服务

测试 API

使用 curl 命令

  1. 设置环境变量 OPENAI_API_KEY,例如: bash export OPENAI_API_KEY="Your OpenAI API Key"

  2. 使用以下 curl 命令测试 API: bash curl --http1.1 -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}]}' https://api.openai.com/v1/chat/completions

使用 Python 代码

  1. 安装 openai 库: bash pip install openai

  2. 设置环境变量 OPENAI_API_KEY,例如: bash $Env:OPENAI_API_KEY = "Your OpenAI API Key"

  3. 使用以下 Python 代码测试 API: python import openai openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Say this is a test!"}] )

参考资料

  1. OpenAI API 官方文档:OpenAI API Reference

  2. Python 接入 ChatGPT API 教程:A Simple Guide to The (New) ChatGPT API with Python

Subscribe to zhende
Receive the latest updates directly to your inbox.
Nft graphic
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from zhende

Skeleton

Skeleton

Skeleton