Deploy to Vercel
This guide takes you through the steps to set up and deploy a serverless application that uses Prisma to Vercel. The application uses Next.js to provide a serverless REST API, and Prisma Client to handle fetching, creating, and deleting records from a database.
Vercel is a cloud platform that hosts static sites and serverless functions. You can integrate a Vercel project with a GitHub repository to allow you to deploy automatically when you make new commits. Next.js is a full-stack React-based serverless framework that integrates seamlessly with Vercel.
The Next.js app has the following components:
- Backend: Next.js API routes that serve a REST API and use Prisma Client to handle database operations on a PostgreSQL database.
- Frontend: Next.js page to interact with the API.
This guide focuses on how to use Prisma with Next.js and Vercel. The starting point is the Prisma Vercel example, which has four serverless REST endpoints configured as Next.js API Routes and a Next.js page that allows you to call the endpoints and display data.
Prerequisites
To follow this tutorial, make sure that you have the following prerequisites in place:
- a GitHub account
- a hosted PostgreSQL database and a connection string for it, e.g.
postgresql://username:password@your_postgres_db.cloud.com/db_identifier
(you can use Supabase, which offers a free plan) - a Vercel account
- Node.js installed
If you add a separate database for preview deployments, you will also need:
- a second hosted PostgreSQL database and a connection string for it
Prisma workflow
At the core of Prisma is the Prisma schema – a file where you declaratively define your data model. The Prisma schema is a single source of truth for both Prisma Client and Prisma Migrate.
In this guide, you will create the database schema with Prisma Migrate. Prisma Migrate takes your changes to the data model in your Prisma schema, generates a SQL migration file with the changes and applies it to your database.
There are two main workflows for Prisma Migrate:
- You can create and apply migrations during local development with
prisma migrate dev
- You can apply the migrations to production with
prisma migrate deploy
For brevity, this guide does not cover how to create migrations with prisma migrate dev
. Instead, it focuses on the production workflow with prisma migrate deploy
and uses the Prisma schema and SQL migration that are included in the example code.
You will use Vercel's build step to run the prisma migrate deploy
command to apply the migrations before Vercel deploys the serverless functions.
To learn more about how to create migrations with Prisma Migrate, see our Get Started guide.
1. Create the project and deploy to Vercel
To begin, you will deploy the deployment-example-vercel
example repository to Vercel. As part of the deployment steps, you will clone the GitHub repository and connect your application to your PostgreSQL database.
Steps
Go to
deployment-example-vercel
and click the Deploy button.The Deploy Button is a Vercel feature that allows you to deploy a project and clone the source Git repository to your own repository.
On the Create Git Repository screen, select GitHub.
Pick a repository name. This will be the name of the cloned repository in your GitHub account. In this guide, we use the name
prisma-vercel-example
:If this is your first time using Vercel, you will be prompted to install the Vercel app in your GitHub account.
Click Create.
On the Configure Project screen, set the
DATABASE_URL
environment variable to the connection string for your PostgreSQL database:The database connection string has the following format:
postgresql://__USER__:__PASSWORD__@__HOST__/__DATABASE__For example, on Supabase it looks similar to:
postgresql://janedoe:randompassword@db.projectcode.supabase.co:5432/postgresClick Deploy.
Vercel will now deploy your application. The
package.json
file in the root directory of the example project uses avercel-build
script to runprisma generate && prisma migrate deploy && next build
. This generates Prisma Client, applies the migrations, and then builds the Next.js project. This ensures that the generated Prisma Client innode_modules/@prisma/client
is available to the Next.js serverless functions.
Result
When the build completes, you should see the following:
You have successfully deployed the app to Vercel. Note that Vercel will build and deploy further commits to your GitHub repository.
To check that the API returns data successfully, click on the image of the deployed application. This will take you to your application at https://__PROJECT-NAME__.vercel.app/
, where __PROJECT-NAME__
is the name that Vercel generates for the project.
When the page loads, click Check API status. If successful, you will see a result similar to the image below.
{"up":true}
2. Test your deployed application
You can now test your application. Open your application at https://__PROJECT_NAME__.vercel.app
to interact with the API.
Steps
Click Load users with profiles.
You do not yet have any data in the database, so the application returns an empty array:
[]This API call uses Prisma to get all
users
in the database with their relatedprofiles
. The code is in thepages/api/users.js
file in the project repository.Click Seed data.
This API call uses Prisma to delete any existing database records and load the database with test
users
,profiles
, andposts
, then returns the createdusers
. The code is in thepages/api/seed.js
file.Click Load users with profiles again.
This now returns the
users
andprofiles
that you added in the previous step:[{"id": 5,"email": "jane@prisma.io","name": "Jane","profiles": [{"id": 13,"bio": "Technical Writer","userId": 5},{"id": 14,"bio": "Health Enthusiast","userId": 5},{"id": 15,"bio": "Self Quantifier","userId": 5}]},{"id": 6,"email": "toru@prisma.io","name": "Toru Takemitsu","profiles": [{"id": 16,"bio": "Composer","userId": 6},{"id": 17,"bio": "Musician","userId": 6},{"id": 18,"bio": "Writer","userId": 6}]}]Click Load posts.
This returns the following output:
[{"id": 11,"content": "https://www.prisma.io/blog/comparison-of-database-models-1iz9u29nwn37/","title": "Comparing Database Types: How Database Types Evolved to Meet Different Needs","authorId": 5,"author": {"id": 5,"email": "jane@prisma.io","name": "Jane"}},{"id": 12,"content": "https://quantifiedself.com/get-started/","title": "Analysing Sleep Patterns: The Quantified Self","authorId": 5,"author": {"id": 5,"email": "jane@prisma.io","name": "Jane"}},{"id": 13,"content": "","title": "Requiem for String Orchestra","authorId": 6,"author": {"id": 6,"email": "toru@prisma.io","name": "Toru Takemitsu"}},{"id": 14,"content": "","title": "Music of Tree","authorId": 6,"author": {"id": 6,"email": "toru@prisma.io","name": "Toru Takemitsu"}},{"id": 15,"content": "","title": "Waves for clarinet, horn, two trombones and bass drum ","authorId": 6,"author": {"id": 6,"email": "toru@prisma.io","name": "Toru Takemitsu"}}]This API call uses Prisma to get all
posts
in the database with their relatedauthors
. The code is in thepages/api/posts.js
file.
Result
You have now tested the API for your application and added data to your database.
3. (Optional) Add a separate database for preview deployments
Your application currently has a single production environment, associated with the main
Git branch of your repository. If you open a pull request to change your application, Vercel creates a new preview environment.
By default, Vercel uses the DATABASE_URL
environment variable that you defined when you imported the project for both the production and preview environments. This causes problems if you create a pull request with a database schema migration, because the pull request will change the schema of the production database.
In this section you associate a second PostgreSQL database with a Vercel preview environment.
Steps
Click the Settings tab of your Vercel project.
Click Environment variables.
Add an environment variable with a key of
DATABASE_URL
and select only the Preview environment option:Set the value to the connection string of your second PostgreSQL database. As before, the connection string has the following format:
postgresql://__USER__:__PASSWORD__@__HOST__/__DATABASE__Click Save.
Result
You should see the DATABASE_URL
environment variable for the preview environment in your list of environment variables:
Summary
You have successfully deployed your application to Vercel.
For more insight into the Prisma Client API, look at the function handlers in the api/
directory of the example repository.
A note on connection pooling
Generally, when you access a database with serverless functions, we recommend you use a connection pooler for scalability because every function invocation might result in a new connection to the database. This is not a problem with a constantly running Node.js server.
Therefore, it is beneficial to pool DB connections to get better performance.
You can use the Data Proxy for connection pooling, to reduce your Prisma Client bundle size, and to avoid cold starts.
For more information on other solutions, see our connection management guide for serverless environments.