Last verified: 2026-06-08
Prerequisites
Before you begin, make sure you have the following:- Node.js installed.
- A Twilio account. You will need your Account SID and Auth Token.
- A Cartesia API key.
- A phone number that you want to call.
- A Twilio phone number to call from.
- An ngrok authtoken (a free account works).
Get Started
1
Set Up Your Project
- Create a new directory for your project and navigate to it in your terminal.
- Initialize a new Node.js project:
- Install the required dependencies:
2
Configure Environment Variables
Create a Replace the placeholder values with your actual credentials.
.env file in your project root and add the following:3
Create the Main Script
Create a file named
app.js (or any name you prefer) and add the following code:4
Configure Cartesia TTS
In the script, you’ll find a configuration section for Cartesia TTS. Make sure to set the following variables according to your needs:
5
Set Up Twilio Calling
Configure your Twilio outbound and inbound numbers:
6
Implement Main Logic
The
main() function orchestrates the entire process:- Connects to the Cartesia TTS WebSocket
- Tests the TTS WebSocket
- Sets up a Twilio WebSocket server
- Creates an ngrok tunnel for the Twilio WebSocket
- Initiates the call using Twilio
7
Run the Application
To run the application, use the following command:
How It Works
- The script establishes a connection to Cartesia’s TTS WebSocket.
- It sets up a local WebSocket server to communicate with Twilio.
- An ngrok tunnel is created to expose the local WebSocket server to the internet.
- A call is initiated using Twilio, connecting to the ngrok tunnel.
- When the call connects, the script sends the predefined message to Cartesia’s TTS.
- Cartesia converts the text to speech and sends audio chunks back.
- The script forwards these audio chunks to Twilio, which plays them on the call.
Customization
- To change the spoken message, modify the
partialResponsevariable. - Adjust the voice parameters in the
voiceobject to change the TTS voice characteristics. - Modify the
audioChunksReceivedthreshold to control when the call should end.
Troubleshooting
- If you encounter any issues, check the console logs for detailed error messages.
- Ensure all required environment variables are correctly set.
- If you see
invalid tunnel configuration, make sure you’re using the better supported@ngrok/ngrokpackage and notngrok.