Note: base_url points to the ThisToken.AI gateway
·ThisToken.AI·
Tutorials入门教程ThisToken.AI
client = OpenAI(
api_key="sk-your-ThisToken-key",
base_url="https://api.thistoken.ai/v1"
)
response = client.chat.completions.create(
model="gpt-4o-mini", # Model names should match the model list in the console
messages=[
{"role": "system", "content": "你是一个小程序内的客服助手。"},
{"role": "user", "content": "帮我查一下订单配送进度"}
]
)
print(response.choices[0].message.content)
Run `pip install openai`, then execute the script. If you get a proper Chinese response back, the pipeline "your code → gateway → model" is working.
### Step 4: Migrate to a WeChat Cloud Function
Once the pipeline is verified, move this logic into a WeChat cloud function. The cloud function only needs two things: `base_url="https://api.thistoken.ai/v1"` and your Key (configure the Key in the cloud function's environment variables — do not hardcode it). The Mini Program side calls `wx.cloud.callFunction`, and the cloud function returns the model's result. This way:
- There are no keys anywhere in the Mini Program code;
- You don't need to buy your own server or file a domain;
- Switching models means changing just one `model` parameter.
If your business needs streaming output (typewriter effect), the cloud function side can enable SSE forwarding, with the Mini Program handling chunked rendering. That's advanced material — I recommend getting the non-streaming version working first.
## A Few Final Reminders
- **Set a budget cap on your Key.** Even during testing, it's a good idea to add a spending limit on your Key in the gateway console to avoid unexpected costs. For all pricing-related questions, refer to the official pricing page.
- **Don't mix test Keys and production Keys.** After registering, you can create multiple Keys and manage them per environment.
- **Model names should follow the console.** The gateway's supported model list gets updated — fill in the `model` parameter in your code according to the docs, not from memory.
## Closing Thoughts
The security issue of connecting Mini Programs directly to AI isn't fundamentally about "whether the encryption is strong enough," but about "whether the key has ever left your control." Once you understand this, all three ways to die are easily avoided. If you're ready to get started, go register an account, create a Key, and run the code above — you can verify the entire pipeline in ten 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