Alibaba Cloud Model Studio supports API calls to models through OpenAI-compatible interfaces and the DashScope SDK.
- Get an API key
- Set up your local development environment
- Call the Qwen API
Account setup
-
Create an account: If you do not have an Alibaba Cloud account, create one.
If you encounter issues, see Register an Alibaba Cloud account.
- 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.
-
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
modelparameter 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. -
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.comas 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.
Steps
Steps
- 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.- Run the following command to append the environment variable setting to the
~/.bashrcfile.
~/.bashrc file.Manual modificationRun the following command to open the ~/.bashrc file.nano ~/.bashrc Add the following content to the configuration file.source ~/.bashrc
3. Open a new terminal window and run the following command to verify that the environment variable is set.echo $DASHSCOPE_API_KEYTemporary
To use the environment variable only for the current session, set it as a temporary environment variable.- Run the following command.
-
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
Check your Python version
Check your Python version
FAQ
Q: The commandspython -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
- Windows
- Linux and macOS
- 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.
- If Python and PATH are correctly configured but the error persists, close your current terminal and open a new one.
Set up a virtual environment (optional)
Set up a virtual environment (optional)
- Create a virtual environment Create a virtual environment named .venv:
- Activate the virtual environment On Windows, activate the virtual environment:
Install the DashScope Python SDK or OpenAI Python SDK
Install the DashScope Python SDK or OpenAI Python SDK
- Install the DashScope Python SDK
- Install the OpenAI Python SDK
Successfully installed ... dashscope-x.x.x to confirm installation.Step 2: Call the API
- DashScope Python SDK
- OpenAI Python SDK
- Create a file named
hello_qwen.py. - Copy this code into
hello_qwen.pyand save it.
-
Run
python hello_qwen.pyorpython3 hello_qwen.pyfrom the command line.The output is:The command in this example must be executed from the directory containing the Python file. To run it from elsewhere, specify the full path.
API reference
- For the input and output parameters, see Qwen API reference.
- For other models, see Model list.
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 |
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 |