Standalone API
Learn how to deploy your supastarter API separately from the frontend as a dedicated service.
This guide will show you how to deploy your supastarter API as a dedicated service. This can be useful if you want to run your API and frontend on different platforms or if you want youc API to be a "long-running" service instead of a serverless deployment. You probably want to use this if you have tasks in your backend that have a long execution duration (that would exceed the timeout limit of a serverless deployment for example).
We have a fully working example of this setup in the supastarter-repository on the api-deployment
branch.
You can either checkout this branch directly or follow the steps below to set it up yourself.
Create a dedicated app for your API
First you want to create a dedciated app inside your monorepo. This will be the app that will run the API as a node.js server.
Create the following files inside your monorepo:
Add rewrite rule to web app
The API will be running on a different URL than the app. To not have to handle cross-origin requests, we are going to proxy the API requests through the web app. To do this, you need to add a rewrite rule to your web app:
Note: We are redirecting all requests to the /api
path to the API service, except for the /api/docs-search
path. This is because the docs search is a serverless function inside our web app and we don't want to proxy it through the API service.
Now you should be able to run the API service locally, by running the known pnpm dev
command inside your monorepo. It will start both apps in development mode.
To keep the repository clean, you can also remove the /apps/web/app/api/[[...rest]]
folder, as it is no longer needed.
Deploy API service
How to deploy the API service depends on where you want to run it. You can either use a platform like Render which allows to deploy a Node.js server, or you wrap the API service in a docker container and deploy it on a platform like Fly.io.
Deploy as Node.js server
To run the API service as a Node.js server, all you need to do is to connect your repository, build the API service and start the server.
Depending on the platform the commands for this will be different. For example, if you want to deploy on Render, you can use the following commands:
Deploy as Docker container
To run the API service as a Docker container, you need to create a Dockerfile and build the container.
Add the following Dockerfile to your apps/api
folder:
To make Prisma work in the Docker container, you need to set the engine type to binary
in the schema.prisma
file:
Now you deploy the API service to your platform of choice.
Set the API url in the web app
Finally, you need to provide the API url to the web app.
You can do so by defining the NEXT_PUBLIC_API_URL
environment variable in the deployment environment of the web app.
Dending on your platform, you might have different deployments for different branches.
A common use case is to have a production
deployment for the main
branch and a staging
deployment for the staging
or dev
branch.
In this case you can define different environment variables for each deployment, to work with the correct API for each environment.