Implementing Play.ht API with Nodejs Application

Posted By :Mohd Ubaish |26th February 2024
Implementing Play.ht API with Nodejs Application : A step by step guide

 

Prerequisites

 

Before we begin, make sure you have the following:

1.Basic understanding of JavaScript and Node.js.
2.Node.js installed on your machine.
3.Access to the Play.ht API key and X-USER-ID.
4.Familiarity with making HTTP requests using Axios or a similar library.

 

Step 1: Setting Up Your Node.js Project
First, create a new directory for your Node.js project and initialize a new Node.js project inside it. Open your terminal and execute the following commands.

 

mkdir playht-nodejs
cd playht-nodejs
npm init -y

 

Step 2: Installing Dependencies
Next, install the necessary dependencies for your project. Since we'll be making HTTP requests to the Play.ht API, we'll use Axios for this purpose:

 

npm install axios dotenv express

 

Step 3: Environment Configuration
Create a .env file in your project directory to store your environment variables, including your Play.ht API key and X-USER-ID:

 

PLAY_HT_API_KEY = YOUR_PLAY_HT_API_KEY
PLAY_HT_X_USER_ID = YOUR_PLAY_HT_X_USER_ID

 

Replace YOUR_PLAY_HT_API_KEY and YOUR_PLAY_HT_X_USER_ID with your actual Play.ht API key and X-USER-ID.

 

Step 4: Writing the Play.ht Integration Code
Create a new file named playhtIntegration.js in your project directory and add the following code:

 


const axios = require('axios');

const PlayHt = async (prompt) => {
    try {
        const url = 'https://api.play.ht/api/v2/tts';
        const data = {
            text: prompt,
            voice: 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',
            output_format: 'mp3',
            voice_engine: 'PlayHT2.0'
        };
        const headers = {
            accept: 'text/event-stream',
            'content-type': 'application/json',
            AUTHORIZATION: process.env.PLAY_HT_API_KEY,
            'X-USER-ID': process.env.PLAY_HT_X_USER_ID,
        };
        const response = await axios.post(url, data, { headers });
        const speech_url = `${response.data.split(`"url":"`)[1].split(`mp3`)[0]}mp3`;
        return speech_url;
    } catch (error) {
        console.error('Error generating speech:', error);
        throw new Error('Failed to generate speech');
    }
};


 

Step 5: Integrating Play.ht with Your Application
Now that you've implemented the Play.ht integration code, you can integrate it into your Node.js application. Import the PlayHt function into your application's code wherever you need to generate speech from text, and call it with the desired text prompt.

 

Step 6: Testing Your Integration
You can test your Play.ht integration by calling the PlayHt function with a text prompt and observing the generated speech URL. Ensure that your Play.ht API credentials are correctly set up in your environment variables and that your application can successfully make requests to the Play.ht API endpoint.


About Author

Mohd Ubaish

Mohd Ubaish is a highly skilled Backend Developer with expertise in a wide range of technologies, including React, Node.js, Express.js, MongoDB, and JavaScript.With a deep understanding of both front-end and back-end development, he is currently dedicated to the development of TripCongo Web Discovery.He is committed to creating a user-friendly experience on the frontend while ensuring the seamless functionality of the backend. By staying up-to-date with the latest industry trends and advancements, he strives to provide innovative solutions and optimize the performance of the application.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us