Introduction
As an Apiary platform founder, you're likely familiar with the pain of deploying your API to a production environment. Vercel is here to make that process smoother and more efficient. In this overview, we'll walk through the key features and concepts you need to know about deploying to Vercel from within the Apiary platform.
Preview Deployments
Preview deployments allow you to test your API in a staging environment before it goes live. This feature is especially useful for catching bugs or making last-minute changes without affecting your production site.
To enable preview deployments, navigate to your project settings in Vercel and toggle on the "Preview" option under the "Deployment" tab.
# Enable preview deployment in Vercel settings
curl -X PATCH \
https://api.vercel.com/v9/projects/<project_id>/environments \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <personal_access_token>' \
-d '{"preview": true}'
Environment Variables
Environment variables are a crucial part of deploying any API. Vercel makes it easy to manage these variables and inject them into your code at runtime.
To set environment variables in Vercel, navigate to the "Environment" tab under your project settings and add new variables as needed.
# Example .env file with environment variables
API_KEY=your_api_key_here
DATABASE_URL=your_database_url_here
You can then access these variables in your code using the process.env object.
// Accessing environment variables in Node.js
const apiKey = process.env.API_KEY;
console.log(apiKey);
Build Cache
Vercel's build cache is a powerful feature that speeds up deployment times by storing pre-built artifacts of your project. This means you can skip rebuilding your code from scratch on each deployment.
To take advantage of the build cache, make sure to configure Vercel with the correct build command and output directory in your vercel.json file.
// Example vercel.json configuration for build caching
{
"version": 2,
"builds": [
{
"src": "package.json",
"use": "@now/node"
}
]
}
Regions
Regions allow you to deploy your API to multiple geographic locations, ensuring faster load times and better performance for users worldwide.
To configure regions in Vercel, navigate to the "Deployment" tab under your project settings and select the desired region from the dropdown menu.
# Example curl command to create a new deployment with region set to 'us'
curl -X POST \
https://api.vercel.com/v9/projects/<project_id>/deployments \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <personal_access_token>' \
-d '{"region": "us"}'
Monorepo Support
Vercel natively supports monorepos, making it easy to manage multiple projects within a single repository.
To enable monorepo support in Vercel, navigate to the "Project Settings" and toggle on the "Monorepo" option under the "General" tab.
# Example monorepo configuration using npm workspaces
{
"name": "my-monorepo",
"version": "1.0.0",
"workspaces": ["components", "services"]
}
Conclusion
In this overview, we've covered the key features and concepts of deploying to Vercel from within the Apiary platform. By understanding these topics, you can streamline your deployment process, catch bugs earlier, and deliver a better experience for your users.
Related/Sources: