How to set up serverless functions on Vercel
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
Install Vercel CLI and create project directory
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.Create the api directory and function file
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.Configure function settings with vercel.json (optional)
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.Authenticate with Vercel CLI
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.Deploy your project via CLI
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.Test your deployed function
/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.Configure function settings in the Vercel dashboard
Set up local development environment
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.Monitor logs and troubleshoot 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.