Skip to content
Menu
 

Functions API Reference

Last updated February 18, 2026

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/my-route/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.

ParameterDescriptionNext.jsOther Frameworks
requestAn instance of the Request objectNextRequestRequest
context Deprecated, use @vercel/functions insteadN/A{ waitUntil }
api/hello.ts
export function GET(request: Request) {
  return new Response('Hello from Vercel!');
}
api/hello.js
export function GET(request) {
  return new Response('Hello from Vercel!');
}
app/api/hello/route.ts
export function GET(request: Request) {
  return new Response('Hello from Vercel!');
}
app/api/hello/route.js
export function GET(request) {
  return new Response('Hello from Vercel!');
}
This feature is only available in the Node.js runtime.

Cancelling requests lets you clean up resources or stop long-running tasks when the client disconnects, such as when a user stops an AI chat response or closes a browser tab.

When cancellation is enabled, Vercel notifies your function through the standard AbortSignal on request.signal. When the client disconnects, the signal fires and the function is terminated.

This differs from a standalone Node.js server, where your process continues running after a client disconnects. In a serverless environment, the execution context can be reclaimed at any time after cancellation. If you have work that must complete (such as flushing logs, writing to a database, or updating a cache), wrap it in waitUntil or after.

Cancellation is opt-in. In your vercel.json, add "supportsCancellation": true to the specific paths you want to enable it for:

vercel.json
{
  "regions": ["iad1"],
  "functions": {
    "api/*": {
      "supportsCancellation": true
    }
  }
}

Termination on disconnect applies to every function matching the glob, whether or not your code listens for the abort signal. Any work not wrapped in waitUntil or after will be lost on cancellation. This is why cancellation is opt-in. Enabling it means your functions can be terminated early, so you should only enable it for functions that are prepared to handle that.

The request.signal is a standard AbortSignal. You can pass it directly to any API that accepts one, such as fetch:

api/proxy/route.ts
export async function GET(request: Request) {
  const response = await fetch('https://my-backend-service.example.com', {
    headers: { Authorization: `Bearer ${process.env.AUTH_TOKEN}` },
    signal: request.signal,
  });
 
  return new Response(response.body, {
    status: response.status,
    headers: response.headers,
  });
}

If you need to perform custom cleanup on abort or coordinate cancellation across multiple operations, you can create your own AbortController and wire it to request.signal:

api/abort-controller/route.ts
export async function GET(request: Request) {
  const abortController = new AbortController();
 
  request.signal.addEventListener('abort', () => {
    console.log('request aborted');
    abortController.abort();
  });
 
  const response = await fetch('https://my-backend-service.example.com', {
    headers: {
      Authorization: `Bearer ${process.env.AUTH_TOKEN}`,
    },
    signal: abortController.signal,
  });
 
  return new Response(response.body, {
    status: response.status,
    headers: response.headers,
  });
}

To run cleanup work after the client disconnects, combine waitUntil with a deferred promise. This keeps the execution context alive until your cleanup finishes. In this example, abortPendingTask only runs when the client disconnects. On normal completion, the pipeTo promise resolves cleanup once all response bytes have been sent:

api/cancel-with-cleanup/route.ts
import { waitUntil } from '@vercel/functions';
 
export async function GET(request: Request) {
  const cleanup = Promise.withResolvers<void>();
  waitUntil(cleanup.promise);
 
  request.signal.addEventListener('abort', () => {
    console.log('request aborted, cancelling pending task');
    abortPendingTask().finally(cleanup.resolve);
  });
 
  const response = await fetch('https://my-backend-service.example.com', {
    headers: { Authorization: `Bearer ${process.env.AUTH_TOKEN}` },
    signal: request.signal,
  });
 
  const { readable, writable } = new TransformStream();
  response.body.pipeTo(writable).finally(() => {
    if (!request.signal.aborted) cleanup.resolve();
  });
 
  return new Response(readable, {
    status: response.status,
    headers: response.headers,
  });
}
 
async function abortPendingTask() {
  await fetch('https://my-backend-service.example.com/cancel', {
    method: 'POST',
  });
}

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;
app/api/example/route.ts
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
This feature is supported on the Node.js and Python runtimes.

A SIGTERM signal is sent to a function when it is about to be terminated, such as during scale-down events. This allows you to perform any necessary cleanup operations before the function instance is terminated.

Your code can run for up to 500 milliseconds after receiving a SIGTERM signal. After this period, the function instance will be terminated immediately.

api/hello.ts
process.on('SIGTERM', () => {
  // Perform cleanup operations here
});
api/hello.js
process.on('SIGTERM', () => {
  // Perform cleanup operations here
});

The @vercel/functions package provides a set of helper methods and utilities for working with Vercel Functions.

  • waitUntil(): This method allows you to extend the lifetime of a request handler for the duration of a given Promise . It's useful for tasks that can be performed after the response is sent, such as logging or updating a cache.
  • getEnv: This function retrieves System Environment Variables exposed by Vercel.
  • geolocation(): Returns location information for the incoming request, including details like city, country, and coordinates.
  • ipAddress(): Extracts the IP address of the request from the headers.
  • invalidateByTag(): Marks a cache tag as stale, causing cache entries associated with that tag to be revalidated in the background on the next request.
  • dangerouslyDeleteByTag(): Marks a cache tag as deleted, causing cache entries associated with that tag to be revalidated in the foreground on the next request.
  • invalidateBySrcImage(): Marks all cached content associated with a source image as stale, causing those cache entries to be revalidated in the background on the next request. This invalidates all cached transformations of the source image.
  • dangerouslyDeleteBySrcImage(): Marks all cached content associated with a source image as deleted, causing those cache entries to be revalidated in the foreground on the next request. Use this method with caution because deleting the cache can cause many concurrent requests to the origin leading to cache stampede problem.
  • getCache(): Obtain a RuntimeCache object to interact with the Vercel Runtime Cache.

See the @vercel/functions documentation for more information.

The @vercel/oidc package was previously provided by @vercel/functions/oidc.

The @vercel/oidc package provides helper methods and utilities for working with OpenID Connect (OIDC) tokens.

See the @vercel/oidc documentation for more information.

The @vercel/oidc-aws-credentials-provider package was previously provided by @vercel/functions/oidc.

The @vercel/oidc-aws-credentials-provider package provides helper methods and utilities for working with OpenID Connect (OIDC) tokens and AWS credentials.

See the @vercel/oidc-aws-credentials-provider documentation for more information.


Was this helpful?