# AI API key for Qwen Code in 2026: where to issue it and what usage costs

> What is an AI API key for Qwen Code, how it differs from a subscription, where to issue one, how to configure settings.json, and what determines token usage.

Source: https://ailoot.ai/blog/en/qwen-code-api-key/
Canonical: https://ailoot.ai/blog/en/qwen-code-api-key/
Language: en
Publisher: Ailoot (https://ailoot.ai/)
Translation: guide-141
Revision: sha256-a1a9304400bac735f5297d0e995422c716b730c582a4044bac2bd4554bc21621


Someone installs Qwen Code, launches it in the terminal, and on the very first request it asks for a key. Then the search begins: people type “buy api key for qwen code” and land on pages where they are sold either a subscription or someone else’s ready-made access. Here’s what the key is, where to get one, and how much it costs, step by step.

The main point up front: nobody sells an API key as a separate boxed product. It’s an access string that you issue yourself in the model provider’s account, and issuing it is free. You need money later, when topping up your balance; the provider’s terms apply at that step. Below: where to get a key, how to connect it to Qwen Code, and what using it costs.

<div class="aside" data-semantic="callout" role="note" aria-label="Attention">

Attention

This material is informational and describes public rules. It does not name intermediary services that offer to “handle” payments while bypassing restrictions, or explain ways to bypass restrictions. Prices are taken from provider pages as of the beginning of September 2026; they change, so check the provider’s site for current prices.

</div>

<div class="aside" data-semantic="editorial-reference" role="note" aria-label="Ailoot documentation">

[Ailoot documentation](https://ailoot.ai/docs/) — reference materials on using the service.

</div>

## What is an AI API key for Qwen Code?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

An API key for Qwen Code is a personal password-like string that the service provider uses to identify your requests to the model and calculate their usage. Qwen Code itself is a free, open-source agent; it doesn’t have its own model built in. It gets responses from an external provider and presents your key there. So “getting a key” means registering with a provider and issuing an access string in its account. Supply the secret through the launching environment and reference its variable name in Qwen Code’s settings. A successful model request, rather than merely saving the settings, confirms access and may incur usage charges.

</div>

Qwen Code is an open-source AI agent that runs in the terminal and writes code for your tasks. It’s free, installs through npm, and has no neural network of its own inside. The model does all the intelligent work, and the model runs on the provider’s servers.

Qwen Code needs a pass to reach the model. That pass is the API key—a long string like a password that the provider issues to you personally. When the agent sends a request, it attaches the key; the provider uses it to identify whose request it is, responds, and deducts usage from your balance.

This leads to a simple point that clears up half the confusion. A key isn’t bought as a marketplace product: you create an account with a provider, click “create key,” and get the string for free. The search “buy api key” really means “where do I create an account and how do I pay for usage afterward,” and both questions are covered below.

## How is an API key different from a subscription and the old free login?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

Qwen Code supports several authentication and billing paths. The old free Qwen OAuth tier was discontinued on April 15, 2026. Alibaba Coding Plan uses a subscription and a dedicated endpoint; a standard Model Studio API key uses pay-as-you-go billing. Current clients also offer other platform and plan choices. This guide covers the standard Model Studio API key path: keep its region, endpoint, model, and billing plan together.

</div>

Inside Qwen Code, `/auth` shows the authentication choices supported by your installed version. Choose the entry for the platform that issued your key and its billing plan. The distinctions below explain the older free login, Coding Plan, and the standard API key used in this guide; they are not an exhaustive list of today’s menu entries.

The first option is free login through a Qwen account (Qwen OAuth). It used to provide access to the model without any key, but it was discontinued.

<figure>
<blockquote>
<p>The free Qwen OAuth tier was discontinued as of April 15, 2026. Previously saved tokens may still work for a while, but new requests will be rejected.</p>
</blockquote>
<figcaption>— Qwen Code Docs, Authentication, official documentation</figcaption>
</figure>

The second option is Alibaba ModelStudio’s Coding Plan subscription. It’s a fixed monthly fee that gives you a weekly request quota and a separate connection address.

The third option is the API key from a model provider. There’s no fixed fee: you put money in your balance and spend it as you work, based on a token meter. This option is flexible and suits you if you use the agent irregularly. The rest of this article focuses on it.

## Where can you issue an API key for Qwen Code?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

Qwen Code can connect to several providers and protocols. For a direct Alibaba Model Studio connection, select the region in your Model Studio console before creating a key. The key, API base URL, workspace, and available model must match that region. An international console address or an English interface does not by itself identify the API region.

</div>

Because Qwen Code understands different protocols, you aren’t limited to just one key provider. The official documentation explicitly lists the services it can connect to.

<figure>
<blockquote>
<p>Supports OpenAI, Anthropic, Gemini, and Qwen protocols. Any third-party provider or local model. Switch on the fly.</p>
</blockquote>
<figcaption>— Qwen Code Docs, Authentication, official documentation</figcaption>
</figure>

Below is the direct connection path.

For the direct path, open the [Alibaba Model Studio console](https://modelstudio.console.alibabacloud.com/), select the intended region, and create a key in its API key management page. Keep the region and workspace name with the configuration; do not copy the key into shared notes.

| Region selected for the key | Standard OpenAI-compatible base URL | What to verify |
|----|----|----|
| China (Beijing) | `DASHSCOPE_BASE_URL` | Beijing key and a model available in that region |
| Singapore | `DASHSCOPE_BASE_URL` | Singapore key and a model available in that region |
| US (Virginia), including Global or US deployment scope | Copy the workspace API Host from the selected US workspace and use its documented OpenAI-compatible base URL | US workspace key, deployment scope, and model list; do not reuse a Beijing or Singapore address |

The [regional guide](https://www.alibabacloud.com/help/en/model-studio/regions) recommends workspace-dedicated domains for production. Copy the real API Host from Workspace Management and follow the [base URL guide](https://www.alibabacloud.com/help/en/model-studio/base-url); do not invent a workspace ID. Existing shared DashScope endpoints and workspace-dedicated endpoints are different access choices. Coding Plan and Token Plan also use their own endpoints and keys. Copy the full OpenAI-compatible Base URL from your selected regional workspace console into `DASHSCOPE_BASE_URL`. The example omits `baseUrl` in JSON and supplies it as `OPENAI_BASE_URL` before launch; use real console values, never a literal workspace placeholder.

There’s also a free option that doesn’t require any key: run a Qwen model on your computer through Ollama and connect Qwen Code to it as your own server. It requires a powerful machine and separate setup. How to run the agent locally is shown in the walkthrough on [launching Qwen Code in the terminal](https://ailoot.ai/blog/en/qwen-code-terminal-auto-edit/).



## How do you add the key to Qwen Code so it works?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

In `~/.qwen/settings.json`, configure the protocol, regional base URL, model ID, and `envKey`, the name of the environment variable that supplies the key. Keep the secret value out of this JSON. Start Qwen Code from the terminal that holds the variable, check the selected connection, and send one short prompt before treating the setup as usable. `/model` lists configured choices; it does not by itself prove provider access.

</div>

Back up existing settings and merge the connection below into `~/.qwen/settings.json`; do not overwrite unrelated providers or preferences. This illustrative configuration follows the [Qwen Code authentication example](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/auth/) with the workspace URL supplied through `OPENAI_BASE_URL`, the API key read from the environment variable named by `envKey`, and the plaintext `env` block omitted. `qwen3-coder-plus` is the model in that official example; first confirm that your selected region/account offers it, or use an actual model ID from that region’s console in both places.

Copy the full OpenAI-compatible Base URL from your selected regional workspace console into `DASHSCOPE_BASE_URL`. The example omits `baseUrl` in JSON and supplies it as `OPENAI_BASE_URL` before launch; use real console values, never a literal workspace placeholder.

```json
{
  "modelProviders": {
    "openai": [
      {
        "id": "qwen3-coder-plus",
        "name": "qwen3-coder-plus",
        "envKey": "DASHSCOPE_API_KEY"
      }
    ]
  },
  "security": { "auth": { "selectedType": "openai" } },
  "model": { "name": "qwen3-coder-plus" }
}
```

`openai` selects the compatible protocol. `envKey` names the variable Qwen Code reads; it is not the key value. `model.name` must match the configured `id`. The [model provider reference](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/model-providers/) documents these fields.

On macOS or Linux, open a Bash terminal and enter the key at a hidden prompt, then launch Qwen Code in that same terminal:

```bash
read -r -p 'Workspace OpenAI-compatible Base URL (copy from regional console): ' DASHSCOPE_BASE_URL
if [[ -z "$DASHSCOPE_BASE_URL" || ! "$DASHSCOPE_BASE_URL" =~ ^https:[/][/] || "$DASHSCOPE_BASE_URL" == *'{'* || "$DASHSCOPE_BASE_URL" == *'}'* ]]; then
  printf '%s\n' 'STOP: supply the real regional workspace Base URL.' >&2
else
  read -r -s -p 'DashScope API key: ' DASHSCOPE_API_KEY
  printf '\n'
  if [[ -n "$DASHSCOPE_API_KEY" ]]; then
    export DASHSCOPE_API_KEY
    export OPENAI_BASE_URL="$DASHSCOPE_BASE_URL"
    qwen
  else
    printf '%s\n' 'STOP: API key is empty.' >&2
  fi
fi
```

On Windows, use PowerShell:

```powershell
$env:DASHSCOPE_BASE_URL = Read-Host 'Workspace OpenAI-compatible Base URL (copy from regional console)'
if ([string]::IsNullOrWhiteSpace($env:DASHSCOPE_BASE_URL) -or $env:DASHSCOPE_BASE_URL -notmatch '^https:[/][/]' -or $env:DASHSCOPE_BASE_URL -match '[{}]') {
  throw 'Supply the real regional workspace Base URL.'
}
$qwenKey = Read-Host 'DashScope API key' -AsSecureString
$env:DASHSCOPE_API_KEY = [System.Net.NetworkCredential]::new('', $qwenKey).Password
if ([string]::IsNullOrWhiteSpace($env:DASHSCOPE_API_KEY)) {
  throw 'API key is empty.'
}
$env:OPENAI_BASE_URL = $env:DASHSCOPE_BASE_URL
qwen
```

These commands keep the literal secret out of JSON and shell command history; the running process still receives it in its environment. Do not print it, commit it, or paste it into a prompt. Remove an old secret-bearing `env` entry from settings when adopting this route. A new terminal needs the variable supplied again.

In Qwen Code, use `/doctor` to inspect authentication and `/model` to check the configured choice. In a harmless empty test folder, send: “Reply with the word connected. Do not read or change files or run commands.” A successful reply tests one model request and can incur usage charges. A configured model appearing in `/model` only confirms the local configuration.

If the request fails, record the redacted error and request ID when supplied. Check authentication separately from model availability and billing: verify the key’s issuing platform, region/workspace, standard-versus-plan endpoint, selected model ID, and account balance/quota in the same provider console. A 401 can reflect a key/endpoint or billing-plan mismatch; it is not proof that the provider is broken. Stop after the failed check and correct the documented mismatch before trying again. This article describes a documentation-based example, not a paid request performed for this guide.

## How much does work with an API key cost, and what makes the bill grow?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

With an API key, you pay by the meter: a price for every thousand input and output tokens, without a fixed monthly amount.  Lighter models, such as Qwen3-Coder-Flash, cost several times less. The bill grows with the amount of work: the longer your files and the more the agent reads and writes, the higher the usage. Always check the provider’s page for the exact price; it changes.

</div>

A key differs from a subscription in that there’s no fixed monthly amount. You pay for what the model reads and writes, measured in tokens. A token is a piece of text roughly a few letters long. Each model has its own price per million input tokens and per million output tokens, with output almost always costing more than input.


A million tokens sounds like a lot, but when you’re working on a large project, it goes faster than you’d expect: the agent reads your files in full on every request.

Here’s the practical takeaway. The bill depends on the amount of text the agent processes. Long files, long sessions, and constantly rereading code consume your balance fastest; the number of tasks is secondary. A separate article explains step by step [how API token usage is calculated](https://ailoot.ai/blog/en/api-token-usage-calculation/) and why it can drain away unnoticed.

## Why are payments declined?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

To start spending from the key, you need to top up your balance. If the payment system rejects your card because of its issuing bank, changing your email, language, or profile settings doesn’t change that card’s terms.

</div>

This is where a common mistake happens.

The obstacle comes one step later. You’ve issued the key, but without money in the balance, it won’t spend anything—the provider returns an insufficient funds error.

If the decline is related to the bank that issued the card, changing your email, interface language, or profile settings doesn’t change the payment terms. This mechanism is explained using [a foreign card for Claude](https://ailoot.ai/blog/en/claude-card-payment-requirements/) as an example.

And bear in mind that the payment gateway makes the final decision. It may decline the payment for its own reasons, and there are no guarantees that it will go through.

## Where should you start?

<div class="aside" data-semantic="key-takeaway" role="note" aria-label="Key takeaway">

Key takeaway

Start by identifying the platform, region and billing plan that issued your key. The old free OAuth path is discontinued; this guide’s recipe covers a standard Model Studio pay-as-you-go key, while plan-specific access has its own endpoint and requirements. Issue the key in the account—it’s free—then supply its secret value in the launching environment and keep only the connection and variable name in the Qwen Code settings file. You need a card accepted by the payment gateway to top up your balance. Choose a lighter model if you want to keep usage down.

</div>

A short step-by-step plan:

1.  Identify the issuing platform and billing plan in your installed client’s authentication options. The old free OAuth path was discontinued in April 2026. This checklist covers a standard Model Studio pay-as-you-go key; other plans and platforms have their own documented routes.
2.  Create an account with a provider. The simplest option is Alibaba directly.
3.  Issue a key in the provider account, in the access keys section. Issuing it is free; you don’t need a card for that.
4.  Merge the regional connection into `~/.qwen/settings.json` without a secret value. Supply `DASHSCOPE_API_KEY` in the launching terminal, then check the selected model and one short reply as shown above.
5.  To pay for usage, you need a card issued in your name that the provider’s payment gateway accepts. The payment gateway sets the top-up terms; issuing a key and paying for usage are separate steps.

It’s also worth considering which model you need. For demanding tasks, choose the higher-end Qwen3-Coder-Plus; to save money, choose the lighter Flash, which is several times cheaper for the same volume. The walkthrough on [Qwen Code as a replacement for Claude Code](https://ailoot.ai/blog/en/qwen-code-token-plan-setup/) shows how Qwen Code differs from other terminal agents.

<div class="section" role="doc-bibliography" aria-label="Sources">

### Sources

- [Qwen Code - GitHub repository](https://github.com/QwenLM/qwen-code) - open-source agent code, installation, multi-protocol support, and third-party provider connections
- [Qwen Code - authentication documentation](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/auth/) - platform and billing-plan choices, discontinuation of free OAuth on 15.04.2026, and settings with environment-based credentials
- [Alibaba Cloud ModelStudio - console](https://bailian.console.alibabacloud.com) - international account where you issue a key for Qwen models


</div>





More materials are available on [Ailoot](https://ailoot.ai/).

## Qwen Code AI API key questions

### Is issuing an API key the same as buying model usage?

No. Issuing the access string is separate from funding usage. The provider identifies and meters requests through the key; an issued key alone does not establish that the account has a usable balance or quota.

### What should match in the Qwen Code provider configuration?

The selected protocol, provider base URL, environment variable holding the key, and model identifier must refer to the same connection. In the example, model.name matches the modelProviders entry’s id; keep the actual key out of shared settings copies.


## Ailoot · The company and its project portfolio

Ailoot combines established operating experience, a large client base, stable service and ongoing KPI control.

The company and its project portfolio: millions of customers; monthly sales in the billions.

Ailoot brings that operating experience to API access for AI features in customer products, agents, applications and automations.
