DEV Community

Cover image for Day 01: Building My First FastAPI Application
Murali
Murali

Posted on

Day 01: Building My First FastAPI Application

Introduction

Every software application starts with a development environment.

As part of my journey towards becoming a Cloud + AI Application Engineer, I decided to strengthen my Python development skills and explore modern API development using FastAPI.

Although I have been working in IT for several years, my experience has primarily been with Visual Basic, C#, .NET Framework, .NET Core and AWS-hosted applications.

Now, I'm expanding my skills towards Python, cloud engineering and AI application development.

In this first lesson, I focused on setting up a Python development environment and building a simple REST API using FastAPI.

Let's explore what I learned.

1. Setting Up the Development Environment

I use the following tools for my development:

  • Operating System: Windows 11
  • Code Editor: Visual Studio Code
  • Programming Language: Python
  • Version Control: Git
  • Framework: FastAPI
  • ASGI Server: Uvicorn

Why do we need these tools?

Python

Python is the programming language used to develop our application.

Visual Studio Code

VS Code is a lightweight code editor that helps us write, organize, execute and debug application code.

Git

Git is a version control system. It helps developers track code changes and maintain different versions of their projects.

FastAPI

FastAPI is a Python framework used to develop APIs quickly and efficiently.

Uvicorn

Uvicorn is an ASGI server that runs our FastAPI application and handles incoming HTTP requests.

2. Creating a Python Virtual Environment

One important concept I learned is the Python virtual environment.

Imagine working on two different Python applications.

Application A requires one version of a library, while Application B requires another version.

Installing all dependencies globally can create conflicts between applications.

A virtual environment solves this problem by maintaining separate Python packages for each project.

I created a virtual environment using:

python -m venv .venv
Enter fullscreen mode Exit fullscreen mode

To activate it in Windows PowerShell:

.venv\Scripts\Activate.ps1
Enter fullscreen mode Exit fullscreen mode

After activation, Python packages installed in this environment are isolated from other projects.

Why is this important?

  • Avoids dependency conflicts.
  • Keeps project dependencies separate.
  • Makes application setup easier for other developers.
  • Helps maintain consistent development environments.

3. Installing FastAPI and Uvicorn

After activating the virtual environment, I installed the required packages:

python -m pip install fastapi uvicorn
Enter fullscreen mode Exit fullscreen mode

FastAPI provides the framework for developing APIs, while Uvicorn runs the application.

4. Understanding Our First FastAPI Application

I created a Python file named main.py.

The initial implementation was:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def root():
    return {"message": "Hello World"}
Enter fullscreen mode Exit fullscreen mode

Although this application contains only a few lines, each statement has an important purpose.

Understanding the code

Step 1: Import FastAPI

from fastapi import FastAPI
Enter fullscreen mode Exit fullscreen mode

This imports the FastAPI class from the FastAPI library.

Step 2: Create a FastAPI instance

app = FastAPI()
Enter fullscreen mode Exit fullscreen mode

This creates an instance of the FastAPI application.

The variable app represents our application and is used to register API endpoints.

Step 3: Define an API endpoint

@app.get("/")
Enter fullscreen mode Exit fullscreen mode

This is a decorator that registers a GET endpoint for the root URL /.

When a client sends an HTTP GET request to this URL, FastAPI executes the associated function.

Step 4: Return a response

def root():
    return {"message": "Hello World"}
Enter fullscreen mode Exit fullscreen mode

The function returns a Python dictionary.

FastAPI automatically converts this dictionary into a JSON response.

5. Running the Application Using Uvicorn

Now that the application is ready, we need a server to run it.

I executed:

python -m uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

Let's understand the command.

  • uvicorn: Starts the ASGI server.
  • main: Refers to the Python file main.py.
  • app: Refers to the FastAPI instance created inside that file.
  • --reload: Automatically reloads the application when source code changes during development.

After starting the server, I accessed:

http://127.0.0.1:8000/

The browser displayed:

{
    "message": "Hello World"
}
Enter fullscreen mode Exit fullscreen mode

It worked!

6. Exploring Automatic API Documentation

One interesting feature of FastAPI is automatic API documentation.

I opened:

http://127.0.0.1:8000/docs

FastAPI displayed an interactive Swagger UI.

Using this interface, developers can:

  • Explore available API endpoints.
  • Understand HTTP methods.
  • Execute API requests.
  • Inspect response bodies and status codes.

This is particularly useful during API development and testing.

7. Creating a Health Check Endpoint

Next, I added another endpoint to check whether the application is running properly.

@app.get("/health")
def health_check():
    return {"status": "healthy"}
Enter fullscreen mode Exit fullscreen mode

When I accessed:

http://127.0.0.1:8000/health

The application returned:

{
    "status": "healthy"
}
Enter fullscreen mode Exit fullscreen mode

Why do we need a health check endpoint?

A health check endpoint provides a simple way to verify that an application is responding.

It can be useful for:

  • Application monitoring.
  • Deployment verification.
  • Infrastructure health checks.
  • Automated monitoring systems.

However, a basic health endpoint only confirms that the endpoint responds. It does not necessarily verify database connectivity or other external dependencies.

8. Managing Project Dependencies

Another important practice is maintaining a requirements.txt file.

I generated it using:

python -m pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

This file contains the installed Python package dependencies and their versions.

Another developer can install the listed dependencies using:

python -m pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

This makes project setup easier and reduces dependency-related issues.

9. Using Git for Version Control

I also initialized Git in my project.

git init
Enter fullscreen mode Exit fullscreen mode

I created a .gitignore file to prevent unnecessary or sensitive files from being committed.

For example:

.venv/
__pycache__/
.env
*.pyc
Enter fullscreen mode Exit fullscreen mode

Why exclude .venv?

Because other developers can recreate the virtual environment using the project's dependency file.

There is no need to commit all installed packages and virtual environment files into the source code repository.

10. What I Learned Today

Although this was a simple application, I learned several fundamental concepts.

  1. How to create and activate a Python virtual environment.
  2. How to install Python dependencies.
  3. What FastAPI is and how to create an application instance.
  4. How HTTP GET endpoints work.
  5. What Uvicorn does.
  6. How automatic API documentation works.
  7. How to create a basic health check endpoint.
  8. Why requirements.txt is important.
  9. How Git and .gitignore help maintain a project.

Final Thoughts

Coming from a .NET development background, I found several concepts familiar, but implementing them using Python and FastAPI helped me understand a different approach to API development.

The most important takeaway from Day 1 is that building a software application is not just about writing code.

A good development environment, dependency management, version control and understanding the underlying framework are equally important.

This is just the beginning of my journey.

Next: Day 2 – Understanding Python application structure, data models and request validation.

CloudEngineering #Python #FastAPI #SoftwareEngineering

Top comments (0)