DEV Community

mycel
mycel

Posted on

Calling the Qwen API for the First Time: From Key to Working Code

Getting a large model API to actually respond can feel like a rite of passage. This is the zero-to-first-request walkthrough I wish I'd had: no framework, no SDK — just curl and an OpenAI-style endpoint.

We'll use Qwen (Tongyi Qianwen) as the model, accessed through a compatible API gateway. The same pattern works for almost any OpenAI-compatible provider.

Step 1: Get an API Key

If you're going direct to Alibaba Cloud, you'd create a DashScope key. But many gateway platforms let you skip that: for example, on Mycel's developer platform you register an app and get a key immediately:

  1. Sign up at the platform's developer console
  2. Create an application
  3. Grab the API Key from the app detail page

⚠️ Treat your key like a password. Don't commit it, don't paste it in screenshots.

Step 2: Understand the Shape of the Request

OpenAI-compatible APIs all look the same:

  • POST to a /chat/completions endpoint
  • Your key in a header (X-API-Key or Authorization: Bearer, most gateways accept both)
  • JSON body with a model name and a messages array

Step 3: The First Request

curl -X POST \
  'https://api.example-gateway.com/api/v1/open/chat/completions' \
  -H 'X-API-Key: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "qwen-plus",
    "messages": [
      {"role": "user", "content": "Say hello in one sentence."}
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

If everything is right, you'll get back JSON with a choices[0].message.content field containing the model's reply.

Step 4: Read the Response Properly

The three fields worth knowing:

{
  "choices": [
    {
      "message": { "role": "assistant", "content": "Hello! 👋 ..." }
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 12
  }
}
Enter fullscreen mode Exit fullscreen mode
  • choices[0].message.content — your answer
  • usage — token counts; on per-call-priced platforms you may not need this, but on token-billed ones it's your cost driver
  • error — when things fail, read this first

Step 5: Python, Minimal

No SDK needed:

import requests

resp = requests.post(
    "https://api.example-gateway.com/api/v1/open/chat/completions",
    headers={"X-API-Key": "YOUR_API_KEY"},
    json={
        "model": "qwen-plus",
        "messages": [{"role": "user", "content": "Write a haiku about APIs."}],
    },
    timeout=60,
)
print(resp.json()["choices"][0]["message"]["content"])
Enter fullscreen mode Exit fullscreen mode

Common First-Timer Errors

Symptom Likely cause
401 Key wrong, or wrong header name
404 Wrong endpoint path — check /v1/ placement
400 model not found Model name typo (qwen-plus vs qwen_max)
Empty content You hit a safety filter or the model needs a longer prompt

Wrapping Up

That's the whole journey: key → header → POST → parse. From here you can add streaming (SSE), function calling, and system prompts — but the core request never changes shape.


The examples here follow the pattern Mycel's developer API uses (OpenAI-compatible, key-per-app). If your platform is OpenAI-compatible too, the code above works with a URL and key swap.

Top comments (0)