Quickstart

Installing the Google Cloud SDK

The Cloud Translation API makes use of the gcloud command line tool, which is distributed within the Google Cloud Platform Cloud SDK. Follow the instructions on that page to install and set up the Cloud SDK.

Note: if you are using a Google Compute Engine VM, you should already have the Cloud SDK preinstalled on that image.

Set up your project

If you haven't already done so:

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Platform Console, go to the Projects page and select or create a new project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Cloud Translation API.

    Enable the API

  5. After enabling the Google Cloud Translation API, click the Go to Credentials button to set up your Cloud Translation API credentials:

  6. See Set Up a Service Account for information on how to authorize to the Cloud Translation API service from your code. Following those steps, you should obtain both a service account key file (in JSON) and a GOOGLE_APPLICATION_CREDENTIALS environment variable that will allow you to authenticate to the Translation API in this Quickstart.

Make a Translation API request

We will make a Translation API Request using the v2 REST method.

  1. Create a JSON request file with the following text, and save it as a translate-request.json plain text file:

    {
      'q': 'The quick brown fox jumped over the lazy dog.',
      'source': 'en',
      'target': 'es',
      'format': 'text'
    }
      

    This JSON snippet includes the text (query) to translate within the q field. We include the source language using the iso-169-1 code for English ("en") and the target language for Spanish ("es"). The format of the query is noted as "text" (for plain text).

  2. Authenticate to your service account, passing the location of your service account key file:

    $ gcloud auth activate-service-account --key-file=service-account-key-file
      
  3. Obtain an authorizaton token using your service account:

    $ gcloud auth print-access-token
    access_token
      
  4. Use curl to make a v2 request, passing it the access token you printed, and the filename of the JSON request you set up in step 1:

    $ curl -s -k -H 'Content-Type: application/json' \
        -H 'Authorization: Bearer access_token' \
        'https://translation.googleapis.com/language/translate/v2 \
        -d @translate-request.json
      

    Note that to pass a filename to curl you use the -d option (for "data") and precede the filename with an @ sign. This file should be in the same directory in which you execute the curl command.

    You should see a response similar to the following:

    {
      "data": {
        "translations": [
          {
            "translatedText": "El zorro rápida saltó sobre el perro perezoso."
          }
        ]
      }
    }
      

Congratulations! You've sent your first request to the Cloud Translation API!

Questions or comments

Learn more about the Translation REST API:

Send feedback about...

Google Cloud Translation API Documentation