V Vercel

How to set up serverless functions on Vercel

beginner 20 min read Updated 2026-03-13
Quick Answer

Create an api folder in your project root, add a function file like api/hello.js with a default handler that accepts request and response objects, then run vercel login followed by vercel to deploy automatically. Your function will be live at your-project.vercel.app/api/hello with automatic scaling and zero server management.

Prerequisites

  • Vercel account (free tier available)
  • Node.js v18+ installed locally
  • Vercel CLI installed via npm i -g vercel
  • Basic knowledge of JavaScript/Node.js
  • Git repository for your project (recommended)

Step-by-Step Instructions

1

Install Vercel CLI and create project directory

Install the Vercel CLI globally on your machine using npm. Open your terminal and run npm i -g vercel. Once installed, create a new project directory for your serverless functions by running mkdir my-serverless-app && cd my-serverless-app. Initialize a package.json file in this directory with npm init -y. This sets up the foundation for your Vercel project and ensures the CLI is available system-wide for deployment commands.
Use a descriptive project name that reflects your application's purpose, as this will be part of your deployed URL.
2

Create the api directory and function file

In your project root, create a folder named api (this exact name is required for Vercel to auto-detect serverless functions). Inside the api folder, create a file named hello.js. Add the following default handler code to this file:
export default function handler(request, response) {
  response.status(200).json({ message: 'Hello World' });
}
This function receives HTTP request and response objects, validates the request, and returns a JSON response. The export default pattern is the standard for Vercel serverless functions.
You can use .ts for TypeScript files instead of .js—Vercel automatically detects and compiles both.
3

Configure function settings with vercel.json (optional)

For advanced configurations, create a vercel.json file in your project root. This allows you to customize memory allocation, execution timeout, and other function-specific settings. Example configuration:
{
  "functions": {
    "api/hello.js": {
      "maxDuration": 10,
      "memory": 128
    }
  }
}
The maxDuration sets the maximum execution time in seconds (10s is the default for Hobby plans), and memory specifies RAM allocation in MB. If you skip this step, Vercel uses sensible defaults automatically.
Start with default settings and only customize vercel.json if you need specific performance tuning.
4

Authenticate with Vercel CLI

Open your terminal in the project directory and run vercel login. This command opens your default web browser automatically and prompts you to authorize the CLI with your Vercel account. Follow the browser prompts to complete authentication. Once authorized, the CLI stores your credentials locally, allowing subsequent deployments without re-authentication. If you encounter issues, you can run vercel logout first to clear cached credentials, then retry the login process.
Keep your browser window open during login—the CLI waits for browser confirmation before proceeding.
5

Deploy your project via CLI

In your project directory, run the command vercel. The CLI will prompt you with several questions about your project setup. For each prompt, you can press Enter to accept the default values: confirm setup and deployment (Y), select your account scope (default), decline linking to an existing project (N for new projects), accept the auto-generated project name, confirm the current directory (.), and decline overriding settings (N). The deployment process will build your project, detect the api folder, and provide you with a live URL such as https://my-serverless-app.vercel.app. Save this URL—it's your deployed application's address.
For subsequent deployments, use vercel --prod to deploy directly to production, or just vercel to create a preview deployment.
6

Test your deployed function

Once deployment completes, test your function by visiting the URL provided by the CLI, appending /api/hello to the end. For example, navigate to https://my-serverless-app.vercel.app/api/hello in your browser. You should see the JSON response: {"message":"Hello World"}. If you receive a 404 error, verify that you're accessing the correct path with /api/ included—accessing the root URL without the api path will not route to your function.
Bookmark this URL for quick testing during development and share it with team members to verify the deployment.
7

Configure function settings in the Vercel dashboard

Log in to your Vercel account at vercel.com/dashboard. Select your newly deployed project from the project list. In the left sidebar, navigate to Settings, then select Functions from the menu. Here you can configure regional deployment (choose a region like iad1 for US East, or leave on auto for automatic selection), adjust maximum duration limits based on your plan, and view other function-specific settings. The dashboard provides a visual interface for managing these settings without editing vercel.json, making it accessible for team members who prefer GUI-based configuration.
Different Vercel plans support different maximum durations—check your plan details if you need execution times longer than 10 seconds.
8

Set up local development environment

To test your functions locally before deployment, run vercel dev in your project directory. This command starts a local development server that mimics Vercel's production environment, typically running on http://localhost:3000. You can then test your function at http://localhost:3000/api/hello in your browser. Any changes you make to files in the api folder are automatically reflected in the local server without requiring a restart. This local development workflow significantly speeds up iteration and debugging before pushing changes to production.
Use vercel dev to catch errors early—local testing prevents failed deployments and reduces debugging time.
9

Monitor logs and troubleshoot issues

After deployment, you can view function execution logs in the Vercel dashboard. Navigate to your project, click on the Functions tab, select your specific function (e.g., hello), and view the Logs section. These logs display execution details, errors, and performance metrics for each function invocation. If your function encounters errors, the logs will show the error message and stack trace, helping you identify and fix issues. You can also use these logs to monitor function performance and identify optimization opportunities.
Check logs immediately after deployment if your function doesn't behave as expected—logs often reveal configuration or code issues.

Common Issues & Troubleshooting

404 error when visiting the deployed URL

This occurs when accessing the root URL (e.g., yourapp.vercel.app) instead of the function path. Vercel serverless functions are always served under the /api/ path by default. Ensure you're accessing the complete URL with /api/hello appended, such as https://my-serverless-app.vercel.app/api/hello. The root URL may display a default Vercel page or 404 error because no function is mapped to it.

CLI authentication failure with 'Vercel CLI not logged in' error

Run vercel logout to clear any cached credentials, then run vercel login again. Ensure your browser popup isn't blocked—the login process requires browser interaction. If the browser doesn't open automatically, manually visit the URL shown in the terminal. After successful browser authentication, return to the terminal and the CLI should recognize your login. If issues persist, try clearing your npm cache with npm cache clean --force and reinstalling the Vercel CLI.

Build failure with 'Function not found' or 'No output directory named api'

Verify that the api folder exists at your project root (not nested inside another directory) and contains your function files. Ensure your function file exports a default handler using either ES6 export syntax (export default function handler) or CommonJS (module.exports = handler). Check that the file extension is .js or .ts. Run vercel dev locally to test before redeploying—this catches configuration errors before they reach production.

Function timeout or 'maxDuration exceeded' error

Your function is taking longer than the configured maximum duration to execute. Check your Vercel plan's maximum allowed duration (Hobby plans default to 10 seconds). If your function legitimately needs more time, upgrade your Vercel plan to Pro or higher, which supports longer execution times. Alternatively, optimize your function code to reduce execution time by removing unnecessary operations, caching results, or breaking complex logic into smaller functions.

Changes to function code not reflected after deployment

Ensure you've run vercel --prod to deploy to production (not just vercel, which creates a preview). Clear your browser cache (Ctrl+Shift+Delete or Cmd+Shift+Delete) to ensure you're seeing the latest version. Check the Vercel dashboard to confirm the deployment completed successfully—look for a green checkmark next to your deployment. If using vercel dev locally, restart the development server with Ctrl+C followed by vercel dev again to pick up code changes.