FastAPI - Getting Started

Hello! This article is a short introduction to FastAPI, a high-performance Python framework used to prototype fast production-ready APIs. Thanks for reading!


✨ Introduction

As posted on the official website, FastAPI key features are listed below:

  • Very high performance - thanks to Starlette and Pydantic
  • Fast-to-code and intuitive
  • Robust - provides interactive documentation out-of-the-box
  • Standards-based - fully compatible with OpenAPI and JSON Schema

In other words, FastAPI helps us to code faster and easier a production-ready API with autogenerated docs.


✨ Simple FastAPI Project

FastAPI requires Python 3.6 (or above) to execute successfully. For most of the Unix environments, Python3 might be already installed. To check the version, open a terminal and type:

$ python3 --version
FastAPI - Check Python Version

If the version is 3.6 or above we can move forward and create a virtual environment for our first project powered by FastAPI.

Create/activate a Virtual Environment (Unix systems)
$ virtualenv env
$ source env/bin/activate
FastAPI - Create VENV on Unix
Create/activate a Virtual Environment for Windows Systems
$ virtualenv env
$ .\env\Scripts\activate
FastAPI - Create VENV on Windows
Install FastAPI
$ pip install fastapi
$ pip install uvicorn
FastAPI - Installation
Create main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello FastAPI"}
FastAPI - First API

Save the file and start the project using uvicorn

$ uvicorn main:app --reload
$ 
$ The project is LIVE - https://localhost:8000 
FastAPI - Start with Uvicorn Server

At this point we should be able to visit the API in the browser:

  • API Root: http://localhost:8000
  • OpenAPI Specs: http://localhost:8000/docs
  • Redoc:  http://localhost:8000/redoc
FastAPI - Interactive Browser Console. 

Thanks for reading! For more resources, please access: