DEV Community

Cover image for My First FastAPI Experience
Juan Benitez
Juan Benitez

Posted on

My First FastAPI Experience

A few weeks ago I started my journey with FastAPI a modern, fast web framework, this is how his creator defines it.

I heard about FastAPI a long time ago when some coworker told me that this framework was built by a Colombian, I'm from Colombia as well so I was really curious about it, anyway I didn't pay too much attention to it at first because I was really focused on learning Django.
But now when I feel like I have a certain level of expertise in Django I wanted to give it a try.

Where I started?

I started at the docs as usual and I thought at first, well this is pretty much Flask but with async support. But obviously, it wasn't just that.

While I was reading the docs I was always trying to make comparisons with Django Rest Framework, even though these are completely different web frameworks, this helped me to better understand what I was reading and why I should use one over the other.

Django and FastAPI

The first comparison I made was serializers and Pydantic, these both can be used to validate the data you are passing in the request and to serialize your objects but the big difference comes when Pydantic benefits from the Python type hints, so you don't learn extra syntax, and this type hints fit perfectly with the IDEs, so you have autocompletion and all those nice features.

The second thing that I wanted to check was the views, these guys that are in charge of taking the request and returning a response. DRF provides many options when it comes to views, it has generic classes, viewsets, mixins, etc. FastApi on the other hand has something they call path operation function, this is a function that is related to a path and it's going to be executed when this path is requested. First, I was confused because it can be hard to understand how this functions works, for example, if you pass a parameter to this function this is going to be treated as a query parameter but you also define the path parameter there, and also the body is passed as a parameter, so you need to define this parameter with special types provided by FastAPI so it knows how to treat every parameter.
Please, I know this sounds confusing but when you understand it, it is pretty simple. I have to be honest and I kind of like the DRF Class views more, it just seems more organized and simple at least for me. Yet, you can organize and share code along with the application, they have something called Dependencies that is used for this, it's worth taking a look at it because it's a really powerful feature.

The last thing I want to mention and that is the async support, this is just great, it comes with the framework so we don't need to configure or install something else. And the same goes for automatic documentation, every time you create a new endpoint the docs for this endpoint are created automatically using all the different things you defined for that endpoint, query parameters, path parameters, body requests, responses, authentication, etc.

What is next?

I'm really excited about this journey, I'm already doing some projects with FastApi and I hope I can tell you soon about it.

Also, I'm contributing to translate the docs to Spanish, this has helped me a lot as well because in this way I can read really carefully all the things that the framework provides.

I hope this can help you as well, and if you have any questions please don't hesitate, and of course check FastApi, it's really powerful and nice to learn.

Top comments (0)