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 Vanilla Python API on Vercel — Free of Cost

--

Simple Guide on Deploying Vanilla Python API on Vercel — Free of Cost
Simple Guide on Deploying Vanilla Python API on Vercel — Free of Cost

Whether you are working on your hobby project or a professional project, deployment is necessary. 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 VS Code.

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 python-api-example
cd ./python-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) 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 http.server import BaseHTTPRequestHandler
import json


# Create a custom HTTP request handler
class handler(BaseHTTPRequestHandler):
# Define how to handle GET requests
def do_GET(self):
print("Here => ", self.path)
if self.path == "/api":
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(
"Python API Example - Welcome to the homepage!".encode("utf-8")
)
else:
self.send_response(404)
self.send_header("Content-type", "application/json")
self.end_headers()
error_data = {"message": "Not Found", "status": "error"}
json_error_data = json.dumps(error_data)
self.wfile.write(json_error_data.encode("utf-8"))

In this, you don't need to include the server creation code as Vercel will automatically create one for us.

3) 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.

# Byte-compiled / optimized files
__pycache__/
*.py[cod]

# Virtual environment
venv/
env/
.venv/
ENV/

# IDE files
.idea/
.vscode/

# Compiled Python files
*.pyc

# Logs
*.log

# OS or editor-generated files
.DS_Store
Thumbs.db

4) Configure the vercel.json file:

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

{
"rewrites": [{ "source": "/(.*)", "destination": "/api" }]
}

You can replace the "dest": "/api" with the folder name you choose to place index.py in!

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 32.6.1
? Set up and deploy “D:\example\python-api-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? python-api-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://vanilla-python-api-vercel-deployment-example.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