Transcriptions API endpoint returning 400 error - node.js

Hey all! I am currently trying to download an mp3 from the web, then send it to the /transcriptions endpoint, but I keep getting a 400 BAD_REQUEST error.

Initially, I was getting an Invalid file format error (even though I’m just sending an mp3), but I resolved that by first writing the mp3 file I downloaded from the web to a file on disk.

I am now using fs.createReadStream() to read the mp3 file from disk and send to the OpenAI /transcriptions endpoint, but, like I mentioned, I am getting a 400 error every time.

I have tried using the openaipackage as well as just using axios and I get the same result.

Here is my code:

app.get('/api/transcribeAudio', async (request, response) => {
    // Download the audio file
    const audioUrl = 'https://example.com/filename.mp3'
    const outputFilePath = 'file.mp3'

    let downloadResponse;
    try {
        downloadResponse = await axios.get(audioUrl, { responseType: 'arraybuffer' })

        // Convert the arraybuffer to a Buffer
        const mp3Buffer = Buffer.from(downloadResponse.data);

        // Write the Buffer to a file
        fs.writeFileSync(outputFilePath, mp3Buffer);

        console.log('MP3 file saved successfully!');
    } catch(e) {
        console.log('Error downloading mp3', e)
    }

       // OpenAI api call using axios
        let data = new FormData();
        data.append("file", fs.createReadStream(outputFilePath));
        data.append("model", "whisper-1");
        data.append("language", "en");

        let config = {
          method: "post",
          maxBodyLength: Infinity,
          url: "https://api.openai.com/v1/audio/transcriptions",
          headers: {
            Authorization:
              `Bearer ${token}`,
            "Content-Type": "multipart/form-data",
            ...data.getHeaders(),
          },
          data: data,
        }; 

        const response = await axios.request(config);
        const transcription = response.data;
        console.log('transcription', transcription)

        // Using OpenAI library
        const model = 'whisper-1';
        const transcriptionResponse = await openai.createTranscription(
            fs.createReadStream(outputFilePath),
            model
        );
        console.log('transcriptionResponse', transcriptionResponse)
})

Any help is greatly appreciated!!!

Hi,

Typical things to check are

  1. is the file you are transcribing less than 25Mb in size, if it’s larger it will cause an error. If your files are larger you need to using an auto file spitter, one that can detect gaps in the speech so as not to cut off mid word work best.
  2. API key problems, either an invalid API or your API key is not being loaded correctly. I don’t see your API key loading code in that example.
  3. No payment method assigned to your account, in which case you should visit OpenAI Platform

Thanks for the reply @Foxalabs.

  1. My file size I’m testing with is 16Mb.
  2. API Key (token) is being declared in a different part in my file and I confirmed it worked fine by calling a different api endpont successfully with the same key.
  3. I do have a payment method assigned

If there are any other ideas I am all ears!

I tried using a new file, this time an m4a file instead of mp3, and the /transcriptions worked fine. So going to try and figure out what might be happening with my mp3 when I download it that is causing issues.

Thanks for the help!