Qwen API tutorial

How to authenticate, which endpoint to call, and the mistakes that cost people an afternoon.

The Qwen API is OpenAI-compatible: the official documentation shows requests built with the standard OpenAI SDKs. In practice that means three values decide whether your first call works — the API key, the base URL and the model ID.

1. Create an account and an API key

2. Point your client at the right endpoint

https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 (legacy: https://dashscope.aliyuncs.com)
Overseas endpoints documented: Singapore https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1 (legacy dashscope-intl.aliyuncs.com), US https://dashscope-us.aliyuncs.com/compatible-mode/v1, Tokyo https://{WorkspaceId}.ap-northeast-1.maas.aliyuncs.com/compatible-mode/v1

If you are migrating existing code, keep your current SDK and change only base_url and the model name. If you see 401 responses, the key is usually wrong; if you see 404 on the model, the model ID is wrong or deprecated.

3. A minimal working request

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DASHSCOPE_API_KEY"],
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # legacy domain; workspace domains are also documented
)

resp = client.chat.completions.create(
    model="qwen-plus",
    messages=[{"role": "user", "content": "Explain vector databases in three sentences."}],
)
print(resp.choices[0].message.content)

4. Watch out for these

5. Controlling cost

6. Next steps

Facts on this page were checked against the official pages linked above on 2026-09-19. Prices and model IDs change frequently: confirm them on the vendor’s own pricing page before you rely on them.