建议用环境变量存放 Key,不要硬编码
client = OpenAI(
api_key=os.getenv("THISTOKEN_API_KEY", "把你的Key粘贴到这里"),
base_url="https://api.thistoken.ai/v1"
)
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "你是一个简洁友好的技术助手。"},
{"role": "user", "content": "用一句话解释什么是 API 网关。"}
],
temperature=0.7
)
print(response.choices[0].message.content)
Run it:
python first_call.py
If everything works, the terminal will print the model's answer after a few seconds. **At this moment, your application is connected to GPT-4.**
For JavaScript users, here's the equivalent:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.THISTOKEN_API_KEY,
baseURL: "https://api.thistoken.ai/v1",
});
const res = await client.chat.completions.create({
model: "gpt-4",
messages: [{ role: "user", content: "用一句话解释什么是API网关。" }],
});
console.log(res.choices[0].message.content);
## Why "OpenAI Format Compatibility" Is the Key to Efficiency
This deserves its own section. Because the API is fully compatible, any future migration or comparative testing only requires changing two parameters—`base_url` and `api_key`—without touching a single line of business code.
In other words, integration cost is compressed to the absolute minimum:
- **Learning cost: zero.** If you know how to use the OpenAI SDK, you already know how to use this.
- **Migration cost: two lines of configuration.** Want to switch models or channels? Just change a parameter.
- **Validation cost: one small deposit.** You're not committing much before verifying it works, and after that, you pay as you go.
For indie developers and small teams with limited manpower, "not having to learn a new stack" is often worth more than "saving X dollars per million tokens"—the half-day you save is enough to push your product prototype forward significantly.
## Three Common Pitfalls
Although the process is fast, these spots can trip you up for more than 30 seconds:
1. **Forgetting to export the environment variable.** Run `export THISTOKEN_API_KEY=sk-xxx` in your terminal (use `set` on Windows), or paste the key directly into the code just to get it running, then refactor later.
2. **Network proxy interference.** If you have a local proxy running and encounter timeouts, try turning it off first to determine whether it's a network issue or a key issue.
3. **Typo in the model name.** The model identifier must match what's in the console documentation exactly—don't slip up on capitalization or hyphens.
## What to Do After the First 5 Minutes
Getting your first piece of code running is just the starting point. Next, you can:
- Wrap the call in a function and hook it into your product logic;
- Add streaming output (`stream=True`) so users see content as it's generated;
- Design your prompts and context management carefully to control token consumption per request;
- Set up usage alerts for your key to avoid surprise bills.
These fall under "second-step optimization" and aren't part of this post's 5-minute budget—but precisely because the first step only took 5 minutes, you have plenty of time left for the things that truly affect product quality.
## Final Thoughts
The rules of indie development have always been: whoever validates fastest, survives longest. In the past, integrating a large model API took half a day of fiddling, and many ideas cooled off during the wait. Now this workflow has been compressed to less time than it takes your coffee to go cold. For the first time, the barrier to validating an AI feature has dropped to the level of "just do it while you're at it."
If you happen to have a project that could use conversational capabilities, why not spend 5 minutes trying it right now? Start here: https://api.thistoken.ai/register
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.Ready to try Token.AI?
Create a project-level API Key, enable channels in the console, and configure routing, budgets, and audit logs.
注册 ThisToken.AI 并获取 API Key