Pass text to the Text-to-Speech API

Demonstrates how to pass text to the Text-to-Speech API to synthesize audio.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


// SynthesizeText synthesizes plain text and saves the output to outputFile.
func SynthesizeText(w io.Writer, text, outputFile string) error {
	ctx := context.Background()

	client, err := texttospeech.NewClient(ctx)
	if err != nil {
		return err

	req := texttospeechpb.SynthesizeSpeechRequest{
		Input: &texttospeechpb.SynthesisInput{
			InputSource: &texttospeechpb.SynthesisInput_Text{Text: text},
		// Note: the voice can also be specified by name.
		// Names of voices can be retrieved with client.ListVoices().
		Voice: &texttospeechpb.VoiceSelectionParams{
			LanguageCode: "en-US",
			SsmlGender:   texttospeechpb.SsmlVoiceGender_FEMALE,
		AudioConfig: &texttospeechpb.AudioConfig{
			AudioEncoding: texttospeechpb.AudioEncoding_MP3,

	resp, err := client.SynthesizeSpeech(ctx, &req)
	if err != nil {
		return err

	err = ioutil.WriteFile(outputFile, resp.AudioContent, 0644)
	if err != nil {
		return err
	fmt.Fprintf(w, "Audio content written to file: %v\n", outputFile)
	return nil


 * Demonstrates using the Text to Speech client to synthesize text or ssml.
 * @param text the raw text to be synthesized. (e.g., "Hello there!")
 * @throws Exception on TextToSpeechClient Errors.
public static void synthesizeText(String text) throws Exception {
  // Instantiates a client
  try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
    // Set the text input to be synthesized
    SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();

    // Build the voice request
    VoiceSelectionParams voice =
            .setLanguageCode("en-US") // languageCode = "en_us"
            .setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE

    // Select the type of audio file you want returned
    AudioConfig audioConfig =
            .setAudioEncoding(AudioEncoding.MP3) // MP3 audio.

    // Perform the text-to-speech request
    SynthesizeSpeechResponse response =
        textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

    // Get the audio contents from the response
    ByteString audioContents = response.getAudioContent();

    // Write the response to the output file.
    try (OutputStream out = new FileOutputStream("output.mp3")) {
      System.out.println("Audio content written to file \"output.mp3\"");


const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');

const client = new textToSpeech.TextToSpeechClient();

 * TODO(developer): Uncomment the following lines before running the sample.
// const text = 'Text to synthesize, eg. hello';
// const outputFile = 'Local path to save audio file to, e.g. output.mp3';

const request = {
  input: {text: text},
  voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
  audioConfig: {audioEncoding: 'MP3'},
const [response] = await client.synthesizeSpeech(request);
const writeFile = util.promisify(fs.writeFile);
await writeFile(outputFile, response.audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);


use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

/** Uncomment and populate these variables in your code */
// $text = 'Text to synthesize';

// create client object
$client = new TextToSpeechClient();

$input_text = (new SynthesisInput())

// note: the voice can also be specified by name
// names of voices can be retrieved with $client->listVoices()
$voice = (new VoiceSelectionParams())

$audioConfig = (new AudioConfig())

$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
$audioContent = $response->getAudioContent();

file_put_contents('output.mp3', $audioContent);
print('Audio content written to "output.mp3"' . PHP_EOL);



def synthesize_text(text):
    """Synthesizes speech from the input string of text."""
    from import texttospeech

    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.VoiceSelectionParams(

    audio_config = texttospeech.AudioConfig(

    response = client.synthesize_speech(
        request={"input": input_text, "voice": voice, "audio_config": audio_config}

    # The response's audio_content is binary.
    with open("output.mp3", "wb") as out:
        print('Audio content written to file "output.mp3"')


require "google/cloud/text_to_speech"

client = Google::Cloud::TextToSpeech.text_to_speech

input_text = { text: text }

# NOTE: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices
voice = {
  language_code: "en-US",
  ssml_gender:   "FEMALE"

audio_config = { audio_encoding: "MP3" }

response = client.synthesize_speech(
  input:        input_text,
  voice:        voice,
  audio_config: audio_config

# The response's audio_content is binary. output_file, "wb" do |file|
  # Write the response to the output file.
  file.write response.audio_content

puts "Audio content written to file '#{output_file}'"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser