Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Vercel Functions

Functions enable running compute on-demand without needing to manage your own infrastructure, provision servers, or upgrade hardware.
Table of Contents

Functions are available on all plans

Vercel Functions enable server-side code execution on Vercel's Managed Infrastructure, removing the need for server management or resource provisioning. These functions scale automatically with user demand and can interact with APIs, databases, and other resources as part of your project's deployment.

When deployed, Vercel's Framework defined infrastructure, automatically sets up the necessary environment, tools, and patterns as required by your chosen framework. This ensures optimal configuration for your functions, whether they run on the edge for reduced latency or in a specific region.

Vercel Functions can be written with, or without, a framework, and handle tasks such as:

  • Streaming data: Process data in real-time, such as chat messages, AI data, or financial transactions
  • Authentication: Implement authentication and authorization logic
  • Data Processing: Manage intensive tasks, such as image/video manipulation, without impeding client-side performance
Functions location within Vercel's managed infrastructure
Functions location within Vercel's managed infrastructure

The infrastructure and abilities of your Vercel Function is determined by the runtime you choose:

  • Node.js runtime (Serverless Functions): Gives you access to all the Node.js APIs that you would expect for writing on the web, with the ability to configure machine resources and dependencies.
  • Edge runtime (Edge Functions): Executes your code at the edge, close to the user. Uses a limited set of Node.js APIs.
  • Python runtime: Enables you to write your functions using Python, including Django and Flask.
  • Go runtime: Enables you to write your functions using Go, exposing a single HTTP handler from a .go file within an /api directory at your project's root.
  • Ruby runtime: Enables you to write your functions using Ruby, exposing a single HTTP handler from a .rb file within an /api directory at your project's root.

To learn more, see choosing a runtime.

To get started with creating your first function, copy the code below:

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}`);
}

To learn more, see the quickstart or deploy a template from the options below:

To learn more about the limitations for Vercel Functions, see the Runtimes reference. This highlights common limits such as size, memory, concurrency, payload size limit, duration, and pricing.

Functions have full support for the console API, including time, debug, timeEnd, etc.

Runtime logs for all functions can be found in the Logs tab. You can use the various filters and options to find specific log information. These logs are held for an amount of time based on your plan.

When a Function on a specific path receives a user request, you may see more than one log when the application renders or regenerates the page.

This can occur in the following situations:

  1. When a new page is rendered
  2. When you are using Incremental Static Regeneration (ISR)

In the case of ISR, multiple logs are the result of:

  • A stale page having to be regenerated. For stale pages, both HTML (for direct browser navigation) and JSON (for Single Page App (SPA) transitions) are rendered simultaneously to maintain consistency
  • On-demand ISR happening with fallback set as blocking. During on-demand ISR, the page synchronously renders (e.g., HTML) upon request, followed by a background revalidation of both HTML and JSON versions

In Next.js projects, logged functions include API Routes (those defined in pages/api/**/*.ts or app/**/route.ts).

Pages that use SSR, such as those that call getServerSideProps or export revalidate, will also be available both in the filter dropdown and the real time logs.

For more information on what to do next, we recommend the following articles:

Last updated on October 3, 2024