Host Your API On Vercel

When I started my web development journey, I used to use Heroku to host my API (Application Programming Interface). Heroku has this concept of dyno where your API goes to sleep (idle) after 30 mins if there is no usage. On September 28, 2020 (my birthday), my Heroku account got suspended. So I started to look for alternatives to host my API. The very thing that came to my mind was to buy a VPS, but since I am broke I can’t afford it right now (but I had money to buy $1000 laptop).

Recently, I noticed that one can host their API using Vercel. Vercel is a cloud platform for static sites and server-less Functions. It enables developers to host websites and web services.

So, let me walk you through the guide on how you can host your API on Vercel.

  1. Create a directory called api, which will contain your API endpoints file.
My API Tree Structure
β”œβ”€β”€ api
β”‚Β Β  └── index.js
β”œβ”€β”€ db
β”‚Β Β  └── db.js
β”œβ”€β”€ models
β”‚Β Β  └── user.js
β”œβ”€β”€ package.json
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ routes
β”‚Β Β  └── user.js
└── vercel.json

  1. Create a new file called vercel.json, and insert the below code
{
  "rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
}
  1. Push your API code to a Git repository, and then deploy it using Vercel

With just three simple steps you have your API up and running on Vercel for free.

Tip: To run your API locally, run node api

This is my time writing a guide on doing something so let me how’s it in comments below or on Mastodon.