Chapter 1: Getting Started with FastAPI
FastAPI is the Python web framework that we are going to use in this book. It is a fast, lightweight modern API and has an easier learning curve when compared to other Python-based web frameworks, such as Flask and Django. FastAPI is relatively new, but it has a growing community. It is used extensively in building web APIs and in deploying machine learning models.
In the first chapter, you will learn how to set up your development environment and build your first FastAPI application. You will begin by learning the basics of Git – a version control system – to equip you with the knowledge of storing, tracking, and retrieving file changes as you build your application. You will also learn how to handle packages in Python using pip, how to create isolated development environments with Virtualenv, and the basics of Docker. Lastly, you will be introduced to the basics of FastAPI by building a simple Hello World application.
An understanding of the technologies previously mentioned is required to build a full-blown FastAPI application. It also serves as an addition to your current skillset.
At the completion of this chapter, you will be able to set up and use Git, install and manage packages using pip, create an isolated development environment with Virtualenv, use Docker, and most importantly, scaffold a FastAPI application.
This chapter covers the following topics:
- Git basics
- Creating isolated development environments with Virtualenv
- Package management with pip
- Setting up and learning the basics of Docker
- Building a simple FastAPI application
Technical Requirement
You can find the code files for this chapter on GitHub at https://github.com/PacktPublishing/Building-Python-Web-APIs-with-FastAPI/tree/main/ch01
Git basics
Git is a version control system that enables developers to record, keep track, and revert to earlier versions of files. It is a decentralized and lightweight tool that can be installed on any operating system.
You will be learning how to use Git for record-keeping purposes. As each layer of the application is being built, changes will be made, and it’s important that these changes are kept note of.
Installing Git
To install Git, visit the downloads page at https://git-scm.com/downloads and select a download option for your current operating system. You’ll be redirected to an instructional page on how to install Git on your machine.
It is also worth noting that Git comes as a CLI and a GUI application. Therefore, you can download the one that works best for you.
Git operations
As mentioned earlier, Git can be used to record, track, and revert to earlier versions of a file. However, only the basic operations of Git will be used in this book and will be introduced in this section.
In order for Git to run properly, folders housing files must be initialized. Initializing folders enables Git to keep track of the content except otherwise exempted.
To initialize a new Git repository in your project, you need to run the following command in your terminal:
To enable tracking of files, a file must first be added and committed. A Git commit enables you to track file changes between timeframes; for example, a commit made an hour ago and the current file version.
What Is a Commit?
A commit is a unique capture of a file or folder status at a particular time, and it is identified by a unique code.
Now that we know what a commit is, we can go ahead and commit a file as follows:
You can track the status of your files after making changes by running the following command:
Your terminal should look similar to the following:

Figure 1.1 – Git commands
To view the changes made to the file, which can be additions or subtractions from the file contents, run the following command:
Your terminal should look similar to the following:

Figure 1.2 – Output from the git diff command
It is good practice to include a .gitignore
file in every folder. The .gitignore
file contains the names of files and folders to be ignored by Git. This way, you can add and commit all the files in your folder without the fear of committing files like .env
.
To include a .gitignore
file, run the following command in your terminal:
To exempt a file from being tracked by Git, add it to the .gitignore
file as follows:
Common files contained in a .gitignore
file include the following:
- Environment files (*
.env
) - Virtualenv folder (env, venv)
- IDE metadata folders (such as
.vscode
and.idea
)
Git branches
Branches are an important feature that enables developers to easily work on different application features, bugs, and so on, separately before merging into the main branch. The system of branching is employed in both small-scale and large-scale applications and promotes the culture of previewing and collaborations via pull requests. The primary branch is called the main branch and it is the branch from which other branches are created.
To create a new branch from an existing branch, we run the git checkout -b newbranch
command. Let’s create a new branch by running the following command:
The preceding command creates a new branch from the existing one, and then sets the active branch to the newly created branch. To switch back to the original main
branch, we run git checkout main
as follows:
Important Note
Running git checkout main
makes main
the active working branch, whereas git checkout -b newbranch
creates a new branch from the current working branch and sets the newly created branch as the active one.
To learn more, refer to the Git documentation: http://www.git-scm.com/doc.
Now that we have learned the basics of Git, we can now proceed to learn about how to create isolated environments with virtualenv.