How to recognize short audio files in SpeechKit
The service can recognize speech in different ways. In the example below, the audio file is recognized using the synchronous recognition API. This API has the following limitations:
- Maximum audio duration: 30 seconds
- Maximum file size: 1 MB
To complete this example, prepare an audio file in one of the supported formats and compliant with the above limitations.
In the example, the API is used via the cURL
Authentication for API access
To work with the SpeechKit API, you need to pass authentication. The authentication method depends on the account type:
- Get an IAM token for your Yandex account or federated account.
- Get the ID of the folder for which your account has the
ai.speechkit-stt.user
,ai.speechkit-tts.user
, or higher roles. -
When accessing SpeechKit via the API, provide the received parameters in each request:
-
For API v1 and API v2:
Specify the IAM token in the
Authorization
header in the following format:Authorization: Bearer <IAM token>
Specify the folder ID in the request body in the
folderId
parameter. -
For API v3:
- Specify the IAM token in the
Authorization
header. - Specify the folder ID in the
x-folder-id
header.
Authorization: Bearer <IAM_token> x-folder-id <folder_ID>
- Specify the IAM token in the
-
SpeechKit supports two authentication methods based on service accounts:
-
With an IAM token:
-
Get an IAM token.
-
Provide the IAM token in the
Authorization
header in the following format:Authorization: Bearer <IAM_token>
-
-
With API keys.
Use API keys if requesting an IAM token automatically is not an option.
-
Provide the API key in the
Authorization
header in the following format:Authorization: Api-Key <API_key>
Do not specify the folder ID in your requests, as YandexGPT uses the folder in which the service account was created.
In the example below, authentication is performed under a Yandex account.
Execute a request
Send a speech recognition request:
export FOLDER_ID=<folder_ID>
export IAM_TOKEN=<IAM_token>
curl \
--request POST \
--header "Authorization: Bearer ${IAM_TOKEN}" \
--data-binary "@speech.ogg" \
"https://stt.api.cloud.yandex.net/speech/v1/stt:recognize?folderId=${FOLDER_ID}&lang=ru-RU"
Where:
FOLDER_ID
: Folder ID you got earlier.IAM_TOKEN
: IAM token you got earlier.lang
: Recognition language.speech.ogg
: Speech audio file you prepared.
The service will respond with the recognized text, for example:
{
"result":"I'm Yandex SpeechKit. I can turn any text into speech. Now you can, too!"
}