从 .env 加载环境变量
·ThisToken.AI·
Tutorials入门教程ThisToken.AI
load_dotenv()
Key 从环境变量注入,代码里没有任何明文密钥
client = OpenAI(
api_key=os.getenv("THISTOKEN_API_KEY"),
base_url="https://api.thistoken.ai/v1",
)
resp = client.chat.completions.create(
model="gpt-4o-mini", # 模型名称以网关文档为准
messages=[{"role": "user", "content": "用一句话解释什么是API网关"}],
)
print(resp.choices[0].message.content)
Note two things:
- `base_url="https://api.thistoken.ai/v1"` —— this is the core of the whole integration. The OpenAI SDK lets you replace the request endpoint, and the gateway is protocol-compatible with the OpenAI API, so none of your other code needs to change;
- `api_key` comes from `os.getenv`. In local development it's read from `.env`; when deployed to a server, it's read from the deployment platform's environment variable configuration—your code stays identical.
Run it. If it prints an answer, the pipeline works.
### Step 4: Switching Models Is a One-Parameter Change
Want to try another model? No need to switch SDKs, re-register accounts, or apply for another Key—just change the `model` parameter:
resp = client.chat.completions.create(
model="claude-sonnet-4-20250514", # 换个模型试试,名称以网关文档为准
messages=[{"role": "user", "content": "用一句话解释什么是API网关"}],
)
This is the biggest value of a unified gateway: **model selection becomes a string parameter, not a re-integration project.** For indie developers, it means you can quickly A/B test and pick the best combo of quality and cost; for small teams, it means the cost of switching models drops from "three days of code changes" to "change one string."
## A Few Closing Tips
- **Key rotation**: if you suspect a leak, immediately revoke the old Key and issue a new one in the console—don't go hunting through your code for where it leaked first. Doing things in the wrong order costs you an extra half hour of panic;
- **Understand billing**: costs for different models on the gateway can vary a lot. Check the official pricing page before integrating so you know what to expect;
- **Don't log your Key**: some folks habitually print out entire request parameters for debugging—make sure the Key inside the client object doesn't get printed along with them.
## Final Thoughts
Going from "hardcoded plaintext Key in the code" to "environment variable injection + a unified gateway" takes fewer than ten lines of code changes, but what you gain is: no leaked keys, the freedom to switch models anytime, and bills managed in one place. If you've been burned by juggling multiple API Keys, or you want to painlessly migrate your existing OpenAI SDK code to a multi-model architecture, go register an account—you can get the code above running in five minutes: https://api.thistoken.ai/register
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key