Skip to content

Edge Functions Quickstart

Learn how to get started with Edge Functions on Vercel using Vercel CLI, or Next.js

Edge Functions are available in Beta on all plans

In this quickstart guide, you'll learn how to get started with Edge Functions using the Vercel CLI or Next.js. For information on the API and how to use it, see the Edge Functions API documentation.

You should have the latest version (>= v25) of the Vercel CLI. To check your version, use vc --version. To install or update Vercel CLI, use:


npm i -g vercel

Updating Vercel CLI using the npm command.

Before you begin, ensure that your app is using the most recent version of Next.js:


npm i next@latest

Updating Next.js using the npm command.

Use create-next-app to create a new Next.js project:

terminal
npx create-next-app@latest --typescript

Follow the prompts to set your project up. You'll be using the TypeScript variant of the starter project as you will be importing some helpers from next/server in later steps.

When you open the project, notice that it comes with a single API Route (hello.ts):

Add the following code to your function:

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

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

export default hello;

export const config = {
  runtime: 'experimental-edge',
};

This code does a couple of important things that you should be aware of:

  • We're importing a helper from next/server, which extends the native Response object. This is not a requirement
  • We specify the function's runtime with an exported config object: runtime: 'experimental-edge'. This is required to create an Edge Function.
  • Because the function is using the Edge Runtime, it's signature can follow the same syntax as Edge Middleware

Note that you cannot use Node.js APIs in Edge Functions. See the Limitations section for more information.

Use the Vercel CLI to deploy your Edge Function:

terminal
vercel deploy

Follow the prompts to deploy your function and once done, open the Production link.

From your dashboard click on the deployed project and choose the Functions tab. This tab displays logs from any running functions within your project. Use the dropdown to select the api/hello function.

The Runtime of the function will read Edge, and the Region will be the default region for all new projects, which is Washington, D.C., USA.

Congratulations! You have now created a new Next.js project and deployed it as an Edge Function. You also learned about the important parts of creating the function and how to see the runtime and regions the function in your Vercel dashboard.

  • The signature of an Edge Function matches that of Edge Middleware because they both use the same APIs.
  • Edge Functions export a custom config object that specifies which runtime to use. In the case of Edge Functions, the runtime is the Edge Runtime
  • Unlike Edge Middleware, they can return responses
  • Helpers that extend the native Response, Event, and Request objects can be imported from next/server
  • You cannot use Node.js APIs in Edge Functions

First, create a new project and add an /api directory and a hello.ts file inside it. This is where the code that will be deployed as an Edge Function will live. The file extension can be .ts or .js, this guide uses TypeScript.

Your folder structure should look like the following:

Alternatively, you can add an Edge Function to your existing project by adding a new TypeScript file within the /api directory.

The important part to note here is that while the file itself can be named anything, it should be located inside the /api directory. For more information, see Using Edge Functions.

In the hello.ts file, add the following code:

api/hello.ts
export const config = {
  runtime: 'experimental-edge',
};

export default (req) => new Response('Hello world!');
  • We specify the runtime of the function with an exported config object: runtime: 'experimental-edge'. This is required to create an Edge Function.
  • Because the function is using the Edge Runtime, its signature can follow the same syntax as Edge Middleware

Use the Vercel CLI to deploy your Edge Function:

terminal
vercel deploy

Follow the prompts to deploy your function and once done, open the Production link.

From your dashboard click on the deployed project and choose the Functions tab. This tab displays logs from any running functions within your project. Use the dropdown to select the api/hello function.

The Runtime of the function will read Edge, and the Region will be the default region for all new projects, which is Washington, D.C., USA.

Congratulations! You have now created a new project and deployed it as an Edge Function. You also learned about what is required to create a function and how to view function logs in your Vercel dashboard.

  • The signature of an Edge Function matches that of Edge Middleware because they both use the same APIs.
  • Edge Functions export a custom config object that specifies which runtime to use. In the case of Edge Functions, the runtime is the Edge Runtime
  • Unlike Edge Middleware, they can return responses
  • You cannot use Node.js APIs in Edge Functions