Skip to main content

OpenAI-compatible API proxy for chat.qwen.ai with Flask server and Python API

Project description

Qwen Web API Proxy

Qwen API Proxy exposes the upstream chat.qwen.ai service through an OpenAI-compatible API. It provides a local Flask server and a Python API for issuing chat completions programmatically.

Features:

  • OpenAI-compatible v1/chat/completions endpoint with streaming support
  • Python API QwenApi for synchronous completions

Installation

Install dependencies:

pip install flask flask-cors requests

The service reads configuration from environment variables:

Variable Description Default
QWEN_AUTH_TOKEN Authorization token copied from chat.qwen.ai required
QWEN_BASE_URL Upstream API base URL https://chat.qwen.ai
PORT Port for the Flask server 5000
QWEN_DEBUG Set to 1 to enable debug logging 0

For authentication, you can either set the QWEN_AUTH_TOKEN environment variable or create a token.txt file in the current working directory with the token. To get the token, follow the steps:

  1. login to chat.qwen.ai;
  2. press F12 to open the browser's developer tools;
  3. navigate to the Console tab;
  4. input localStorage.getItem("token") and press Enter;
  5. copy the token (without quotes) and paste it into the token.txt file or set the QWEN_AUTH_TOKEN environment variable.

Usage

Run the server

python -m qwen.server

The app binds to 0.0.0.0:<PORT> and exposes the following endpoints:

  • GET /health – health check
  • GET /v1/models – list models
  • POST /v1/chat/completions – OpenAI-compatible chat completions
  • DELETE /v1/chats/<chat_id> – delete a stored chat session upstream

Python API

from qwen import QwenApi

messages = [{"role": "user", "content": "Hello, Qwen!"}]
client = QwenApi("qwen-flash")
response = client(
    messages,
    enable_thinking=True,
    thinking_budget=2048,
)
print(response)

QwenApi raises ConfigurationError when the token is missing and QwenAPIError for upstream request issues.

Chat parameters

  • messages (list): List of messages in the format of [{"role": "user", "content": "Hello, Qwen!"}]. Or a single string containing the user query.
  • model (str): Model name. The name mapping is defined in qwen/config.py, including:
    DEFAULT_MODEL_MAP: dict[str, str] = {
        "qwen": "qwen3-max",
        "qwen-think": "qwen3-235b-a22b",  # 2507
        "qwen-coder": "qwen3-coder-plus",
        "qwen-flash": "qwen-plus-2025-09-11",  # next-80b-a3b
        "qwen-vl": "qwen3-vl-plus",  # Qwen3-VL-235B-A22B
    }
    
  • enable_thinking (bool): Set True to request autodocumented reasoning. Defaults to the upstream user preference when omitted. Not available on all models.
  • thinking_budget (int): Optional token budget for reasoning content. Only used when enable_thinking=True.
  • stream (bool): Force streaming behaviour. QwenApi always disables streaming, but the REST endpoint honours the flag.

Notes

  1. rate limit unknown - not recommended to use on main account
  2. currently only supports single, pure-text message

License

GPLv3 License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qwen_webapi-0.1.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qwen_webapi-0.1.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file qwen_webapi-0.1.1.tar.gz.

File metadata

  • Download URL: qwen_webapi-0.1.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for qwen_webapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cc3abf986464270408e49dcdcea559b49d24748439ef237b2183b9d29bda33af
MD5 b7902a5ff181f696c047422bb2fc3153
BLAKE2b-256 548d07f3f8dbf27d085c44cd12069f136fb7047510eb8a09f13c837b55b390d1

See more details on using hashes here.

File details

Details for the file qwen_webapi-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: qwen_webapi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for qwen_webapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae7bcad73e59e7f8ae2b6f1acc795f64f9590ed0232daa46802434346057f075
MD5 5c98af4869e2360f9029749de253fe07
BLAKE2b-256 49d7d379cf41a59486325617b044e5feb05858c8476a047cbceeed82da706121

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page