Lesson 1
Running Your First FastAPI Application
Running Your First FastAPI Application

Welcome to the first lesson of our FastAPI for Beginners course! We're thrilled to have you here. By the end of this lesson, you will be able to set up a FastAPI application and run it on your local server — a crucial first step in creating web APIs. So, whether you're a seasoned developer exploring new frameworks or a complete beginner embarking on your programming journey, we've got you covered. Let's dive in and start building with FastAPI! ⚡

What is FastAPI?

FastAPI is a Python web framework that allows you to build APIs quickly and efficiently. It's built on top of other robust libraries like Starlette for the web parts and Pydantic for the data handling. One major advantage of FastAPI is that it uses Python type hints. These type hints not only allow for better editor support but also provide data validation, serialization, and documentation.

Comparatively, though Flask and Django also allow for API building, FastAPI often outstrips them in terms of speed and ease of use. That's why many people, specially beginners to back-end engineering, chose to learn FastAPI to turn their project into reality.

Required Libraries

Before we dive into building your FastAPI application, let's explore the libraries we'll be using: fastapi and uvicorn.

  1. fastapi: This is the core library that provides all the functionalities for building APIs.

  2. uvicorn: This is an ASGI (Asynchronous Server Gateway Interface) server used to run your FastAPI applications. It serves as the bridge between your FastAPI app and the web.

To install these libraries, if you are working on your local machine, you can use the following pip commands:

Bash
1pip install fastapi 2pip install uvicorn

If you are using the CodeSignal environment provided for this course, there's no need to install any libraries as they are already pre-installed. You can jump straight into coding!

Setting up Your First FastAPI Application

Building a FastAPI application starts with a simple script. At its most basic, a FastAPI app involves importing the FastAPI class and creating an instance of this class. Observe the following code:

Python
1# Import FastAPI class 2from fastapi import FastAPI 3 4# Initialize a FastAPI app instance 5app = FastAPI()

In this simple script, we first import the FastAPI class from the fastapi module. Then, we create an instance of this class and assign it to a variable. While you can name this variable whatever you want, it's generally accepted to use app. That's it! You've just created your first FastAPI application.

Running the FastAPI Application

So, you've set up your FastAPI application. Now, how do you run it? After saving your script (let's call it filename.py), open your terminal and type in the following command:

Bash
1uvicorn filename:app --port 8000

Let's break down this command:

  • uvicorn: This is the command to start the Uvicorn server.
  • filename:app: This specifies what to run. filename is the name of your Python script (without the .py extension), and app is the instance of the FastAPI class in that script.
  • --port 8000: This specifies that the server should listen on port 8000. If you don't specify a port, Uvicorn will default to port 8000. A port is a communication endpoint that allows a computer to differentiate between multiple services running on the same IP address.
Accessing Your Application

Once you run this command, your terminal will display some logs indicating that the server is running. You should see a message like:

Plain text
1Uvicorn running on http://127.0.0.1:8000

Here, 127.0.0.1 refers to localhost, which is the default hostname that refers to your own computer. The :8000 signifies that the server is listening on port 8000. This means you can open a web browser and navigate to http://localhost:8000 or http://127.0.0.1:8000 to see your FastAPI application in action.

Shutting Down the Server

Once you are done working with your FastAPI application or if you want to relaunch your app, simply go back to your terminal where Uvicorn is running. Press Ctrl+C to stop the server. This will terminate the process, shutting down the server and freeing up the port for future use.

Recap and Preparations for Practice

Congratulations! You've just learned how to create and run your first FastAPI application. While this is only the beginning of your journey with FastAPI, knowing how to build and run an application is the cornerstone of all future lessons.

Next, in our exercise section, we'll put this knowledge into practice and create your first FastAPI application. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.