How to use build output api on Vercel
The Vercel Build Output API allows you to define custom build outputs through a standardized directory structure and configuration files. You create a .vercel/output directory with static files, serverless functions, and configuration to control how Vercel serves your application.
Prerequisites
- Basic knowledge of Node.js and deployment concepts
- Vercel account and CLI installed
- Understanding of build processes and static file generation
- Familiarity with JSON configuration files
Step-by-Step Instructions
Create the Build Output Directory Structure
.vercel/output directory in your project root. Inside this directory, create three subdirectories: static for static assets, functions for serverless functions, and config.json for routing configuration.mkdir -p .vercel/output/static
mkdir -p .vercel/output/functions
touch .vercel/output/config.jsonConfigure the Routes in config.json
config.json file to define routing rules. Specify version as 3, and define routes array with patterns and destinations:{
"version": 3,
"routes": [
{ "src": "/api/(.*)", "dest": "/api/$1" },
{ "src": "/(.*)", "dest": "/static/$1" }
]
}Add Static Files to the Static Directory
.vercel/output/static directory. These files will be served directly by Vercel's CDN:cp -r dist/* .vercel/output/static/
# or
cp index.html .vercel/output/static/
cp -r assets/ .vercel/output/static/Create Serverless Functions
.vercel/output/functions. Each function needs an index.js file and a .vc-config.json configuration:mkdir .vercel/output/functions/api
echo '{ "runtime": "nodejs18.x" }' > .vercel/output/functions/api/.vc-config.jsonCreate your function in
.vercel/output/functions/api/index.js.Write Function Code
index.js file, export a default function that handles the request and response:export default function handler(request, response) {
response.status(200).json({
message: 'Hello from Build Output API',
method: request.method
});
}Set Build Command to Generate Output
vercel.json or package.json build script to generate the .vercel/output structure. Set buildCommand to null if using custom build:{
"buildCommand": null,
"outputDirectory": ".vercel/output"
}Deploy Using Vercel CLI
.vercel/output directory and use the Build Output API:vercel deploy
# or for production
vercel deploy --prodMonitor the deployment logs in your Vercel Dashboard under the Deployments tab.
Common Issues & Troubleshooting
Functions not found or 404 errors
Verify that each function directory contains both index.js and .vc-config.json files. Check that your routes in config.json correctly map to function paths.
Static files not serving correctly
Ensure static files are placed in .vercel/output/static directory and routes in config.json include patterns to serve static content. Check file permissions and paths.
Build output not recognized by Vercel
Confirm the directory structure follows .vercel/output/ exactly. Verify config.json has version: 3 and valid JSON syntax. Check that outputDirectory is set correctly.
Function runtime errors
Check the .vc-config.json specifies the correct runtime version (e.g., nodejs18.x). Verify function exports use correct syntax and handle request/response properly.