Installation and Your First Appπ± Beginner
To start building with FastAPI, you need to install the framework itself, and a lightning-fast ASGI (Asynchronous Server Gateway Interface) web server called Uvicorn to run it.
What is Uvicorn?
FastAPI is just a web frameworkβit provides the tools to write routes. It does not actually have the ability to listen to physical internet traffic. Uvicorn is the actual server that binds to a port, listens for HTTP requests, and hands them off to your FastAPI code.
Why Do We Need Both?
This separation of concerns is standard in Python (unlike Node.js, where Express handles both). It allows you to swap out the server later for maximum performance without ever rewriting your application logic.
How to Install and Run
Inside your Python virtual environment, install the required packages:
# Install FastAPI and Uvicorn
pip install fastapi uvicornOnce you write your main.py file, you start the server from the terminal:
# uvicorn [filename]:[app_variable] --reload (auto-restarts on save)
uvicorn main:app --reloadYour API is now running on http://localhost:8000. Best of all, if you navigate to http://localhost:8000/docs, you will instantly see your fully generated, interactive API documentation!
--reload flag is incredible for local development, as it restarts the server instantly when you save a file. However, never use --reload in a production environment, as it severely degrades performance.