Stay organized with collections Save and categorize content based on your preferences.

Create audio from text by using the command line

This document walks you through the process of making a request to Text-to-Speech using the command line. To learn more about the fundamental concepts in Text-to-Speech, read Text-to-Speech Basics.

Before you begin

Before you can send a request to the Text-to-Speech API, you must have completed the following actions. See the before you begin page for details.

  • Enable Text-to-Speech on a GCP project.
    1. Make sure billing is enabled for Text-to-Speech.
    2. Create and/or assign one or more service accounts to Text-to-Speech.
    3. Download a service account credential key.
  • Set your authentication environment variable.

Synthesize audio from text

You can convert text to audio by making an HTTP POST request to the 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. Execute the REST request below at the command line to synthesize audio from text using Text-to-Speech. The command uses the gcloud auth application-default print-access-token command to retrieve an authorization token for the request.

    HTTP method and URL:


    Request JSON body:

        "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."

    To send your request, expand one of these options:

    You should receive a JSON response 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:

  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.


    1. Copy only the base-64 encoded content into a text file.

    2. Decode the source text file using the base64 command line tool by using the -d flag:


    Mac OSX

    1. Copy only the base-64 encoded content into a text file.

    2. Decode the source text file using the base64 command line tool:



    1. Copy only the base-64 encoded content into a text file.

    2. Decode the source text file using the certutil command.

  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/synthesized-audio.mp3

Clean up

To avoid unnecessary Google Cloud Platform charges, use the Google Cloud 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.