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.

Need a command prompt? You can use the Google Cloud Shell. The Google Cloud Shell is a command line environment that already includes the Google Cloud SDK, so you don't need to install it. (The Google Cloud SDK also comes preinstalled on Google Compute Engine Virtual Machines.)

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 Manage resources page and select or create a new project.

    Go to the Manage resources 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:

    Google Cloud translation API has
    been enabled. Next, to use the API you'll need the right credentials.
  6. See the authentication guide for information on how to authenticate 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-639-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. If you have not already done so, set the Application Default Credentials (ADC) GOOGLE_APPLICATION_CREDENTIALS environment variable:

    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
      
  4. Obtain an authorizaton token using your service account:

    gcloud auth application-default print-access-token
    access_token
      
  5. 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 -X POST -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!

What's next

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Google Cloud Translation API Documentation