Getting started with Translate
Yandex Translate provides an API for integrating translation technologies 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 Setting up access with API keys.
Getting started
-
On the Billing
page, make sure you have a billing account that is eitherACTIVE
orTRIAL_ACTIVE
. If you do not have a billing account yet, create one. -
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 keyIAM token-
If you do not have a service account, create one.
-
Assign the
ai.translate.user
role for the folder to the service account. -
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. -
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>
-
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 thefolderId
field in the body of each request. -
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>
-
Translating text
The example below is intended to be run in MacOS and Linux. To run it in Windows, see how to work with Bash in Microsoft Windows.
This example shows how to translate two strings containing Hello and World into Russian. The text source language is recognized 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 a list of supported languages.
-
Submit the file for translation by running the 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_KEY
is the API key you got before you started. If you use an IAM token for authentication, change theAuthorization
header to"Authorization: Bearer <IAM_token>"
.The service will return the translated strings:
{ "translations": [ { "text": "Привет", "detectedLanguageCode": "en" }, { "text": "Мир", "detectedLanguageCode": "en" } ] }