# Python FastAPI Tutorial (Part 1): Getting Started - Web App + REST API

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=7AMjmCTumuo
- **Дата:** 12.01.2026
- **Длительность:** 23:53
- **Просмотры:** 82,348
- **Источник:** https://ekstraktznaniy.ru/video/11690

## Описание

In this series of videos, we'll be learning how to build a full-featured web application from the ground up using the FastAPI framework in Python. We'll build both a JSON API for programmatic access and HTML pages for users to browse in the browser. Throughout the series, we'll set up a database with SQLAlchemy, create Pydantic models for data validation, and implement complete CRUD operations. We'll add user registration and login with secure password hashing and JWT tokens, handle file uploads for profile pictures, use background tasks for sending emails, and organize our code properly with routers.

In this first video, we'll keep things simple. We'll install FastAPI, create a basic application, build a couple of routes that return JSON, run the app from the command line, and explore FastAPI's automatic documentation. Then we'll add some dummy data, create an API endpoint, and preview returning HTML responses. Let's get started...

The code from this video can be found here:
https:/

## Транскрипт

### Segment 1 (00:00 - 05:00) []

Hey there. How's it going everybody? In this series of videos, we're going to be learning how to build a fullfeatured web application from the ground up using the Fast API framework in Python. So Fast API has quickly become one of the most popular Python web frameworks and for a pretty good reason. So it's fast, it's modern, it has some really nice features like automatic API documentation, built-in data validation, and some really nice async support. So first, let me show you what we're going to be building in this series of videos. and then we'll get started learning how to actually put all of this together. So, this is the application that we're going to be building here. Now, this is the front-end page and this is a front-end view for our back-end API. If I navigate to act one of the actual API routes, then you can see that returns JSON data. Now, fast API comes built in with this automatic documentation. So, this is one of the documentation routes here, and you can see that it shows us all of the API endpoints that we're going to be creating throughout this video. Now, it also has a newer automatic documentation route here, uh, and we can see that this one looks a little bit more modern here, uh, but it's basically the same thing. It's documentation for our API. Now, for anybody who's ever seen my old Flask series, you can see that this probably looks a little similar, but that Flask series did not use a back-end API like this. Uh, another change that I've put in here is that I put in a front-end uh switch between light mode and dark mode. So, you can use dark mode if you're following along and that's a little bit more comfortable on your eyes. So, almost all the functionality that we have on our front end here uses our backend API. So I can use that backend API to create new users. I can also log in if I have existing users. If I have forgotten a password then I have a reset password functionality here and that uses background task to email the user so that they can reset that. If I go ahead and log in to one of my accounts here. So I'll just put in my email and the test password that I created for that account. Once I'm logged in, then I won't save my password there. So, we can go to our user profile page. And from here, you can see that we can update a lot of our user information. Uh, we can upload a new picture if we wanted. So, let's say that I wanted a picture of my dog here instead. So, I'll upload that. And if I go to the top, we can see that picture was uploaded successfully. So, I can also create new posts here. So I can say testing and create this new post. Okay. And we're also authorized to perform certain actions. So if I go to a post that isn't mine, then we can only view it here. But if I go to a post that is mine, then we have the option to update it here. So I can say testing uh the edit and we can see that works. I can also delete the post here and we can see that works. And another thing that we have here on the homepage is if we have a lot of post then we have this dynamic pageionation down here at the bottom where we can load more posts and that will fetch more posts from our API uh in sequence. So we can go down here and just keep loading post from the API until we run out. So that is what we're going to be building here. We're going to build both a JSON REST API for programmatic access and HTML pages for users to browse within a web browser. Uh we're going to set up a database with SQL Alchemy. We're going to use SQL light at first and then transition over to Postgress. We are going to create pyantic models for data validation. We're going to implement uh complete CRUD operations for post. We're going to add user registration and login with secure password hashing and uh JWT tokens. We're going to handle file uploads, background tasks for sending emails, and organize our code properly with routers. And we're also going to cover async and await, showing you when and how to use asynchronous uh versus synchronous effectively. So by the end of the series, we should have a solid understanding of how to build a real application with fast API and we'll know the patterns and best practices that translate directly to production projects. We're also going to learn how to test this application and deploy the application. So we're going to go from the complete start to the complete finish. Now I will mention this several times throughout the series. Uh, but if you're following along and would like to download the source code of each step of the process, then I will have links to the source code of each video in the description section below so that you can download that if you'd like. So, for now, I'm just going to shut down that sample application that I had before.

### Segment 2 (05:00 - 10:00) [5:00]

And now we are going to start completely from scratch. So, let me shut that down. So, in this first video, we're going to keep things pretty simple. We're going to install fast API. We're going to create a basic application, build a couple of routes that return JSON, uh run the app from the command line, uh look at the automatic documentation, then we'll get some dummy data and create an API endpoint and also look at creating some HTML responses from that as well. So, with that said, let's go ahead and get started. So, first we want to create our new project. I'm going to call this project fast API blog. Now, in this series, I'm going to be using UV, which is a faster uh package manager that I've been using lately. If you'd like to learn more about UV, then I do have a video on that, and I'll leave a link to that video in the description section below. If you're not using UV, you can follow along perfectly fine. All you would have to do is create a new directory and then follow along using pip. But since I'm using UV, I'm going to create this directory using uvit. So I'll call this uh fast API_blog and then I will cd into that. And I'm just on my desktop right now. So this is just a uh new directory on my desktop. And now we need to install fast API. So the recommended way to install fast API is using the standard extras. Uh if you're using pip then you would do a pip install and this is going to be fast API. And whenever I said standard extras, I meant putting standard in brackets like this. Now, these quotes that I'm using around the package name, those are just recommended so that it works in all kinds of different terminals. Uh, some shells have issues with these scare square brackets without the quotes. Now, since I'm using UV instead of a pip install here, I'm going to use a UV add. So, I'll do UV add fast API standard. Go ahead and install that. And it looked like that installed successfully. So what does the fast API with the standard extras include? So it includes the fast API framework itself. Uh uicorn which is an uh ASGI server to run our applications and the fast API CLI command that we'll use to run our app. And this is what's listed on the fast API documentation. So now I'm just going to go ahead and open up this uh new directory that I've created here in VS Code. So let me open this up and go ahead and pull this over a bit. And let me go ahead and put this in Zen mode here so that uh we don't have so much stuff taking up our view. Now since I used UV innit, it already created a main. py Python file here with a simple hello world. If you're not using UV, then you would create this main. py file manually. And you might not have anything else in here. This is just stuff that uh UV uses by default. But to get started, uh all you have to do is create main. py in that directory that we created. And that's going to be completely blank for now. So I'll delete that starting code. And now let's build out a basic fast API application. So at the top here, I'm just going to say from fast API import fast API and then we can create an instance of our application just by saying app is equal to and that's going to be fast API and that is really all we need to get started here. Now for some reason here I'm getting this uh underlining here. I think it's because this doesn't know about our virtual environment. Let me select that just to get rid of those underlines there. Uh those are annoying. It couldn't resolve that import. Uh we just had the um wrong Python interpreter selected in VS Code. But like I was saying, uh this is really all we need to get started. So this app object is what we'll use to define all of our routes. If you've used Flask before, then this should look pretty familiar. So Fast API uses decorators for routes just like Flask does. So let's create our first route. And we'll create a home route that responds to get requests at the root URL. And to do that, we can use this app. get decorator. And we want this to be for the home route. So I'll just put a forward slash there. And now we want the function that we're uh decorating. I'll just call this route home. And we're just going to uh return a message here. And I will call this message and then just pass in a string of hello world. And let's go ahead and keep that spelled incorrectly. So we're decorating a function called home with this app. get decorator. And we pass in the path which

### Segment 3 (10:00 - 15:00) [10:00]

is just a forward slash for the root. And then we define our function. So you'll notice that I'm using a regular defaf here and not async defaf. Uh, like I mentioned, Fast API supports both and it handles sync functions perfectly fine. Uh, a lot of people use async right off the bat. I'm just going to stick with uh, regular synchronous functions for now just to keep things simple and then we'll have a video specifically on asynchronous. Uh, so for the return value, we're returning a dictionary here uh, with just a single value of message. Fast API will automatically convert this to JSON for us. So now let's save this and run our application. So to run this fast API application, we can use the fast API CLI command. So in the terminal here, uh if you installed this with uh pip, then you can say fast API uh dev main. py. Now, if you're using UV like I am in order to get uh UV to use that virtual environment, we can say UV run fast API dev main. So, I'll go ahead and run that. It's going to spin up our server here. We can see that it says that the startup is complete. Now, you'll notice here that I used fast API. Let me go back to this command here. So, I used fast API dev uh here. There's also fast API run. Now the difference is that the dev mode includes auto reload. So whenever we make changes to our code, the server will automatically restart. Uh it also gives us more helpful debugging output. So in production, you use fast API run, which is optimized for performance. Uh but throughout most of this series, we're going to be using this dev mode. So, just to show you what I mean here, let me go ahead and actually finish uh or fix that typo there. So, now if I save this main py with that, then we can see that it shut it down and restarted that server. So, that's what the dev mode uh gives us there. So, now let me go back to our browser here. And now I'm going to reload our localhost port 8000 here. And if I make this a little bit larger, a lot of bit larger, then we can see that we have that message hello world. And like I said, this is actually returning JSON here. If I do a pretty print, uh that's built in Chrome feature there. Uh then we can pretty print that JSON. So just with a few lines of code, we have an API endpoint uh returning JSON. So that's great. So now let me show you one of the better features of fast API uh which is the automatic API documentation. So all we've added is that one decorator but built in we can go to for slashdoccks and when we go to for/doccks uh we have this API documentation here and this is an interactive swagger UI that's automatically generated from our code. We didn't have to configure anything or write any documentation ourselves. It just works. And we can see that we get our route listed here. So if I click on this route to expand it, then we can try it out in our browser. So we can see that we have this try it out button here. If I click on that, then it will allow us to execute. Now if we were to have any parameters here, then it would let us fill those in here. Uh but we don't have any parameters. So I could just execute this now. And we can see here that it gives us all kinds of cool information here. So it gives us a curl command that it used uh to get this response. It shows us the request URL that it used and also what the response body was. Now these curl commands that it gives us, these are super useful if you want to test uh your API from the terminal. So I can copy this curl command here. If I go back to my terminal and I run this, then now we can see that we used curl to talk to that API endpoint and got that same hello world response. Um, so we're going to be using this documentation a lot uh throughout the video or throughout the series, sorry. Um, now we also have this uh redoc route here. So if I go to for slashredoc then this is just a different way of viewing the same information. Both are automatically generated from your code. Um but this one is a little bit more modern looking. Okay. So now let's add some actual data to our application that we can work with here. So I'm just going to keep that server running there. Um, let me create I have some snippets here that I would like to use for our dummy data for now. This is currently offscreen. Let me go ahead and uh grab

### Segment 4 (15:00 - 20:00) [15:00]

those and paste these in here. So what I have here for these snippets, this is just a list of dictionaries where each dictionary represents a post with an ID, an author, a title, uh a some content, and a date posted. Now again, this is temporary. We're going to replace this with a proper database later in the series. Um so now let's create an API endpoint. Uh let me put all of this here at the top of our application. now so that we have this post available in main. py and let me clear up my screen a little bit here so that it's easier for everybody to see. So now we have this list of dictionaries here. Now let's create an API endpoint uh that returns that list of dictionaries. So just like before I'm going to go ahead and create a new route here. Now this route instead of the root I'm going to set this to forward slash API slashpost and now I will call this function get_post and now let's just return that post dictionary or list of dictionaries sorry and fast API will automatically convert that list of dictionaries to a JSON array. So now if I save this, if my server is still running there, let me go back to port 8000. Okay, so our server is still running here. If I go to forward slapi slpost, then we can see that it returns that as a JSON array. So now we have a proper API endpoint that returns JSON data for programmatic access. So if I go back to the docs now and reload this. Sorry, some of these still have all of the stuff from before whenever I was uh showing out the complete application. So let me go ahead and reload all those. Okay. But now if I go back to the docs, then we can see that now we have two routes listed. So we have our root route there and we have our API post route and we can test this uh just like we did the other one. So I can say try it out execute and it returns that uh JSON array right here uh in the request body. Okay. So our API post route that returns JSON which is great for programmatic access like if a front end or another service wanted to access our data. But what if we wanted to display some HTML for humans to read in the browser? So let me show you how to do that. So let me go back to our uh code here. Now first I need to import HTML response from fast API. So I will say uh from fast API responses and we are going to import HTML response. So now I'm going to update our home route to return HTML instead of JSON. So we need to add a uh response class equal to HTML response. So I'll say response class is equal to and that's going to be HTML response and we are adding that to our decorator. And then let's return an HTML string here. So uh I'm just going to do an F string here. Let's just do an H1 tag. So I'll do an opening and closing H1 tag. And now let's just grab the title of the first post. So I'll access the first item from that post list. And now this should be a single dictionary. And now I'll grab the title from that single dictionary. So now let me save that. And now let's check that out in the browser. So if I go back to our home route and reload this then we can see that now it's returning some HTML here. Now just to make sure if I rightclick this and go to uh view page source. Oh I accidentally clicked on that actual element there. Let me go to view page source uh we can see if I make this larger uh that it is returning that H1 tag uh that we returned earlier. Okay. So now let's say that we want this same content to be available at multiple URLs. So let's say that we have this available at our home route right now. Uh but we also want all of our post um displayed in forward slashpost. Now remember our API route is actually slappi slpost. But let's say that we want the HTML version to just be forward

### Segment 5 (20:00 - 23:00) [20:00]

slashpost. Uh so to do that let's go back to our code here. So basically what I was saying is that I want our root and I want forward slashpost both to show the same page. And in fast API we can do this by stacking decorators on the same function. So all I have to do here is add another decorator here. And I can just call this one forward slashpost. So now both our root and forward slashpost will execute that same function. Uh so now if I save that if I go back to our browser here and go to for slpost then we can see that we get that same page. And also if I reload our docs here uh then we can see that now we're getting three routes here. But here's the thing. So these HTML routes are showing up in our API documentation. But at API docs are really meant for JSON APIs. These HTML routes are more for humans browsing the site, not for programmatic API consumption. So it would be nice to hide those routes uh from the docs to keep them cleaner. And fast API has a parameter for this called include in schema. So let me show you this here. So, right here beside our response class, all we have to do is say include in schema, set that equal to false. I'm going to do that for both of those routes. And when we set that equal to false, the route still works in the browser, but it won't appear in the API documentation. So, now that I've saved that, let me go back to our documentation and reload this. And we can see that now we only have this forward slapi post route. But if I go back to the site itself, uh for/post still works and the root of the website still works. So that's going to give us a nice clean separation between our API routes and the uh HTML routes that are meant for humans. Okay. So let's do a quick recap. So far we've got uh fast API installed. We've created a super basic application with multiple routes that returns some dummy data in both HTML and JSON form. And we have our automatic API documentation at for/doccks and for/redoc. And that's only showing our API routes and not those HTML routes. Now, in the next video, we're going to learn about templates with Ginga 2. And this is something that I've not seen many other fast API tutorials touch on. Uh instead of returning raw HTML strings like we did here, we're going to use proper templates to build maintainable web pages. So we'll set up template inheritance, add some styling, and keep our API and page routes working together nicely. But I think that's going to do it for this video. Hopefully now you have a good idea of how to get started with Fast API and the very basics, but there's a ton of great stuff to learn coming up in the series. But if anyone has any questions about what we covered in this video, then feel free to ask in the comment section below and I'll do my best to answer those. And if you enjoy these tutorials and would like to support them, then there are several ways you can do that. The easiest way is to simply like the video and give it a thumbs up. Also, it's a huge help to share these videos with anyone you think would find them useful. And if you have the means, you can contribute through Patreon or YouTube. And there are links to those pages in the description section below. Be sure to subscribe for future videos. And thank you all for watching.
