Google Cloud APIs Guide

Google has many services, and many APIs to access these services. Third-party apps can use these APIs to take advantage of, or extend the functionality of the existing services.

To see all available Cloud APIs, see Google Cloud APIs in the Google Cloud console API Library:

https://console.cloud.google.com/apis/library

This guide covers the following topics:

  1. How to get a Google APIs key?

  2. An overview of using Google Cloud APIs in API Tester:

  • Cloud Translation API,

  • Cloud Vision API,

  • Cloud Speech-to-Text API,

  • Cloud Text-to-Speech API,

  • Places API.

How to get a Google APIs key?

Usage of all Google APIs requires authentication and authorization using the Oauth 2.0 protocol. To start, it is necessary to obtain credentials from the Developers Console.

In the process, you will need to create a billing account or use credentials of already existing one, which will be used for payment, in case you exceed your limits. So be prepared to have your billing data with you.

Following steps, described below, you will get your API key:

  1. Go to the Google Cloud Platform, tap “Get Started for free”:

    https://cloud.google.com/apis/docs/overview

  2. You’ll need to enable billing. This is a required step, you won’t be able to start using Google APIs key without it. Fill in the required personal information.

  3. Create a new project or choose an existing one. All your API keys will be stored on a single page. To reach them, you need to go to "APIs and Services", and afterwards to "Credentials" in the sidebar.

4. Go to "Create Credentials", and then – "API key". Your API key will be created, and you’ll be able to see it in a pop-up window. Copy the generated token and keep it secret.

5. Enable required APIs. Go to "APIs and Services", and afterwards to "Library" in the sidebar.

Find the API you want to test in the search, go to the API page and tap "ENABLE".

Now you can use the generated key to test the APIs added to your application. You can add multiple APIs to one application and use only one key to access them.

Cloud Translation API

Cloud Translation API enables your websites and applications to dynamically translate text for more than 100 language pairs. This API can be used for:

  • translating text to a given target language (e.g. English to Spanish);

  • detecting languages of a source text and translating it to a given target language (e.g. Italian + English to German).

We'll start with translating text to a given target language. To do that with API Tester app:

  1. Find the "Cloud Translation API" in the library and tap "ENABLE":

    https://console.cloud.google.com/apis/library/translate.googleapis.com
  2. Launch API Tester and create a new request by tapping on the “Plus” icon.

  3. Select the POST request method.

  4. In the screen that opens, place the following endpoint URL in the address field:

    https://translation.googleapis.com/language/translate/v2
  5. Add the "key" parameter and set the API key generated in Google Cloud Platform as the value in the Query Params section. This parameter will be automatically added to the query string.

  6. Select Raw in the Body section. Select the "application/json" value for the Content Type field.

  7. Enter the following code in the Post data field:

    { "q": "The Great Pyramid of Giza (also known as the Pyramid of Khufu or the Pyramid of Cheops) is the oldest and largest of the three pyramids in the Giza pyramid complex.", "source": "en", "target": "es", }


    Here "q" is the text we want to translate, "source" is the language we want to translate from, "target" is the language we want to translate the text into.

Tap the send request button. On the response screen we will get the text in Spanish:

You can import this request into API Tester app via these links:

Now, we'll take a look at detecting languages of a source text and translating it.

Let's pass an array of strings in different languages to be translated into German. Enter the following code in the Post data field:

{ 
  "q": [" Ciao mondo! ", "My name is Matthew"], 
  "target": "de" 
}

On the response we will get both phrases translated into German:

You can import this request into API Tester app via these links:

Cloud Vision API

Find the Cloud Vision API in the library and tap "ENABLE":

https://console.cloud.google.com/apis/library/vision.googleapis.com

The Vision API can detect and extract text from images. There are two annotation features that support optical character recognition (OCR):

  1. TEXT_DETECTION - detects and extracts text from any image. For example, a photo might contain a street sign or traffic sign. The JSON includes the entire extracted string, as well as individual words, and their bounding boxes.

  2. DOCUMENT_TEXT_DETECTION - also extracts text from an image, but the response is optimized for dense text and documents. The JSON includes page, block, paragraph, word, and break information.

Let's try to send a request with the first type. To do this, we will need to change the endponit and the request body, all other fields remain as in requests from the previous part of the guide:

  1. New endpoint:

    https://vision.googleapis.com/v1/images:annotate
  2. Request JSON body:

    { 
      "requests": [ 
        { 
          "image": { 
            "source": { 
              "imageUri": "https://os-system.com/blog/wp-content/uploads/2021/09/Front-End-and-Back-End.png" 
            } 
           }, 
           "features": [ 
             { 
               "type": "TEXT_DETECTION" 
             } 
           ] 
        } 
      ] 
    }

For the "imageUri" field, you can insert the address of any image from the Internet. Once you have everything set up, hit the "Send" button and you will receive the following response:

You can import this request into API Tester app via these links:

Cloud Speech-to-Text API

Find the Cloud Speech-to-Text API in the library and tap "ENABLE":

https://console.cloud.google.com/apis/library/speech.googleapis.com

Cloud Speech-to-Text API can be used to transcribe an audio file to text. Let's use the synchronous recognition method for a short audio file. Follow these steps to send a recognize REST request to the Speech-to-Text API:

  1. Add new endpoint:

    https://speech.googleapis.com/v1/speech:recognize
  2. Add a Request JSON body:

    {
      "config": {
          "encoding":"FLAC",
          "sampleRateHertz": 16000,
          "languageCode": "en-US",
          "enableWordTimeOffsets": false
      },
      "audio": {
          "uri":"gs://cloud-samples-tests/speech/brooklyn.flac"
      }
    } 

All Speech-to-Text API synchronous recognition requests must include a speech recognition "config" field. A RecognitionConfig contains the following sub-fields:

  1. "encoding" - (required) specifies the encoding scheme of the supplied audio (of type AudioEncoding). If you have a choice in codec, prefer a lossless encoding such as FLAC or LINEAR16 for best performance. The encoding field is optional for FLAC and WAV files where the encoding is included in the file header.

  2. "sampleRateHertz" - (required) specifies the sample rate (in Hertz) of the supplied audio. The sampleRateHertz field is optional for FLAC and WAV files where the sample rate is included in the file header.

  3. "languageCode" - (required) contains the language + region/locale to use for speech recognition of the supplied audio. The language code must be a BCP-47 identifier. Note that language codes typically consist of primary language tags and secondary region subtags to indicate dialects (for example, 'en' for English and 'US' for the United States in the example above).

In the "uri" field, enter the address of the audio file in the public domain.

You should see a response similar to the following:

You can import this request into API Tester app via these links:

Cloud Text-to-Speech API

Synthesizes natural-sounding speech by applying powerful neural network models. Text-to-Speech allows developers to create natural-sounding, synthetic human speech as playable audio. You can use the audio data files you create using Text-to-Speech to power your applications or augment media like videos or audio recordings.

Find the Cloud Text-to-Speech API in the library and tap "ENABLE":

https://console.cloud.google.com/apis/api/texttospeech.googleapis.com

For making this request in API Tester use:

  1. New endpoint:

    https://texttospeech.googleapis.com/v1/text:synthesiz
  2. Request JSON body:

    {  
      "input":{  
        "text":"I've added the event to your calendar." 
      },  
      "voice":{  
        "languageCode":"en-gb",  
        "name":"en-GB-Standard-A",  
        "ssmlGender":"FEMALE" 
      },  
      "audioConfig":{  
        "audioEncoding":"MP3" 
      }  
    }

Text-to-Speech has a wide selection of custom voices available for you to use. The voices differ by language, gender, and accent (for some languages). For example, you can create an audio that mimics the sound of a female English speaker with a British accent.

Let's look at the response from the server.

The speech synthesis process generates raw audio data as a base64-encoded string. You will need to decode the base64-encoded string into an audio file before you can play it.

You can import this request into API Tester app via these links:

Places API

The Places API is a service that returns information about places using HTTP requests. The following place requests are available:

  • "Place Search" - returns a list of places based on a user's location or search string.

  • "Place Details" - returns more detailed information about a specific place, including user reviews.

  • "Place Photos" - provides access to the of place-related photos.

  • "Place Autocomplete" - automatically fills in the name and/or address of a place as users type.

  • "Query Autocomplete" - provides a query prediction service for text-based geographic searches, returning suggested queries as users type.

In this guide we will review a simple Find Place request.

A Find Place request takes a text input and returns a place. The input can be any kind of Place's text data, such as a name, address, or phone number. The request must be a string.

Required parameters for making a request:

  • "input" The text string on which to search, for example: "restaurant" or "123 Main Street". This must be a place name, address, or category of establishments. Any other types of input can generate errors and are not guaranteed to return valid results. The Places API will return candidate matches based on this string and order the results based on their perceived relevance.

  • "inputtype" The type of input. This can be one of either textquery or phonenumber.

Optional parameter:

  • "fields" Use the "fields" parameter to specify a comma-separated list of place data types to return. For example: fields=formatted_address,name,geometry.

Let's look at the example of getting the address of the Australian Museum of Сontemporary Art.

  1. Find the Places API in the library and tap "ENABLE":

    https://console.cloud.google.com/apis/library/places-backend.googleapis.com 
  2. Launch API Tester and create a new request by tapping on the “plus” icon.

  3. Select the GET request type.

  4. In the screen that opens, place the following endpoint URL in the address field:

    https://maps.googleapis.com/maps/api/place/findplacefromtext/json
  5. Add the "key" parameter and set the API key generated in Google Cloud Platform as the value in the Query Params section. This parameter will be automatically added to the query string.

  6. Add the following parameters in the Query Params section:

  • fields=formatted_address,

  • input= MuseumOfContemporaryArtAustralia,

  • inputtype=textquery.

The server will return the address of the Museum:

You can import this request into API Tester app via these links:

Last updated