Modern APIs with FastAPI and Python Transcripts
Chapter: Welcome to the course
Lecture: Why FastAPI?
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Why is there so much excitement around FastAPI?
0:02
Well, let's go through just some of the things that make it special. FastAPI
0:06
is one of the fastest Python web frameworks available.
0:09
In fact, it's on par with the performance of things like Node.js and Go,
0:13
and that's largely because it actually runs on top of something called Starlette,
0:18
a lower level web framework, and uses Pydantic for data exchange and data validation.
0:23
So, when you think FastAPI,
0:25
you can think fast to execute, fast to run.
0:28
But that's not the only fast that matters,
0:30
is it? One of the most important ways to be fast is to create applications
0:34
fast and not spend a lot of time building them.
0:36
So it's also fast to code because FastAPI
0:40
does many of the mundane type of things that you would do in a web
0:43
application, automatically for us, and some estimates have put it the speed of using
0:49
FastAPI over things like Flask and Django at 2 to 3 times faster.
0:54
That's because we can do things like just a API method and say it
0:58
takes some kind of class, one of these Pydantic models,
1:00
and it will automatically look and make sure all the stuff that that class says it
1:04
must have is there, the types the class says it must take are either there or
1:09
automatically convertible to there, so you have a class that's a location,
1:13
it has a city and a state,
1:15
well, it better have a city and a state,
1:17
and those are gonna be strings that can be converted over and so on.
1:20
So a lot of the work around like validation, data exchange, documentation,
1:25
all of that is largely automatic,
1:27
so you're not gonna have to spend time writing that code. Also,
1:31
when you don't write code and stuff happens automatically for you,
1:34
that means you might not be writing bugs.
1:36
So there's also about 40% less chance of an error because you don't have to write
1:41
as much validation and data exchange.
1:43
Also, because of the type hints,
1:45
it deeply embraces this idea of teaching the editors what type of data is being exchanged.
1:50
You're passing rich objects to the API
1:53
methods, all the various parts of the API itself, the framework itself, have
1:57
type hints, so editors know what to do and give you automatic completion everywhere,
2:02
less time in the documentation, less time debugging.
2:05
It's also designed to be easy to use and learn.
2:07
If you know Flask, you will be pretty close to knowing FastAPI
2:10
actually, and there's not a whole lot of code you have to
2:14
write. So not too much to write,
2:15
as well as not too much to learn, less time reading docs,
2:19
also, because of the great editor support. When we create our
2:22
API's, we'll put parameters in there saying "it takes this kind of data, returns that
2:25
kind of data, and it has this type". Because of all the automatic stuff that's happening
2:29
for us, you'll see the framework is actually doing multiple things,
2:32
like ensuring the variable is there, doing automatic type conversion, and so on.
2:36
So again, less code duplication, fewer bugs.
2:39
And, it comes with a production ready web server built on Async and await, so
2:44
you get the highest performance production grade servers,
2:48
you can just drop right into production.
2:50
One of the challenges with building HTTP based API's is there's zero documentation.
2:55
What's supposed to be exchanged? Who knows?
2:57
Just go out there and read the docs and play with it.
3:00
But with FastAPI, it automatically uses what was known as "swagger",
3:03
OpenAPI, to generate documentation for all the API methods,
3:08
what data is exchanged through JSON schema, and things like that.
3:12
So it's really, really nice that we get automatic documentation by simply writing our program.
3:16
Nothing else. So, these are just some of the reasons to be excited about
3:21
FastAPI, and we're gonna explore all of them throughout this course.