Prerequisites
Quickstart
This example uses the Singapore region. Replace {WorkspaceId} in the code with your workspace ID. The example saves the audio as output.mp3 in the current directory.
# coding=utf-8
import os
import dashscope
# If the environment variable is not configured, set your API key directly: api_key = "sk-xxx"
# The following is the configuration for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
api_key=os.environ.get('DASHSCOPE_API_KEY'),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
# Model
model = "qwen-audio-3.0-tts-flash"
# Voice
voice = "longanhuan_v3.6"
# Send the text to synthesize and get the binary audio
audio = client.audio.tts.synthesize("今天天气怎么样?", model=model, voice=voice)
# Save the audio to a local file
with open('output.mp3', 'wb') as f:
f.write(audio)