Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Follow publication

Simple Guide on Deploying Python FastAPI on Vercel — Free of Cost

--

Simple Guide on Deploying Python FastAPI on Vercel — Free of Cost
Simple Guide on Deploying Python FastAPI on Vercel — Free of Cost

Whether you are working on your hobby project or a professional project, deployment is necessary.

FastAPI is a modern, high-performance web framework for building APIs with Python. FastAPI, like Node.js in JavaScript, uses asynchronous programming (async/await) in Python to handle non-blocking operations. This approach allows for efficient handling of concurrent tasks by pausing and resuming execution.

Vercel provides a very simple way and interface for deploying applications with continuous deployment. One big benefit of deploying on Vercel is that it is Free!

Please note that this article assumes you have Python and any IDE installed. If not, you can follow installation guides for Python and VsCode.

1) Initialize the Project:

If you already have a project move to the next step!

In this step, we will initialize a basic project. Run the following command to create a new project and open it in the IDE.

mkdir flask-api-example
cd ./flask-api-example

# below command will open vscode in the current directory
# if you have any other IDE, feel free to use that
code .

Open the terminal in IDE and create the virtual environment. Virtual Environment is not necessary but it is a good practice:

python -m venv .venv

2) Add Required Dependencies:

Add the required dependencies of your app in the requirements.txt file as follows:

# /requirements.txt

fastapi==0.88.0
uvicorn==0.20.0
# other required dependencies

Now to install the dependencies run the following command:

pip install -r requirements.txt

3) Write the API:

Now create a directory api (you can choose any directory name) and within this directory create index.py with the following code:

# /api/index.py

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Hello World"}

4) Add a .gitignore file:

Initialize the git repository using the command:

git init

Add a .gitignore file to specify intentionally untracked files that Git should ignore.

idea/
.vscode/
__pycache__/
.tox/
.coverage
.coverage.*
htmlcov/
docs/_build/
dist/
venv/
.vercel
.venv

5) Configure the vercel.json file:

Now add a vercel.json file at the root of the app and write the following code.

{
"devCommand": "uvicorn main:app --host 0.0.0.0 --port 3000",
"builds": [
{
"src": "api/index.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "api/index.py"
}
]
}

After all the steps your folder structure will look like the following:

Folder Structure
Folder Structure

5) Deploying on Vercel:

Now you can deploy on Vercel using two ways:

a) By Using Vercel CLI:

Install Vercel CLI globally using the following command:

npm i -g vercel

To login to CLI you can run the following command:

vercel login

You will be presented with the following options and you can select one. A session in the browser will open and it will authenticate the CLI!

Options for Vercel Login
Options for Vercel Login

To run in your local environment you can use:

vercel dev

To deploy it to Vercel, use the following command:

vercel deploy

You will be presented with the options as follows, select the appropriate ones!

Vercel CLI 30.2.3
? Set up and deploy “~\example\fastapi-vercel-example”? [Y/n] y
? Which scope do you want to deploy to? example
? Link to existing project? [y/N] n
? What’s your project’s name? fastapi-vercel-example
? In which directory is your code located? ./
Local settings detected in vercel.json:
No framework detected.
? Want to modify these settings? [y/N] n

And your application is deployed!

b) Using Vercel Dashboard:

Upload your repo to GitHub or any other provider and connect the provider to your Vercel account. Now go to your Vercel Dashboard and follow along.

Click on Add New... and then select Project . After clicking you will be directed to the screen where you can import your project. Find your desired project and click import. Leave the settings as it is and click Deploy and it will take a few minutes to deploy. All the steps written are shown below:

Add New Project Page
Add New Project Page
Import your project
Import your project
Deployment Configuration Screen
Deployment Configuration Screen
Deployed Project Screen
Deployed Project Screen

And that’s it, Happy Coding! 😊

Access the Code at:

Visit the Live version at: https://fastapi-vercel-example-phi.vercel.app/

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Farasat Ali

Tech Savvy 💖 Blockchain, Web & Artificial Intelligence. Love to travel, explore culture & watch anime. Visit My Portfolio 👇 https://linktr.ee/faraasat

No responses yet

Write a response