Quickstart: Using the Command Line

This quickstart introduces you to the Cloud Text-to-Speech API. In this quickstart, you set up your Google Cloud Platform project and authorization and then make a request for the Text-to-Speech API to create audio from text.

To learn more about the fundamental concepts in Cloud Text-to-Speech API, read Cloud Text-to-Speech API Basics.

Before you begin

  1. Sign in to your Google Account.

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

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the Cloud Text-to-Speech API.

    Enable the API

  5. Set up authentication:
    1. Go to the Create service account key page in the GCP Console.

      Go to the Create Service Account Key page
    2. From the Service account drop-down list, select New service account.
    3. Enter a name into the Service account name field.
    4. Don't select a value from the Role drop-down list. No role is required to access this service.
    5. Click Create. A note appears, warning that this service account has no role.
    6. Click Create without role. A JSON file that contains your key downloads to your computer.
  6. Install and initialize the Cloud SDK.
  7. Provide authentication credentials to your application code by running the following command. Replace [PATH] with the location of the JSON file that contains your credentials.

    gcloud auth activate-service-account --key-file=[PATH]

Synthesize audio from text

You can convert text to audio by making an HTTP POST request to the https://texttospeech.googleapis.com/v1beta1/text:synthesize endpoint. In the body of your POST command, specify the type of voice to synthesize in the voice configuration section, specify the text to synthesize in the text field of the input section, and specify the type of audio to create in the audioConfig section.

  1. Run the following line at the command line to synthesize audio from text using the Text-to-Speech API. The command uses the gcloud auth application-default print-access-token command to retrieve an authorization token for the request.

    The response is directed to the output file, synthesize-output.txt.

    curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
      -H "Content-Type: application/json; charset=utf-8" \
      --data "{
        'input':{
          'text':'Android is a mobile operating system developed by Google,
             based on the Linux kernel and designed primarily for
             touchscreen mobile devices such as smartphones and tablets.'
        },
        'voice':{
          'languageCode':'en-gb',
          'name':'en-GB-Standard-A',
          'ssmlGender':'FEMALE'
        },
        'audioConfig':{
          'audioEncoding':'MP3'
        }
      }" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > synthesize-text.txt
    

    The contents of the synthesize-output.txt file should be similar to the following:

    {
      "audioContent": "//NExAASCCIIAAhEAGAAEMW4kAYPnwwIKw/BBTpwTvB+IAxIfghUfW.."
    }
    
    

  2. The JSON output for the REST command contains the synthesized audio in base64-encoded format. Copy the contents of the audioContent field into a new file named synthesize-output-base64.txt. Your new file will look something like the following:

    //NExAARqoIIAAhEuWAAAGNmBGMY4EBcxvABAXBPmPIAF//yAuh9Tn5CEap3/o
    ...
    VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    

  3. Decode the contents of the synthesize-output-base64.txt file into a new file named synthesized-audio.mp3. For information on decoding base64, see Decoding Base64-Encoded Audio Content.

    base64 synthesize-output-base64.txt --decode > synthesized-audio.mp3
    

  4. Play the contents of synthesized-audio.mp3 in an audio application or on an audio device. You can also open the synthesized-audio.mp3 in the Chrome browser to play the audio by navigating to the folder that contains the file, for example file://my_file_path/synthesize-audio.mp3

Clean up

To avoid unnecessary Google Cloud Platform charges, use the GCP Console to delete your project if you do not need it.

What's next

  • Learn more about Cloud Text-to-Speech by reading the basics.
  • Review the list of available voices you can use for synthetic speech.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Text-to-Speech API