Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Translate
  • Getting started
    • All guides
    • How to translate text
    • How to improve the accuracy of translations
    • Detecting language
    • Getting a list of supported languages
    • Setting up access with API keys
  • Access management
  • Pricing policy

In this article:

  • Getting started
  • Detect the language of a phrase
  • Specify the most likely languages
  1. Step-by-step guides
  2. Detecting language

Detecting the text language using Translate

Written by
Yandex Cloud
Updated at April 21, 2025
  • Getting started
  • Detect the language of a phrase
  • Specify the most likely languages

To detect the language of a text, use the detectLanguage method.

Note

The detectLanguage method returns the language code of the source text. If the language cannot be detected, the language code field in the response will be empty.

Getting startedGetting started

To use the examples, install cURL.

The examples below are intended to be run in MacOS and Linux. To run them in Windows, see how to work with Bash in Microsoft Windows.

To authenticate under a service account, you can use an API key or an IAM token; to authenticate under a user account, you can only use an IAM token.

Get your account details for authentication in the Translate API:

API key
IAM token
  1. If you do not have a service account, create one.

  2. Assign the ai.translate.user role for the folder to the service account.

  3. Get the ID of the folder your service account was created in. Make sure to include the folder ID in the folderId field in the body of each request.

  4. Create an API key with the yc.ai.translate.execute scope.

    Provide the key in the Authorization header of each request in the following format:

    Authorization: Api-Key <API_key>
    
  1. Get the ID of any folder for which your account has the ai.translate.user role or higher. Make sure to include the folder ID in the folderId field in the body of each request.

  2. Get an IAM token for your Yandex account, federated account or service account.

    Provide the token in the Authorization header of each request in the following format:

    Authorization: Bearer <IAM_token>
    

Detect the language of a phraseDetect the language of a phrase

In this example, we will detect the language that the phrase Hello, world is written in.

To detect the language of the text, pass it in the detectLanguage request body:

cURL
export FOLDER_ID=<folder_ID>
export API_KEY=<API_key>
export TEXT="Hello, world"
curl \
  --request POST \
  --header "Content-Type: application/json" \
  --header "Authorization: Api-Key ${API_KEY}" \
  --data "{\"folderId\": \"${FOLDER_ID}\", \"text\": \"${TEXT}\"}" \
  "https://translate.api.cloud.yandex.net/translate/v2/detect"

Where:

  • FOLDER_ID: Folder ID you got before you started.
  • Where API_KEY is the API key you got before you started. If you use an IAM token for authentication, change the Authorization header to "Authorization: Bearer <IAM_token>".

The service will respond with the language code of the source text:

{
    "languageCode": "en"
}

Specify the most likely languagesSpecify the most likely languages

Some words are spelled the same in different languages. For example, the English word hand is also written as hand in German, Swedish, and Dutch. If the text you provide contains such words, Translate may detect the wrong source language.

To avoid mistakes, you can use the languageCodeHints field to specify which languages should be prioritized when determining the text language:

cURL
{
    "folderId": "<folder_ID>",
    "languageCodeHints":["ru", "de"],
    "text": "hand"
}

Where:

  • folderId: Folder ID you got before you started.
  • languageCodeHints: Languages to prioritize when determining the language of the text.
  • text: Text to translate as a string.

Save the request body to a file (e.g., body.json) and provide the file using the detectLanguage method:

export API_KEY=<API_key>
curl \
  --request POST \
  --header "Content-Type: application/json" \
  --header "Authorization: Api-Key ${API_KEY}" \
  --data '@body.json' \
  "https://translate.api.cloud.yandex.net/translate/v2/detect"

{
    "languageCode": "de"
}

Where API_KEY is the API key you got before you started. If you use an IAM token for authentication, change the Authorization header to "Authorization: Bearer <IAM_token>".

Was the article helpful?

Previous
How to improve the accuracy of translations
Next
Getting a list of supported languages
© 2025 Direct Cursus Technology L.L.C.