Example of using the API v1 for synchronous recognition

The example shows how the API v1 helps synchronously recognize speech in the OggOpus audio file format.

The example uses the following parameters:

  • Language: Russian.
  • Other parameters are left at their defaults.

Use the cURL utility to generate and send a request to the server for recognition.

The Yandex account or federated account are authenticated using an IAM token. If using a service account, you do not need to include the folder ID in the request. Learn more about authentication in the SpeechKit API.

Request example

Send a request to convert speech to text:

POST /speech/v1/stt:recognize?topic=general&lang=ru-RU&folderId={<folder_ID>} HTTP/1.1
        Host: stt.api.cloud.yandex.net
        Authorization: Bearer <IAM_token>
        
        ... (binary content of an audio file)
        

Where:

Send a request to convert speech to text:

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?topic=general&folderId=${FOLDER_ID}"
        

Where:

Send a request to convert speech to text:

import urllib.request
        import json
        
        FOLDER_ID = "<folder_ID>" #: Folder ID.
        IAM_TOKEN = "<IAM_token>>" #: IAM token.
        
        with open("speech.ogg", "rb") as f:
            data = f.read()
        
        params = "&".join([
            "topic=general",
            "folderId=%s" % FOLDER_ID,
            "lang=ru-RU"
        ])
        
        url = urllib.request.Request("https://stt.api.cloud.yandex.net/speech/v1/stt:recognize?%s" % params, data=data)
        # Authentication with an IAM token
        url.add_header("Authorization", "Bearer %s" % IAM_TOKEN)
        
        responseData = urllib.request.urlopen(url).read().decode('UTF-8')
        decodedData = json.loads(responseData)
        
        if decodedData.get("error_code") is None:
            print(decodedData.get("result"))
        

Where:

Send a request to convert speech to text:

<?php
        
        $token = '<IAM_token>'; #: IAM token.
        $folderId = "<folder_ID>"; #: Folder ID.
        $audioFileName = "speech.ogg";
        
        $file = fopen($audioFileName, 'rb');
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://stt.api.cloud.yandex.net/speech/v1/stt:recognize?lang=ru-RU&folderId=${folderId}&format=oggopus");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        
        curl_setopt($ch, CURLOPT_INFILE, $file);
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize($audioFileName));
        $res = curl_exec($ch);
        curl_close($ch);
        $decodedResponse = json_decode($res, true);
        if (isset($decodedResponse["result"])) {
            echo $decodedResponse["result"];
        } else {
            echo "Error code: " . $decodedResponse["error_code"] . "\r\n";
            echo "Error message: " . $decodedResponse["error_message"] . "\r\n";
        }
        
        fclose($file);
        

Where:

Response example

HTTP/1.1 200 OK
        YaCloud-Billing-Units: 15
        {
          "result": "your number is 212-85-06"
        }
        

See also