Getting started with Translate
In this section, you will learn how to translate text using the Translate API. To translate a text, pass it using the translate method.
Before getting started
To use the examples, install cURL and get the authorization data for your account:
- On the billing page, make sure that your billing account status is
ACTIVE
orTRIAL_ACTIVE
. If you don't have a billing account, create one. - Get an IAM token required for authentication.
- Get the ID of any folder that your account is granted the
editor
role or higher for.
-
Select the authentication method:
-
Get an IAM token used in the examples.
-
Create an API key. Pass the API key in the
Authorization
header in the following format:Authorization: Api-Key <API key>
-
-
Assign the
editor
role or a higher role to the service account for the folder where it was created.Don't specify the folder ID in your requests: the service uses the folder where the service account was created.
-
Use the CLI to get an IAM token required for authentication:
$ yc iam create-token
-
Get the ID of any folder that your account is granted the
editor
role or higher for.
Translate text from any language
This example shows how to translate the following two lines of text into Russian: Hello
and World
. The text language is recognized automatically.
-
Create a file with the request body (for example,
body.json
).
Infolder_id
, enter the folder ID. List the text strings to translate in thetexts
field.In the
targetLanguageCode
field, enter the target language in ISO 639-1 format. For Russian, it'sru
. The language code can be obtained together with a list of supported languages.{ "folder_id": "b1gvmob95yysaplct532", "texts": ["Hello", "World"], "targetLanguageCode": "ru" }
-
To upload the file for translation, use the translate method:
$ export IAM_TOKEN=CggaATEVAgA... $ curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d '@body.json' \ "https://translate.api.cloud.yandex.net/translate/v2/translate"
The response from the service will contain translated text:
{ "translations": [ { "text": "Привет", "detectedLanguageCode": "en" }, { "text": "Мир", "detectedLanguageCode": "en" } ] }