Getting started with Translate
Yandex Translate provides its own API for integrating translation algorithms into your applications. If you want to use a UI for translation, use Yandex Translator.
In this section, you will learn how to translate text using the Translate API.
To use the examples, install cURL.
Refer to the step-by-step guides for more examples in different programming languages.
To learn how to create a service account and an API key to integrate Yandex Translate into your application, see How to create an API key for accessing Yandex Cloud AI Studio.
Getting started
-
On the Billing page, make sure you have a billing account that is either
ACTIVEorTRIAL_ACTIVE. If you do not have a billing account yet, create one. -
To authenticate on behalf of a service account, you can use an API key or an IAM token; to authenticate on behalf of a user account, you can only use an IAM token.
Get your account credentials for authentication in the Translate API:
API keyIAM token-
If you do not have a service account, create one.
-
Assign the
ai.translate.userrole for the folder to the service account. -
Get the ID of the folder your service account was created in. Make sure to provide the folder ID in the
folderIdfield in the body of each request. -
Create an API key with the
yc.ai.translate.executescope.Provide the key in the
Authorizationheader of each request in the following format:Authorization: Api-Key <API_key>
-
Get the ID of any folder for which your account has the
ai.translate.userrole or higher. Make sure to provide the folder ID in thefolderIdfield in the body of each request. -
Get an IAM token for your Yandex account, federated account, local account, or service account.
Provide the token in the
Authorizationheader of each request in the following format:Authorization: Bearer <IAM_token>
-
Translating text
The example below is for MacOS and Linux. To implement it in Windows, see how to use Bash in Microsoft Windows.
This example shows how to translate two lines containing Hello and World into Russian. The text source language will be detected automatically.
-
Create a file with the request body, e.g.,
body.json:{ "folderId": "<folder_ID>", "texts": ["Hello", "World"], "targetLanguageCode": "ru" }Where:
folderId: Folder ID you got before you started.texts: Text to translate as a list of strings.targetLanguageCode: Target language. You can get the language code together with the list of supported languages.
-
Provide the file for translation by running this command:
export API_KEY=<API_key> curl \ --request POST \ --header "Content-Type: application/json" \ --header "Authorization: Api-Key ${API_KEY}" \ --data '@<path_to_JSON_file>' \ "https://translate.api.cloud.yandex.net/translate/v2/translate"Where
API_KEYis the API key you got before you started. If you use an IAM token for authentication, change theAuthorizationheader to"Authorization: Bearer <IAM_token>".This will return the translated text lines:
{ "translations": [ { "text": "Привет", "detectedLanguageCode": "en" }, { "text": "Мир", "detectedLanguageCode": "en" } ] }