Reference
6 min read

Functions API Reference

Learn about available APIs when working with Vercel Functions.
Table of Contents

Functions are defined similar to a Route Handler in Next.js. When using Next.js App Router, you can define a function in a file under app/api/{example}/route.ts in your project. Vercel will deploy any file under app/api/ as a function.

Vercel Functions use a Web Handler, which consists of the request parameter that is an instance of the web standard Request API. Next.js extends the standard Request object with additional properties and methods.

To use a Web Handler, you must be using Node.js 18 or later. If you are using an earlier, you must use the Node.js signature.

ParameterDescriptionNext.jsOther Frameworks
requestAn instance of the Request objectNextRequestRequest
Next.js (/app)
Next.js (/pages)
Other frameworks
app/api/hello/route.ts
export const dynamic = 'force-dynamic'; // static by default, unless reading the request
 
export function GET(request: Request) {
  return new Response(`Hello from ${process.env.VERCEL_REGION}`);
}

The waitUntil() method is not currently supported with Next.js.

To learn more about streaming responses, see the Streaming quickstart.

To configure your function when using the App Router in Next.js, you use segment options, rather than a config object.

app/api/example/route.ts
export const runtime = 'nodejs';
export const maxDuration = 15;

The table below shows a highlight of the valid config options. For detailed information on all the config options, see the Configuring Functions docs.

PropertyTypeDescription
runtimestringThis optional property defines the runtime to use, and if not set the runtime will default to nodejs.
preferredRegionstringThis optional property and can be used to specify the regions in which your function should execute. This can only be set when the runtime is set to edge
maxDurationintThis optional property can be used to specify the maximum duration in seconds that your function can run for. This can't be set when the runtime is set to edge
Last updated on February 21, 2024