Search code examples
node.jsresponseopenai-api

How to handle the response OpenAI Text-To-Speech API in Node.js?


Here's my code:

const speechUrl = 'https://api.openai.com/v1/audio/speech';
    
const headers = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
};

async function voiceGenerator(text) {
    console.log('voiceGenerator is triggered');
    console.log('text: ', text);
    const body = {
        "model": "tts-1",
        "input": text,
        "voice": "alloy",
        "response_format": "mp3",
        "speed": 0.9
    };

    return axios.post(speechUrl, body, { headers: headers })
    .then((res) => {
        if (res.status === 200 || res.status === 204) {
            // res.data = Buffer.from(res.data, 'binary');
            return res.data;
        } else {
            console.log('res: ', res);
            throw res;
        }
    })
    .catch((err) => {
        console.error('OpenAI API failed, error: ', err);
        throw err;
    });
}

And my question is that how do I convert the thing I received into mp3 buffer and store it? I don't know what exactly am I receiving. All I know is that the Content-Type is audio/mpeg and Transfer-Encoding is chunked.

I can't use openai SDK because it keep throws error no matter when. I had to use API call here. Postman can just get the file by calling it btw.


Solution

  • async function voiceGenerator(text) {
        console.log('voiceGenerator is triggered');
        console.log('text: ', text);
        const body = {
            "model": "tts-1",
            "input": text,
            "voice": "alloy",
            "response_format": "mp3",
            "speed": 0.9
        };
    
        return axios.post(speechUrl, body, { headers: headers, responseType: 'arraybuffer' })
        .then((res) => {
            if (res.status === 200 || res.status === 204) {
                const buffer = Buffer.from(res.data);
    
                return buffer;
            } else {
                console.log('res: ', res);
                throw res;
            }
        })
        .catch((err) => {
            console.error('OpenAI API failed, error: ', err);
            throw err;
        });
    }
    

    This is the solution I reached. It turns out that by adding "responseType": "arraybuffer", the API would return the buffer array that you can convert into buffer later on.