Skip to main content
Get Started

Make your first API call to Qwen

Alibaba Cloud Model Studio supports API calls to models through OpenAI-compatible interfaces and the DashScope SDK.

To call the Qwen API:
  • Get an API key
  • Set up your local development environment
  • Call the Qwen API

Account setup

  1. Create an account: If you do not have an Alibaba Cloud account, create one.
    If you encounter issues, see Register an Alibaba Cloud account.
  2. Activate Model Studio: Use your Alibaba Cloud account to go to Alibaba Cloud Model Studio. Read and accept the Terms of Service to activate the service. If no Terms of Service dialog appears, the service is already activated.
  3. Get an API key: Go to the API Key page and click Create API key. Then use the API key to call models. You do not need to select a model when you create an API key. Specify the model to call through the model parameter in the request body, for example, model="qwen-plus". For supported models, see Model list. To limit the models that an API key can call, select the Custom permission when you create the key and turn on the Model access scope switch. The key can then call only the models that you selected.
  4. Get your workspace ID: When calling models in the China (Beijing), Singapore, Japan (Tokyo), Germany (Frankfurt), or China (Hong Kong) region, you need to include the workspace ID (WorkspaceId) in the Base URL. You can find it on the Workspace Management page. For models in the US (Virginia) region, workspace-specific domains are not supported. Do not include the workspace ID (WorkspaceId) in the Base URL — use {WorkspaceId}.us-east-1.maas.aliyuncs.com as the endpoint domain directly.

Set your API key as an environment variable

Store your API key in an environment variable to avoid hardcoding credentials and reduce security risks.
  • Linux
  • macOS
  • Windows

Permanent

To make the API key available in all new sessions for the current user, set it as a permanent environment variable.
  1. Run the following command to append the environment variable setting to the ~/.bashrc file.
# Replace YOUR_DASHSCOPE_API_KEY with your Model Studio API key
echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bashrc
Alternatively, you can manually edit the ~/.bashrc file.Manual modificationRun the following command to open the ~/.bashrc file.nano ~/.bashrc Add the following content to the configuration file.
# Replace YOUR_DASHSCOPE_API_KEY with your Model Studio API key
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
In the nano editor, press Ctrl+X, then Y, and then Enter to save and close the file. 2. Run the following command to apply the changes.source ~/.bashrc 3. Open a new terminal window and run the following command to verify that the environment variable is set.echo $DASHSCOPE_API_KEY

Temporary

To use the environment variable only for the current session, set it as a temporary environment variable.
  1. Run the following command.
# Replace YOUR_DASHSCOPE_API_KEY with your Model Studio API key
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  1. Run the following command to verify that the environment variable is set. echo $DASHSCOPE_API_KEY

Choose a development language

Select a language or tool to call model APIs.
  • Python
  • Node.js
  • Java
  • curl
  • Other languages

Step 1: Set up Python

Python 3.8 or later is required. For installation instructions, see Install Python.Run this command to check whether Python and pip are installed:
python -V
pip --version
For example, on Windows Command Prompt:
C:\Users\Administrator>python -V
Python 3.13.2

C:\Users\Administrator>pip --version
pip 24.3.1 from C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\pip (python 3.13)

FAQ

Q: The commands python -V and pip --version return errors:
  • 'python' is not recognized as an internal or external command, operable program or batch file.
  • 'pip' is not recognized as an internal or external command, operable program or batch file.
  • -bash: python: command not found
  • -bash: pip: command not found
Try these solutions:
  • Windows
  • Linux and macOS
  1. Confirm that you installed Python by following Install Python, and added python.exe to the PATH environment variable. When installing Python 3.13.2, select Add python.exe to PATH at the bottom of the installer to add Python to the system environment variables, then click Install Now to complete the installation.
  2. If Python and PATH are correctly configured but the error persists, close your current terminal and open a new one.
If Python is already installed, you can create a virtual environment to install the OpenAI Python SDK or the DashScope Python SDK and avoid dependency conflicts.
  1. Create a virtual environment Create a virtual environment named .venv:
# If this fails, replace python with python3
python -m venv .venv
  1. Activate the virtual environment On Windows, activate the virtual environment:
.venv\Scripts\activate
On macOS or Linux, run:
source .venv/bin/activate
You can call models on Model Studio using the DashScope Python SDK (recommended) or the OpenAI Python SDK.
  • Install the DashScope Python SDK
  • Install the OpenAI Python SDK
Install or upgrade the DashScope Python SDK:
# If this fails, replace pip with pip3
pip install -U dashscope
Look for Successfully installed ... dashscope-x.x.x to confirm installation.

Step 2: Call the API

  • DashScope Python SDK
  • OpenAI Python SDK
With Python and the DashScope Python SDK installed, send your first API request.
  1. Create a file named hello_qwen.py.
  2. Copy this code into hello_qwen.py and save it.
import os
from dashscope import MultiModalConversation
import dashscope

# The following URL is for the Singapore region. When calling, replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
messages = [
    {'role': 'system', 'content': [{'text': 'You are a helpful assistant.'}]},
    {'role': 'user', 'content': [{'text': 'Who are you?'}]}
]
response = MultiModalConversation.call(
    # Singapore, US (Virginia), and China (Beijing) API keys are not interchangeable. Get your API key: https://www.alibabacloud.com/help/model-studio/get-api-key
    # If the environment variable is not configured, replace with: api_key = "sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="qwen3.8-max",
    messages=messages
)

if response.status_code == 200:
    print(response.output.choices[0].message.content[0]["text"])
else:
    print(f"HTTP status code: {response.status_code}")
    print(f"Error code: {response.code}")
    print(f"Error message: {response.message}")
    print("See: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
  1. Run python hello_qwen.py or python3 hello_qwen.py from the command line.
    The command in this example must be executed from the directory containing the Python file. To run it from elsewhere, specify the full path.
    The output is:
I am a large-scale language model from Alibaba Cloud. My name is Qwen.

API reference

FAQ

How do I fix theModel.AccessDeniederror after calling the model API?

A: This error occurs because you are using an API key from a sub-workspace. A sub-workspace cannot access applications or models in the default workspace. To use a sub-workspace API key, the root account administrator must grant model authorization for the corresponding sub-workspace (for example, this topic uses the qwen3.8-max model). For detailed steps, see Configure model calling permissions.

Next steps

Explore more models

The example code uses qwen3.8-max. Model Studio also supports other Qwen models. See the Model list for supported models and their API references.

Learn advanced features

The example code covers basic Q&A only. To learn more about the Qwen API, such as streaming output, structured output, and function calling, see the Text generation model overview.

Try models in the browser

If you want to interact with models through a dialog box, like on Qwen Chat, go to the Playground .

Call a custom-trained model

If you have deployed a custom-trained model on Model Studio, use the model code (not the model ID) from the model deployment page as the model parameter when making API calls. For details, see Post-deployment calls.

Token Plan
Model Playground
  • Music generation
Statistics and Monitoring
Support