Production Ready FastAPI Application From 0 to 1 [Part-2]

Mohit
3 min readNov 4, 2022

--

In Part 1 of this series, we tried to set the basic context of FastAPI. This part will familiarize you with the fundamentals of creating a FastAPI application. We are covering below items:

  • Setting up a development environment.
  • Create a simple “Hello world” project.
  • Running this project using an async web server.
  • Visualize the beautiful inbuilt API documentation.

The Part 2 implementation is available in the GitHub repository.

Setting up a development environment

Pycharm is an excellent IDE for developing production-grade applications. You can install the community edition from https://www.jetbrains.com/pycharm/download.

Pipenv is a tool for dependency management and packaging. It automatically creates and manages a virtualenv for your projects. It adds/removes packages as well.

You can install the Pipenv from the instructions on their official website https://pypi.org/project/pipenv/.

Mac users who love brew can use the below command ❤️

brew install pipenv

Let’s Create a simple “Hello world” project.

Create directories in your preferred directory

mkdir fastapi-app-zero-to-one 
cd fastapi-app-zero-to-one

Setup the basic dependencies pipenv configuration

#This will set up the project with python 3.9 dependencies. You can choose other supported versions.
pipenv --python 3.9

Since we have completed the basic setup, open the project in Pycharm to simplify things.

Install FastAPI dependency

#activate this project's virtualenv now, run the following:
pipenv shell
#install fastapi package
pipenv install fastapi

This creates Pipfile and Pipfile.lock files in the project directory. The pipfile will maintain our project-level dependencies.

Code a simple “Hello world” project

Create a new app python package in the root directory and create a main.py file inside the app package.

Bingo !!! The above snippet is sufficient to serve the Hello World application.

Now, run this project using an async web server.

pipenv install "uvicorn[standard]"
uvicorn app.main:app --port 8000 # port is optional

You will see the below output. Hurray, our simple application is running successfully on http://127.0.0.1:8000. Open this URL in your browser and you will get a “Hello World” message.

Checkout the beautiful readymade API documentation ❤️

In next article Part 3 we have covered

  • Code setup to read environment variables.
  • Database configuration using SQLAlchemy.
  • Models & repositories creation.
  • Running Postgres using docker.

Please stay tuned and follow me for updates.

Summary

We kept things simple so that this could serve as a base for the next part, and then we could go deep there.

We would be glad to hear suggestions from you. Stay tuned for the next part of this series. Thank you for reading.

The whole project is available in the GitHub repository.

A quick link to articles from this series:

If this article is helpful, follow me to see my next articles.

--

--

Mohit

Founder @SignalsIQ.AI | Prev @Co-Founder @getphyllo.com