Hello! Welcome to the course.
In the previous module, "API Fundamentals," we explored what makes a good API from the outside. You learned how to interact with endpoints using tools like curl and Postman, and how to recognize the design patterns of a well-structured RESTful API. The key takeaway was that great APIs are predictable and consistent, following conventions for resource naming, HTTP methods, and status codes.
Now, we shift our perspective from consuming APIs to building them. This marks the beginning of our second module, FastAPI First Steps. It's time to roll up our sleeves and write some code.
Your goal for this first lesson is to install FastAPI and Uvicorn and set up a basic project structure. We will lay the foundational groundwork for our entire course, ensuring you have a clean, ready-to-use development environment. By the end of this lesson, you'll have a running, albeit minimal, FastAPI application.
1. The Tools of the Trade: FastAPI and Uvicorn
Before we install anything, let's clarify the roles of our two key components:
-
FastAPI: This is the web framework itself. Given your proficiency in Python, you'll appreciate that it's built on modern Python features like type hints and
async/await. It's designed for high performance and rapid development, making it an excellent choice for serving ML models and building general-purpose APIs. Think of FastAPI as providing the structure and rules for building your API endpoints. -
Uvicorn: This is an ASGI server. ASGI (Asynchronous Server Gateway Interface) is a standard interface between web servers and Python applications. In simple terms, while FastAPI defines what your API does, Uvicorn is the server that actually runs it and makes it accessible over the network. It's the engine that brings your FastAPI application to life.
2. Setting Up Your Development Environment
Our first practical step is to create a dedicated, isolated environment for our project. This is a crucial best practice in Python development.
Step 1: Create a Project Directory and Virtual Environment
First, create a new folder for your project. You can name it whatever you like, such as fastapi_project. Open your terminal, navigate into this new directory, and create a Python virtual environment.
# For macOS/Linux
python3 -m venv venv
source venv/bin/activate
# For Windows
python -m venv venv
.\venv\Scripts\activate
You should now see (venv) at the beginning of your terminal prompt, indicating that your virtual environment is active.
Step 2: Install FastAPI and Uvicorn
The recommended way to install FastAPI is with its [standard] extras. This single command installs fastapi itself, uvicorn for serving the application, and other useful development tools.
The following video provides a clear walkthrough of this installation process.
Python FastAPI Tutorial (Part 1): Getting Started - Web App + REST API
Watch this segment from Corey Schafer's highly-regarded FastAPI tutorial. It demonstrates how to create a project and install FastAPI with its standard dependencies.
Watch from 05:21 to 07:23. The video uses a tool called uv, but the pip command shown (pip install "fastapi[standard]") is exactly what you need. Focus on understanding what this command installs.
After watching, run the following command in your activated virtual environment:
pip install "fastapi[standard]"
This single installation gives you everything you need to get started.
3. Creating the Basic Project Structure
With the tools installed, we can create our first application file. For now, our project structure will be very simple: just a single file in our project directory.
Step 1: Create main.py
In your project folder, create a new file named main.py. This is the conventional name for the main entry point of a FastAPI application.
Step 2: Write the Minimal Application Code
Open main.py and add the following two lines:
from fastapi import FastAPI
app = FastAPI()
Let's break this down:
- We import the
FastAPIclass from thefastapilibrary. - We create an instance of this class, which we call
app. Thisappobject is the core of your API; you'll use it to define all of your API endpoints.
This simple structure—a project directory containing a main.py file—is the starting point for any FastAPI project.
For a more detailed look at a common, more scalable project structure that you will eventually grow into, the following resource is useful.
FastAPI Tutorial: A Complete Guide for Beginners
While we are starting with a single file, real-world applications require more organization. This article from David Muraya shows a typical project layout that separates concerns into different directories.
Read the section titled 'Structuring Your FastAPI Project'. You don't need to implement this now, but pay attention to the directory layout with app/ and app/routers/. This gives you a preview of how we will organize larger projects later in the course.
4. Verifying Your Setup
An installation isn't complete until we've verified it works. Let's add a temporary endpoint to our main.py file and run the server to confirm everything is set up correctly.
Step 1: Add a "Hello World" Endpoint
Modify your main.py file to look like this. Since you are familiar with decorators in Python, you'll recognize the @app.get("/") syntax immediately.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
Here, @app.get("/") is a decorator that tells FastAPI that the function read_root() is responsible for handling GET requests to the root URL (/). The function returns a Python dictionary, which FastAPI will automatically convert into a JSON response.
Step 2: Run the Development Server
Now, go back to your terminal (make sure you're in the project directory with your main.py file) and run the following command:
uvicorn main:app --reload
Let's dissect this command:
uvicorn: The command to run the Uvicorn server.main:app: This tells Uvicorn where to find the FastAPI application instance. It means: "look inside the filemain.pyand find the object namedapp".--reload: This is an extremely useful flag for development. It tells Uvicorn to automatically restart the server whenever you save a change in your code.
After running the command, you should see output in your terminal indicating that the server is running, similar to the image below.

Now, open your web browser and navigate to http://127.0.0.1:8000. You should see the JSON response: {"Hello":"World"}.
Congratulations, you have a live, running API!
Note: The fastapi[standard] installation also includes a new command-line tool. An alternative way to run the server in development is fastapi dev main.py. This command is specifically designed for development and achieves the same result as uvicorn main:app --reload.
Conclusion
In this lesson, you took the first crucial step from theory to practice. You've successfully set up a complete development environment for building APIs with FastAPI.
Here are the key takeaways:
- FastAPI is the framework for defining your API, and Uvicorn is the ASGI server that runs it.
- The recommended installation is
pip install "fastapi[standard]"within a virtual environment. - A basic project starts with a single
main.pyfile where you create yourFastAPIapplication instance (app = FastAPI()). - You can run the development server with
uvicorn main:app --reloadto get automatic code reloading.
You've laid the foundation. Now we're ready to start building on top of it.
In the next lesson, we will take a closer look at the code we just wrote. We'll properly break down how to create endpoints with different paths and HTTP methods, and you'll discover one of FastAPI's most powerful features: the automatic interactive API documentation.