Skip to content

Edge Functions

Build high-performance APIs that are deployed to every Edge Network region.

Vercel's Edge Functions enable you to deliver dynamic, personalized content with the lightweight Edge Runtime.

Our Edge Runtime is more performant and cost-effective than Serverless Functions on average. Edge Functions are deployed globally on our Edge Network, and can automatically execute in the region nearest to the user who triggers them. They also have no cold boots, which means they don't need extra time to start up before executing your code.

Edge Functions are useful when you need to interact with data over the network as fast as possible, such as executing OAuth callbacks, responding to webhook requests, or interacting with an API that fails if a request is not completed within a short time limit.

See the Quickstart to learn how to create your first Edge Function.

Edge Functions use Vercel's Edge Runtime, which is built on the same high-performance V8 JavaScript and WebAssembly engine used by the Chrome browser.

Using the V8 engines allows Edge Functions to run in isolated execution environments that don't require a container or virtual machine. This constrains the Edge Runtime in many ways, but also keeps it lightweight. Starting up an Edge Function process requires fewer resources than a Serverless Function, effectively eliminating cold boot times.

The universal benefit of Edge Functions is the Edge Runtime. It's cost-effective and resource-minimal. However, you can also use Edge Functions to deliver dynamic content at low latency in certain contexts.

Delivering dynamic content at low latency is possible because all Edge Functions are duplicated and deployed across a global network of data centers on Vercel's Edge Network. They can be invoked in whichever data center is closest to the user, or in a region near your databases. You can also serve them personalized content based on their region at high speeds.

Edge Functions also run after the cache, and can both cache and return responses.

Edge Functions location within Vercel infrastructure.

Edge Functions can offer faster responses than Serverless Functions on average for globally distributed userbases. The following are some specific benefits that Edge Functions provide:

Because Edge Functions can execute geographically near your users, or near your databases, their network requests can travel shorter distances and accrue lower costs than Serverless Functions.

Serverless Functions are usually deployed and invoked in one region, so they sometimes travel far to reach your database before they can deliver a response to the user.

See our docs on Edge Network regions to configure your Edge Functions to execute closest to their dependencies.

Edge Functions that don't rely on databases or geographically distant dependencies can be configured to execute near your users. This can lead to responses being delivered to users at much lower latency than Serverless Functions.

For example, you could enable or disable feature flags in the frontend of your application based on the region an Edge Function executes in. Using an Edge Function to do this would cause all app routes that check for the feature flag to respond hundreds of milliseconds faster, since they would not suffer from the slow cold boot times that come with Serverless Functions.

You can use custom code in your Edge Functions to deliver content based on which data center on the Edge Network a user is invoking a Function in.

You can compile libraries and tools written in languages like C or Rust and use them in Edge Functions. See our WebAssembly docs to learn more.

One advantage of Edge Functions is that they can be executed at data centers that are geographically close to your visitors, reducing network request times. However, this benefit is mitigated if the function depends a data source far away from your visitors.

For example, imagine a visitor in Japan invokes a function that depends on a database located in the US. The function will be invoked on machines in Japan on the Edge Network, but the final response requires a round-trip from Japan -> U.S. -> Japan, making the overall response time to the visitor slower.

To ensure efficient routing of your Edge Function, you can specify which region your function should execute in. To do so, edit the config object that your Edge Function exports:

pages/api/hello.ts
export const config = {
  runtime: 'edge', // this is a pre-requisite
  regions: ['iad1'], // only execute this function on iad1
};

Edge Functions are defined in JavaScript or TypeScript files in your codebase. You can specify a Function's region by exporting a config object from within the file.

Add a regions key to the config object, assigning the ID of your preferred region as a string.

Here's an example of an Edge API Route with its region set to 'iad1', Vercel's region near Washington, D.C., USA. You can see a list of valid Edge region IDs in our Edge Network regions list.

pages/api/hello.ts
import { NextRequest, NextResponse } from 'next/server';

export const config = {
  runtime: 'edge', // this is a pre-requisite
  regions: ['iad1'], // only execute this function on iad1
};

export default (req: NextRequest) => {
  return NextResponse.json({
    name: `Hello, from ${req.url} I'm now an Edge Function!`,
  });
};

Read our docs on Edge Network regions to learn more about the benefits of configuring regions for your Edge Functions. Check out our list of Edge regions to find the appropriate region ID.

As with all solutions, Edge Functions may not be suitable for all use cases and there are some limitations you should be aware of:

See our Edge Functions limitations page for more information.

  • Edge Functions run after the Edge Network cache, and unlike Edge Middleware, can therefore cache responses
  • Like Edge Middleware, but unlike Serverless Functions, Edge Functions use the Edge Runtime. This allows for faster cold boots, but means that you cannot use Node.js APIs in Edge Functions
  • Their signature matches Edge Middleware
    • Note that when using Edge Functions with Next.js (as API Routes configured to run at the edge), you can import and use the next/server helpers (NextRequest, NextResponse). However, this is not a requirement
  • Edge Functions can return responses
  • When implementing Edge Functions, you can add the @vercel/edge package and import helper functions such as geolocation and ipAddress
  • For information on comparing Edge Functions and Serverless Functions, see the comparison table.

Edge Functions have full support for the console API, including time, debug, timeEnd, etc. Logs will appear inside your Vercel project by clicking View Functions Logs next to the deployment.