Server request timeout on Netlify Edge using Nuxt

PLEASE help us help you by writing a good post!

  • we need to know your netlify site name:
    bedtimeai

I have a Nuxt app with a backend server route that fetches an audio file from a ElevenLabs.io API. That API requires a string of text to be sent which will be converted by the API and sends back an audio file url of that string of text. Works good locally, no problem. However, when I deploy to Netlify Edge, this audio conversion process times out for some reason. The server returns a 502 Bad Gateway with a response of “The request timed out.”. Also, it works fine on the server if I just have a small string such as “Hello World”, but if I send over a text string that is about 10 paragraphs in length, I will get that timeout issue. Reminder, THIS WORKS FINE LOCALLY and the Audio API converts the text into speech with no problem.

Could this be an Edge function issue?

Anyone got any ideas on what could be the problem?

Here’s my Nuxt server route code for context:

export default defineEventHandler(async (event) => {
  const { text } = await readBody(event)
  console.log('text for API to use: ', text)
  const response = await $fetch(
    'https://api.elevenlabs.io/v1/text-to-speech/EXAVITQu4vr4xnSDxMaL',
    {
      method: 'POST',
      headers: {
        accept: 'audio/mpeg',
        'Content-Type': 'application/json',
        'xi-api-key': 'XXXXXX'
      },
      body: {
        text: text,
        // text: 'Hello world this is a test. This is just a test from Netlify Edge!',
        model_id: 'eleven_monolingual_v1',
        voice_settings: {
          stability: 0.5,
          similarity_boost: 0.75,
          style: 0,
          use_speaker_boost: true
        }
      }
    }
  )
  return response
})

And this is the API reference for the ElevenLabs API text to speech service:
Text to Speech - ElevenLabs

Thanks for any tips or thoughts…

Have you checked this: Functions overview | Netlify Docs, specifically:

You cannot have functions runnins for indefinite time.

Yes, but is there a way to cancel the execution before 10 seconds, I can have a promise.race whereas if it takes 9000 ms then execute the timeout promise instead and avoid the app to crash, is it possible ?

Return a response anytime before 10 seconds and the execution will end without your app crashing.