Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Python Web APIs with FastAPI

You're reading from   Building Python Web APIs with FastAPI A fast-paced guide to building high-performance, robust web APIs with very little boilerplate code

Arrow left icon
Product type Paperback
Published in Jul 2022
Publisher Packt
ISBN-13 9781801076630
Length 216 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Abdulazeez Abdulazeez
Author Profile Icon Abdulazeez
Abdulazeez
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Part 1: An Introduction to FastAPI
2. Chapter 1: Getting Started with FastAPI FREE CHAPTER 3. Chapter 2: Routing in FastAPI 4. Chapter 3: Response Models and Error Handling 5. Chapter 4: Templating in FastAPI 6. Part 2: Building and Securing FastAPI Applications
7. Chapter 5: Structuring FastAPI Applications 8. Chapter 6: Connecting to a Database 9. Chapter 7: Securing FastAPI Applications 10. Part 3: Testing And Deploying FastAPI Applications
11. Chapter 8: Testing FastAPI Applications 12. Chapter 9: Deploying FastAPI Applications 13. Other Books You May Enjoy

Building a simple FastAPI application

Finally, we can now get to our first FastAPI project. Our aim in this section is to introduce FastAPI by building a simple application. We shall cover in-depth operations in subsequent chapters.

We’ll begin by installing the dependencies required for our application in the todos folder we created earlier. The dependencies are the following:

  • fastapi: The framework on which we’ll build our application.
  • uvicorn: An Asynchronous Server Gateway Interface module to run our application.

First, activate your development environment by running the following command in your project directory:

$ source venv/bin/activate

Then, install the dependencies as follows:

(venv)$ pip install fastapi uvicorn

For now, we’ll create a new api.py file and create a new instance of FastAPI as follows:

from fastapi import FastAPI
app = FastAPI()

By instantiating FastAPI in the app variable, we can proceed to create routes. Let’s create a welcome route.

A route is created by first defining a decorator to indicate the type of operation, followed by a function containing the operation to be carried out when this route is invoked. In the following example, we’ll create a "/" route that only accepts GET requests and returns a welcome message when visited:

@app.get("/")
async def welcome() -> dict:
    return { "message": "Hello World"}

The next step is to start our application using uvicorn. In your terminal, run the following command:

(venv)$ uvicorn api:app --port 8000 --reload

In the preceding command, uvicorn takes the following arguments:

  • file:instance: The file containing the instance of FastAPI and the name variable holding the FastAPI instance.
  • --port PORT: The port the application will be served on.
  • --reload: An optional argument included to restart the application on every file change.

The command returns the following output:

(venv) ➜  todos uvicorn api:app --port 8080 --reload
INFO:     Will watch for changes in these directories: ['/Users/youngestdev/Documents/todos']
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
INFO:     Started reloader process [3982] using statreload
INFO:     Started server process [3984]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

The next step is to test the application by sending a GET request to the API. In a new terminal, send a GET request using curl as follows:

$ curl http://0.0.0.0:8080/

The response from the application logged in your console will be the following:

{"message":"Hello World"}
You have been reading a chapter from
Building Python Web APIs with FastAPI
Published in: Jul 2022
Publisher: Packt
ISBN-13: 9781801076630
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image