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
andJSON 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
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
Create/activate a Virtual Environment for Windows Systems
$ virtualenv env
$ .\env\Scripts\activate
Install FastAPI
$ pip install fastapi
$ pip install uvicorn
Create main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello FastAPI"}
Save the file and start the project using uvicorn
$ uvicorn main:app --reload
$
$ The project is LIVE - https://localhost:8000
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
Thanks for reading! For more resources, please access:
- FastAPI - official website
- Learn FastAPI by Coding - open-source sample project