Build With Abdallah logo Build With Abdallah Software · AI · Automation
Tutorial 6 min read Jun 24, 2026

Creating a Serverless Function with Next.js 16.2 and Vercel

What You'll Build

A
Abdallah Mohamed
Senior Full-Stack Engineer
Creating a Serverless Function with Next.js 16.2 and Vercel

Creating a Serverless Function with Next.js 16.2 and Vercel

What You'll Build

In this tutorial, you'll create a simple serverless function using Next.js 16.2 and deploy it on Vercel. The function will handle HTTP requests and respond with JSON data. By the end of this guide, you'll have a minimalistic API endpoint that you can expand upon for various applications, such as fetching data, processing user input, or integrating with third-party services.

Here's what the final output will look like when you hit the API endpoint:

{
  "message": "Hello from your serverless function!"
}

Why This Matters

Serverless functions are an excellent choice for developers looking to build scalable and cost-effective applications without managing server infrastructure. They are particularly useful for:

  • Handling API requests: You can create lightweight endpoints for your frontend applications.
  • Performing scheduled tasks: Use serverless functions to run cron jobs without maintaining a server.
  • Integrating with third-party services: Easily connect your application to external APIs.

This approach benefits developers who want to focus on writing code without worrying about server management. It's ideal for small to medium-sized applications and can be a great starting point for learning cloud-based deployments.

Architecture Overview

When you deploy a serverless function with Next.js and Vercel, here's a simplified view of the architecture:

Client Request --> Vercel Platform --> Next.js Serverless Function --> Response
  • Client Request: Initiated by a user or another service.
  • Vercel Platform: Manages the deployment and scaling of your serverless function.
  • Next.js Serverless Function: Your code that processes the request and sends back a response.

Step-by-Step Implementation

Let's dive into the process of creating and deploying your serverless function.

1. Set Up a New Next.js Project

First, you need to set up a new Next.js project. If you haven't installed the Next.js CLI, make sure you have Node.js and npm installed, then run the following command:

npx create-next-app@16.2 my-serverless-app

This command will create a new directory called my-serverless-app with the basic structure of a Next.js application.

Explanation: The create-next-app command scaffolds a new Next.js project with all the necessary configurations. Version 16.2 ensures you're using the latest features and improvements.

2. Create a Serverless Function

Next, you'll create a serverless function within your Next.js project. Navigate to the pages/api directory and create a new file named hello.js.

Add the following code to hello.js:

// pages/api/hello.js

export default function handler(req, res) {
  res.status(200).json({ message: 'Hello from your serverless function!' });
}

Explanation: This code defines a simple serverless function that responds with a JSON object. The handler function is the default export, which Next.js recognizes as an API route. When a request is made to /api/hello, this function gets executed.

3. Test Your Serverless Function Locally

Before deploying, it's a good idea to test your serverless function locally. Start your Next.js development server with:

cd my-serverless-app
npm run dev

Once the server is running, open your browser and navigate to http://localhost:3000/api/hello. You should see the JSON response:

{
  "message": "Hello from your serverless function!"
}

Explanation: Running npm run dev starts the Next.js development server on port 3000. Visiting the /api/hello endpoint triggers the serverless function you created, returning the JSON response, which confirms that your function is working as expected.

In the next steps, you'll learn how to deploy this function to Vercel, making it accessible on the internet.

4. Deploy Your Serverless Function to Vercel

Now that your serverless function is working locally, it's time to deploy it to Vercel. First, ensure you have the Vercel CLI installed. If not, you can install it globally using npm:

npm install -g vercel

Next, log in to your Vercel account. If you don't have one, you'll need to sign up at vercel.com.

vercel login

Once logged in, deploy your project with the following command:

vercel

The command will prompt you to select your project settings. You can press Enter to choose the default options, which should suffice for this tutorial.

Explanation: The vercel command automatically detects your Next.js project and deploys it to the Vercel platform. By default, Vercel handles the configuration and scaling of serverless functions, so you don't need to manage any infrastructure.

5. Access Your Deployed Serverless Function

After deployment, Vercel will provide you with a URL for your project. You can use this URL to access your serverless function. Visit the following endpoint in your browser:

https://<your-project-name>.vercel.app/api/hello

You should see the same JSON response:

{
  "message": "Hello from your serverless function!"
}

Explanation: The endpoint is live and accessible over the internet. Vercel takes care of routing requests to your serverless function, ensuring it's always available.

Common Mistakes

  • Incorrect File Path: Ensure your serverless function is placed in the pages/api directory. Placing it elsewhere will prevent Next.js from recognizing it as an API route.
  • Missing Vercel Login: If you haven't logged into Vercel using the CLI, the deployment will fail. Use vercel login to authenticate.
  • Network Issues: Sometimes, firewalls or network restrictions can prevent the Vercel CLI from connecting to the internet. Check your network settings if you encounter connectivity issues.

How I Would Use This

When to Use

  • Prototyping and Small Applications: Ideal for quickly deploying small applications or prototypes without worrying about server management.
  • Microservices: Perfect for building microservices that require minimal maintenance and can scale automatically.
  • Event-Driven Architectures: Use serverless functions to handle events from other services or APIs.

When to Avoid

  • Long-Running Processes: Serverless functions have execution time limits. Avoid using them for tasks that require extensive processing time.
  • Stateful Applications: Since serverless functions are stateless, they are not suitable for applications that require persistent state across requests.

Production Considerations

  • Cold Starts: Be aware of cold starts, which can introduce latency when your function is invoked infrequently.
  • Cost: While the pay-as-you-go model can be cost-effective, monitor usage to avoid unexpected charges.

Lessons Learned

  • Ease of Use vs. Control: While serverless functions are easy to deploy and manage, they offer less control over the underlying infrastructure compared to traditional servers.
  • Scalability: Automatic scaling is a significant advantage, but understanding the limits of serverless functions, such as memory and execution time, is crucial.
  • Integration Complexity: Integrating with external services can introduce latency and complexity, especially if your function depends on multiple APIs.

Next Steps

  • Explore Vercel's Dashboard: Familiarize yourself with Vercel's dashboard to manage deployments, view logs, and monitor performance.
  • Add Environment Variables: Learn how to use environment variables in Vercel for secure configuration management.
  • Advanced Features: Dive deeper into Next.js features like dynamic API routes, middleware, and authentication.

Sources