Configuring Maximum Duration for Vercel Functions

Learn how to set the maximum duration of a Vercel Function.
Table of Contents

The maximum duration configuration determines the longest time that a function in can run. This guide will walk you through configuring the maximum duration for your Vercel Functions.

You can't configure a maximum duration for functions using the Edge runtime. They can run indefinitely provided they send an initial response within 25 seconds.

For other runtimes, the method of configuring the maximum duration depends on your framework and runtime:

For these runtimes / frameworks, you can configure the number of seconds directly in your function:

app/api/my-function/route.ts
export const maxDuration = 5; // This function can run for a maximum of 5 seconds
export const dynamic = 'force-dynamic';
 
export function GET(request: Request) {
  return new Response('Vercel', {
    status: 200,
  });
}

For these runtimes and frameworks, configure the maxDuration property of the functions object in your vercel.json file:

vercel.json
{
  "functions": {
    "api/test.js": {
      "maxDuration": 30 // This function can run for a maximum of 30 seconds
    },
    "api/*.js": {
      "maxDuration": 15 // This function can run for a maximum of 15 seconds
    },
    "src/api/*.js": {
      "maxDuration": 25 // You must prefix functions in the src directory with /src/
    }
  }
}

If your Next.js project is configured to use src directory, you will need to prefix your function routes with /src/ for them to be detected.

The order in which you specify file patterns is important. For more information, see Glob pattern .

While Vercel specifies defaults for the maximum duration of a function, you can also override it in your vercel.json file with:

vercel.json
{
  "functions": {
    "app/api/**/*": {
      "maxDuration": 5 // All functions can run for a maximum of 5 seconds
    }
  }
}

This glob pattern will match everything in the specified path, so you may wish to be more specific by adding a file type, such as app/api/**/*.ts instead.

There are limits to the maximum duration you can set for a function. To learn more about these limits, see the Max duration limits documentation.

Last updated on April 27, 2024