DEV Community

Lucas S Nogueira
Lucas S Nogueira

Posted on

Starting with FastAPI: Creating a Super Simple CRUD Exercise

Hi, folks.
This is my first post, and I'm super excited about it.

I decided to learn FastAPI this week, and the first exercise that came to my mind was "a super simple CRUD app".

It's super easy to do, and an easy way to start with FastAPI.

First of all, notice that CRUD comes from Create, Read, Update, and Delete.

Instead of a Database, I've decided to use operations with a simple Python dictionary.

Let's start.

Note. Link to the project on Github.

First, activate a virtual environment.

$ python3 -m venv .venv
$ . .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Then, upgrade pip and install FastAPI, Pytest, Requests and Uvicorn.

$ pip install -U pip fastapi pytest uvicorn requests
Enter fullscreen mode Exit fullscreen mode

Creating the API

In the app.py file (where is the API code), let's import FastAPI and its HTTPException class and status module.

Use the app as a variable to FastAPI, and create a simple dictionary to handle our data.

Notice the dictionary needs to be above the API methods (actually, above all methods that can use it).

Importing FastAPI and creating a simple Python dictionary

Now is the time to create the CRUD functions.

I decided to create a function to get all data, get data by id, delete data by id, delete all data, update, and create.

Create

Starting with the first CRUD letter. The C comes from Create.

To create a message (simple string to add as a value to our dictionary) I needed to think about my endpoint (URL). I didn't want it to need a parameter more than the message itself. So I needed something like an autoincrement key.

It's a simple task to do. We just need to concatenate the key string message_ with the number of our current index key. Our current index key is just the next key of our dictionary.
So we just need to know the last key index and then increment. To do so, get the length of the dictionary and add 1. Update the next dictionary key with the new value.

Create function

Read

To get the messages I just need to verify if there's a message (data) in the dictionary. if it doesn't have any, raise an HTTP Exception informing this.

I created two functions to handle the Read step:

Read functions

Update

Almost done here. The update function needs the id (key) of the message and some data to update. Again, we need to be sure that we have a message with the passed id.

Update function

Delete

The last step, finally.

I created two functions here. One to delete by id and another one to delete all (the Metallica way).

Delete functions

Running

To run our application just go to a terminal and type uvicorn pointing it to the file path (folder.subfolder_if_exists.file:fastapi_variable), followed of --reload.

$ uvicorn app.exercise01.app:app --reload
Enter fullscreen mode Exit fullscreen mode

Now it's time to take the tests. They're in my Github repository. Check it out.

I hope this post helps you, and if you have any questions, pls make a comment.

Top comments (1)

Collapse
 
respect17 profile image
Kudzai Murimi

l am just getting started with FASTAPI IN PYHTON.
Thanks for sharing, still waiting for more!