Functions API Reference
Learn about available APIs when working with Vercel Functions.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 version, you must use the Node.js signature.
Parameter | Description | Next.js | Other Frameworks |
---|---|---|---|
request | An instance of the Request object | NextRequest | Request |
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 enqueues an asynchronous task to be performed during the lifecycle of the request. You can use it for anything that can be done after the response is sent, such as logging, sending analytics, or updating a cache, without blocking the response from being sent. waitUntil()
is available in the Node.js and Edge Runtime. Promises passed to waitUntil()
will have the same timeout as the function itself. If the function times out, the promises will be cancelled.
To use waitUntil()
in your function, import the waitUntil()
method from @vercel/functions
package. For more information, see the @vercel/functions
reference.
When using waitUntil()
with Edge Runtime, be aware
that it only allows you to extend the function's execution for another 30
seconds following a client disconnection. If your task takes longer than that
to complete, the function will be terminated.
// Use the @vercel/functions package to import waitUntil
import { waitUntil } from '@vercel/functions';
export function GET() {
const country = request.headers.get('x-vercel-ip-country');
// Returns a response immediately while keeping the function alive
waitUntil(fetch(`https://api.vercel.app/countries/?incr=${country}`));
return new Response(`You're visiting from beautiful ${country}`);
}
To configure your function when using the App Router in Next.js, you use segment options, rather than a config
object.
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.
Property | Type | Description |
---|---|---|
runtime | string | This optional property defines the runtime to use, and if not set the runtime will default to nodejs . |
preferredRegion | string | This 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 |
maxDuration | int | This 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 |
Was this helpful?