ReSponse AI integration with Eleven Labs Voice

Introduction

This guide will help you get started with the Eleven Labs Voice API. You will learn how to generate a voice from a text and how to use the generated audio file in your application.

Prerequisites


    try {

        const response = await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${voice_id}`, requestOptions);

        console.log("API Response Status:", response.status);

        if (response.ok) {

            const audioBlob = await response.blob();

            console.log("audioBlob:", audioBlob);

            const arrayBuffer = await audioBlob.arrayBuffer();

            const buffer = Buffer.from(arrayBuffer);

            res.status(200).send(buffer);

        } else {
            console.error("Error:", response.status);
            // Handle the error, for example by returning an error response
            res.status(response.status).send("Error generating voice");
        }

    } catch (error) {
        console.error("Error:", error);
        // Handle the error, for example by returning an error response
        res.status(500).send("Error generating voice");
}


Transcription

Transcription is the process of converting speech into text. This is the first step in the process of generating a voice from a text.


export default async (req, res) => {

    var audio = req.body.audio;

    const buffer = Buffer.from(audio.binaryData, 'base64');

    try {

        const formData = new FormData();

        formData.append('file', buffer, {
            filename: 'audio.webm',
            contentType: 'audio/webm'
        });
        
        formData.append('model', 'whisper-1');

        const response = await fetch('https://api.openai.com/v1/audio/transcriptions', {
            method: 'POST',
            headers: {
                "Authorization": process.env.OPEN_AI,
            },
            body: formData
        });

        if (response.ok) {
            const responseText = await response.json();
            console.log(responseText);
            return res.status(200).json(responseText);

        } else {
            const errorMessage = `Error: ${response.status} - ${response.statusText}`;
            console.error(errorMessage);
            return res.status(response.status).send(`Error transcribing voice: ${errorMessage}`);
        }
    } catch (error) {
        console.error(error);
        return res.status(500).send(`Error transcribing voice: ${error.message}`);
    }
}