Starting with FastAPI: Creating a Super Simple CRUD Exercise
Tags: fastapi, python, crud
Published: 2022-01-21 16:47:04

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 **C**reate, **R**ead, **U**pdate, and **D**elete.

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](https://github.com/snlucas/fapi-exercises).

First, activate a virtual environment.
```sh
$ python3 -m venv .venv
$ . .venv/bin/activate
```

Then, upgrade pip and install FastAPI, Pytest, Requests and Uvicorn.
```sh
$ pip install -U pip fastapi pytest uvicorn requests
```

## Creating the API
In the [app.py](http://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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyi02voxwojydbdgp0ig.png)

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.

### **C**reate
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3lyl8wqc2y7mp90rrlu.png)

### **R**ead
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d42s57eng40j3vbqhwfe.png)

### **U**pdate
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdq0jdig9lrskj5talx4.png)

### **D**elete
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6k818emv82tzkkddnhf.png)

## 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.
```sh
$ uvicorn app.exercise01.app:app --reload
```

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.