Stephen Sanwo
Stephen Sanwo

Stephen Sanwo

FastAPI Boiler Plate Set up - Basic & Advanced

FastAPI Boiler Plate Set up - Basic & Advanced

Stephen Sanwo's photo
Stephen Sanwo
·Jul 1, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

This snippet shows you how to set up a basic FastAPI service. There is first a basic version with uvicorn that should be sufficient for development, and then a more production-ready version with gunicorn.

Note: this snippet assumes that you have a fresh virtual environment and have installed fastapi, uvicorn, and gunicorn as dependencies as shown below.

python3 -m venv env
source env/bin/activate
pip install  fastapi uvicorn gunicorn

Basic FastAPI setup with Uvicorn

Run programmatically with python assuming your entry point is named Going to localhost:1234/ will display "Success"

from fastapi import FastAPI
import uvicorn

api = FastAPI()

async def root():
    return 'Success'

if __name__ == "__main__":"api:api", host="", port=1234, reload=True)

FastAPI setup with workers and Gunicorn

This sets up FastAPI with Gunicorn, a more capable application server. We have defined the number of workers based on the cpu_count, and we pass our options to the BaseApplication class of gunicorn.

from fastapi import FastAPI
import uvicorn
from import BaseApplication
import multiprocessing


api = FastAPI()

def number_of_workers():
    print((multiprocessing.cpu_count() * 2) + 1)
    return (multiprocessing.cpu_count() * 2) + 1

class StandaloneApplication(BaseApplication):
    def __init__(self, app, options=None):
        self.options = options or {}
        self.application = app

    def load_config(self):
        config = {
            key: value for key, value in self.options.items()
            if key in self.cfg.settings and value is not None
        for key, value in config.items():
            self.cfg.set(key.lower(), value)

    def load(self):
        return self.application

async def root():
    return 'Success'

if __name__ == "__main__":
    if APP_ENV == "development":"api:api", host="", port=1234, reload=True)

        options = {
            "bind": "",
            "workers": number_of_workers(),
            "accesslog": "-",
            "errorlog": "-",
            "worker_class": "uvicorn.workers.UvicornWorker",

        StandaloneApplication(api, options).run()
Share this