Vercel's Serverless Functions empower developers to run code on-demand without the need for managing infrastructure, provisioning servers, or upgrading hardware. Available on all plans, Serverless Functions offer a versatile solution for handling tasks such as user authentication, form submissions, database queries, custom Slack commands, and more.
Key Features
Dynamic Scalability: As traffic increases, Serverless Functions automatically scale up and down to meet demand, ensuring cost-effective resource utilization without downtime.
Serverless Functions Location
Serverless Functions should execute near your data source to minimize latency. By default, they execute in Washington, D.C., USA (iad1). You can set a new default region through your project's settings on Vercel. Enterprise teams can configure multiple regions.
Adding Utility Files to /api Directory
To include extra code files, like utils.js or my-types.d.ts, in the /api folder without turning them into Serverless Functions, prefix them with underscore (_), dot (.), or end with .d.ts.
Serverless Functions Lifecycle
For each incoming request, a new invocation of a Serverless Function occurs. Vercel optimizes performance by reusing recently executed functions for subsequent invocations, minimizing the number of active functions over time. In the absence of incoming traffic, Serverless Functions scale down to zero.
Error Logs
Runtime logs for Serverless Functions are accessible in the Logs tab, held based on your plan.
Local Development
When building Next.js applications on Vercel, use the native next dev command and local development server to iterate on your API Routes.
Logging Serverless Functions
Utilize runtime logs to view all logs related to your Serverless Functions. Filter logs by selecting "Serverless" under Type in the Functions section.
Number of Logs per Request
Expect multiple logs per request, especially when rendering new pages or using Incremental Static Regeneration (ISR).
Bundling Serverless Functions
An internal process bundles multiple Serverless Functions into a single function to optimize resources. This feature is currently enabled for Next.js and will expand to other scenarios.
For advanced configurations, create a vercel.json file for Runtimes and customizations. Address asynchronous use cases with queuing, pooling, or streaming approaches.
Improving Performance
Explore ways to enhance Serverless Function performance, especially addressing cold start latency. See "How can I improve serverless function cold start performance on Vercel?" for guidance.
Supported APIs
Serverless Functions using the Node.js runtime support all Node.js APIs, including standard Web APIs. Example using the URL API:
// app/api/url-example/route.ts
import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const hasTitle = searchParams.has('title');
const title = hasTitle
? searchParams.get('title')?.slice(0, 100)
: 'My default title';
return NextResponse.json(
{ title },
{
status: 200,
},
);
}
Technical Details
CPU Calculation
Serverless Functions are allocated CPU power based on the configured memory amount. For example, with 1,769 MB memory configured, a Serverless Function has the equivalent of one vCPU.
Limits
Refer to the Runtimes reference for details on Serverless Functions limits, covering size, memory, concurrency, payload size, duration, and pricing.
Related Articles
Git Integration: Learn how Vercel integrates with Git for an efficient workflow.
Serverless Functions Overview
Vercel's Serverless Functions empower developers to run code on-demand without the need for managing infrastructure, provisioning servers, or upgrading hardware. Available on all plans, Serverless Functions offer a versatile solution for handling tasks such as user authentication, form submissions, database queries, custom Slack commands, and more.
Key Features
Serverless Functions Location
Serverless Functions should execute near your data source to minimize latency. By default, they execute in Washington, D.C., USA (iad1). You can set a new default region through your project's settings on Vercel. Enterprise teams can configure multiple regions.
Adding Utility Files to
/api
DirectoryTo include extra code files, like
utils.js
ormy-types.d.ts
, in the/api
folder without turning them into Serverless Functions, prefix them with underscore (_), dot (.), or end with .d.ts.Serverless Functions Lifecycle
For each incoming request, a new invocation of a Serverless Function occurs. Vercel optimizes performance by reusing recently executed functions for subsequent invocations, minimizing the number of active functions over time. In the absence of incoming traffic, Serverless Functions scale down to zero.
Error Logs
Runtime logs for Serverless Functions are accessible in the Logs tab, held based on your plan.
Local Development
When building Next.js applications on Vercel, use the native
next dev
command and local development server to iterate on your API Routes.Logging Serverless Functions
Utilize runtime logs to view all logs related to your Serverless Functions. Filter logs by selecting "Serverless" under Type in the Functions section.
Number of Logs per Request
Expect multiple logs per request, especially when rendering new pages or using Incremental Static Regeneration (ISR).
Bundling Serverless Functions
An internal process bundles multiple Serverless Functions into a single function to optimize resources. This feature is currently enabled for Next.js and will expand to other scenarios.
For advanced configurations, create a
vercel.json
file for Runtimes and customizations. Address asynchronous use cases with queuing, pooling, or streaming approaches.Improving Performance
Explore ways to enhance Serverless Function performance, especially addressing cold start latency. See "How can I improve serverless function cold start performance on Vercel?" for guidance.
Supported APIs
Serverless Functions using the Node.js runtime support all Node.js APIs, including standard Web APIs. Example using the URL API:
Technical Details
CPU Calculation
Serverless Functions are allocated CPU power based on the configured memory amount. For example, with 1,769 MB memory configured, a Serverless Function has the equivalent of one vCPU.
Limits
Refer to the Runtimes reference for details on Serverless Functions limits, covering size, memory, concurrency, payload size, duration, and pricing.
Related Articles
Last updated on February 17, 2023