Vercel Serverless Functions can time out due to various reasons such as long-running tasks or inefficient code. This guide outlines the maximum durations for each Vercel plan and provides troubleshooting steps to address timeout issues.
Maximum function durations
Functions on the Edge Runtime do not have a max duration limit but must begin sending a response within 25 seconds. They require an initial response then allow you to continue streaming responses after that time.
Serverless Functions, however, have default and maximum duration limits. To learn how to configure the maximum duration of a serverless function, see the Max duration docs.
Troubleshooting serverless functions timeouts
If you are seeing an execution timeout error, check the following possible causes:
The function is taking too long to process a request
Check that any API, or database requests you make in your function, are responding within the Serverless Function Execution Duration limit applicable to your plan.
import type { VercelRequest, VercelResponse } from '@vercel/node';
export default function handler( request: VercelRequest, response: VercelResponse,) {
// res.send("Hello, World!") return response.status(400).json({ error: 'Bad Request' });}
Users on Pro and Enterprise plans can configure the maximum duration by using the maxDuration
property. See the maxDuration docs for more information.
If you require a longer execution, Edge Functions provide an alternative that only require an initial response within 25 seconds and allow you to continue streaming responses after that time.
The function isn't returning a response
The function must return an HTTP response, even if that response is an error. If no response is returned, the function will time out.
The example below fetches data from an API. If successful, it returns a 200 HTTP status with data. For API errors, it returns the corresponding error status. However, if an exception occurs (API is down), the function logs the error but does not send an HTTP response, leading to a timeout. Always ensure to send an HTTP response to prevent timeouts.
import type { VercelRequest, VercelResponse } from '@vercel/node';
export default async function handler( request: VercelRequest, response: VercelResponse,) { const apiUrl = 'https://api.example.com/data';
try { const apiResponse = await fetch(apiUrl);
if (apiResponse.ok) { const data = await apiResponse.json(); return response.status(200).json(data); } else { // Properly return an error response return response.status(apiResponse.status).json({ message: `Failed to fetch data. API returned ${apiResponse.status}`, }); } } catch (error) { // Forget to return an HTTP response, leading to a function timeout console.error(`Error fetching data from ${apiUrl}: ${error}`); }}
You have an infinite loop within your function
Check that your function is not making an infinite loop at any stage of execution
import type { VercelRequest, VercelResponse } from '@vercel/node';
export default function handler( request: VercelRequest, response: VercelResponse,) { let counter = 0;
// A condition that might lead to an infinite loop while (counter >= 0) { if (someExternalCondition()) { counter--; } else { counter++; }
// Do some other work }
// This line will never be reached due to the infinite loop response.status(200).json({ body: request.body, query: request.query, cookies: request.cookies, });}
Upstream errors
If you're connecting to an external database or depending on a third-party API before responding, check to make sure there are no errors upstream. Make sure you have all the right environment variables set for talking to third party services successfully.
In the event of an error upstream, it is still good practice to return something informative to the client. If the upstream service hasn't responded to you successfully within a certain time, return a response indicating the error.
import type { VercelRequest, VercelResponse } from '@vercel/node';
export default async function handler( request: VercelRequest, response: VercelResponse,) { await within(dbGetUsers, response, 7000);}
async function within(fn: Function, res: VercelResponse, duration: number) { const id = setTimeout(() => { res.json({ message: 'There was an error with the upstream service!' }); }, duration);
try { const data = await fn(); clearTimeout(id); res.json(data); } catch (e) { res.status(500).json({ message: e.message }); }}
async function getUsers() { return await dbGetUsers();}