Speech synthesis in the REST API v3
Written by
Updated at May 20, 2024
You can use the REST API v3 in SpeechKit to synthesize speech if you do not need the benefits of the gRPC API.
The example uses the following synthesis parameters:
- Voice:
marina
- Role:
friendly
- Audio format: WAV (default)
Authentication takes place under a Yandex account or a federated account using an IAM token. Authentication requires the ID of the folder in which the user has the ai.speechkit-tts.user
role to work with SpeechKit. If you use your service account, you do not need to include the folder ID in the request. For more information about authentication in the SpeechKit API, see Authentication with the SpeechKit API.
cURL
To reproduce this example, you will need the jq
- Create a file called
request.json
with the following request parameters:
{"text": "Hi, I'm Yandex Speech+Kit. I can turn any text into speech. Now yo+u can, too!", "hints": [{"voice": "marina"}, {"role": "friendly"}]}
Where:
text
: Text to synthesizehints
: List of synthesis parameters:voice
: Voice for synthesisrole
: Role
- Get the folder ID and the IAM token for the account you will use with SpeechKit, and include them in the request headers.
export FOLDER_ID=<folder_ID>
export IAM_TOKEN=<IAM_token>
curl -H "Authorization: Bearer $IAM_TOKEN" \
-H "x-folder-id: $FOLDER_ID" \
-d @request.json https://tts.api.cloud.yandex.net:443/tts/v3/utteranceSynthesis |
jq -r '.result.audioChunk.data' |
while read chunk; do base64 -d <<< "$chunk" >> audio.wav; done
Where:
FOLDER_ID
: ID of the folder for which your account has theai.speechkit-tts.user
role or higher.IAM_TOKEN
: IAM token of your Yandex account or federated account.
You will get the synthesized speech Base64-encoded and saved to the audio.wav
file.