Getting Started with Amazon Polly using Node.js

After building a smart pumpkin that speaks, I was hoping for a better Text-to-Speech engine API to be released. Luckily, at AWS Re:Invent 2016, Amazon did just that.

Amazon Polly is a low-cost, easy-to-use Text-To-Speech API with impressive sounding voices. You get 5 million characters per month, for the first 12 months free. You can transcribe ~1600 average emails a month for free.

We can quickly get started with Polly using Node.js.

Download Sample Project

$ git clone https://github.com/awslabs/aws-nodejs-sample.git

Configure AWS Keys

[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

The credential file is what the aws-sdk uses by default to connect to your AWS account. Overall, be careful with these keys. They could do damage in the wrong hands.

Text-to-Speech to an MP3 file

Now we can create a new file called amazon-polly-file.js:

cd aws-nodejs-sample && touch amazon-polly-file.js

Here is the code for amazon-polly-file.js:

You can run it with:

node amazon-polly-file.js

In this example, we are taking the text ‘Hi, my name is @anapfox’, sending it to Polly, and writing the contents to the file.

As you can see, we are setting the VoiceID to Kimberly. You can check out all the valid voices here.

Text-to-Speech to Speaker

npm i --save speaker

Now we can create a new file called amazon-polly-speaker.js:

touch amazon-polly-speaker.js

Here is the code for amazon-polly-speaker.js:

You can run it with:

node amazon-polly-speaker.js

In this speaker example, we are doing the same thing but creating a stream from the audio we get back from Polly. Then, sending that stream to the speaker module.

All Done 🤗

If I missed anything, feel free to reach out to me on Twitter.

I talk about technology I love. Developer Advocate @meroxadata, 🤘🛠

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store