Day 43-44: Beginner FastAPI Series - Part 1
John Enad
Posted on June 4, 2023
I've recently come across an amazing web framework for building APIs with Python, and I'm excited to learn it. To document this, I'm starting a new series that chronicles my adventures (and misadventures!) as I slowly get familiar with it.
FastAPI bills itself as a modern, high-performance web framework for building APIs. It compares its speed to NodeJS and Go, and touts itself as one of the fastest Python frameworks around. I poked around its documentation and looked up a examples, and liked what I saw So without a second thought, I decided to dive right in!
I took a quick pause to think about what I was going to build and I aimed for simplicity so I settled on developing a basic CRUD (Create, Read, Update, Delete) project for a Person with just four fields: id, first_name, last_name, and active.
To those who might want to follow along, here are the prerequisites:
- Python 3.11 installed.
- VSCode installed with a basic understanding of setting up a venv environment.
- A fundamental grasp of Python. Some familiarity with FastAPI would be advantageous.
- Basic understanding of HTTP and REST.
Next, I mapped out the routes for the API and here's what I came up with:
HTTP METHOD ROUTE
GET /api/persons (Retrieves all persons)
GET /api/persons/:person_id (Retrieves a single person)
POST /api/persons/:person_id (Creates a new person)
PUT /api/persons/:person_id (Updates an existing person)
DELETE /api/persons/:person_id (Deletes a single person)
I then created a new folder titled 'enad-project-1' to house the project. Launching VSCode, I initiated Git in a VSCode terminal with the command: 'git init'.
In the root folder, I set up a '.gitignore' file to prevent unnecessary files from being saved in our Git repository. It excludes the following:
.DS_Store
.idea/
__pycache__/
venv/
.env
To set up the Python Virtual Environment (venv), I used VS Code's Command Palette to run 'Python: Create Environment', selecting 'venv'. Upon opening a new terminal, I noticed that the command-line prompt now begins with '(.venv)', signifying the active virtual environment.
Now it's FastAPI time!
Installing FastAPI was quick and easy with the command:
pip install "fastapi[all]"
Next, I created a folder named 'app' and inside, I generated an empty file named '__init__.py
' which (as you may or may not know) indicates that 'app' is a Python package.
After that, I created 'main.py', which had the following code to create a FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/api/hello")
def root():
return {"message": "Hello World"}
I ran the application using the command:
uvicorn app.main:app --host localhost --port 8000 --reload'
Then, by navigating to http://localhost:8000/api/hello in a web browser, I was greeted with the ubiquitous "Hello World"!
And off we go to the races! It's the start and I'm going to continue to build on this while learning about FastAPI and Python.
Posted on June 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.