#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: FastAPI
Lecture: The most basic API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, this was fun, but hello, FastAPI is not exactly what we were hoping.
0:05 We're hoping to build a web application that our programs and other services could talk to. So let's go and build that.
0:12 Now, if you've ever worked with flask, FastAPI is sort of a flask derivative style of API.
0:19 It's not the same, but a lot of your intuition will work for it there.
0:22 So what we're gonna do is we're gonna create, we can either call it app, or I've also seen it called API. Let's go with API.
0:27 We'll come over and say FastAPI and want to create a instance of FastAPI like that. And we'll drop that magic there.
0:34 And then we're going to define some function. This is going to be an API endpoint.
0:39 So we're going to have a calculator and we'll just say calculate like that.
0:45 And let's say it's going to do something incredible, like return two plus two.
0:51 And then in flask, you would go over here and say run, but that's not what happens here. What we need to do is provide an external server.
0:59 And there's a really awesome production level high performance one recommended here. And we're going to use uvicorn.
1:07 Now notice there's an error here because uvicorn is not necessarily included when we install FastAPI. So that's the next thing to go over here.
1:16 Again, it's not misspelled. This time I'll just press the button, let it do the install. You'll see it happen down the bottom. And we're good.
1:24 So now that's up there. And so then we just say uvicorn run and we pass it the API. That's it.
1:32 Except how does it know that calculate has anything to do with this and what URL should we use anyway.
1:38 So the last thing we're going to do is come over here and say API dot. Now be careful, you've been doing flask, you might type route. And that's fine.
1:46 But what you really want to probably say is I want to only respond to get requests HTTP get requests.
1:53 And then we'll pass over, you can see there's a few options here, a lot of stuff going on, but our initial usage is simple.
1:59 I'm going to do a get against API slash calculate like this. All right, now let's try to run it and see what happens.
2:08 So let's just open this up and see what we get. Now this is not the most encouraging response here. What is the response not found?
2:17 Well, that's because nothing is listening on just the forward slash like the basic URL.
2:22 So we're gonna have to go to API slash calculate, we can fix this like opens as a crash sort of thing in a minute, but calculate spelling is hard.
2:32 But once you get it, right? Yes, look at that for the answer is four. And maybe we want to respond with some sort of JSON, right?
2:40 That's how API is are. So we can come over here and we could say, result equals.
2:46 Let's just say, we're going to calculate the value, then we're going to store it into this
3:01 thing that we're going to turn and actually, let's go ahead and inline that right there.
3:05 So we'll just straight up return that value, that dictionary out of there. So we'll do our work, and then we're going to return the dictionary.
3:11 And again, and now, you can see if we go look at the raw data, we have proper JSON.
3:17 So when you're talking to API's, it probably makes sense to have some kind of schema, some
3:22 kind of data structure rather than returning just the number four or some string. Not always true, but generally, it's a good idea.
3:29 So we'll just start doing that here. And we'll build on that, of course, as we go along throughout this course. So that's it.
3:35 Let's just review real quick what we got to do. We import FastAPI, we create an instance of the API.
3:42 And on there, we use that to decorate the functions. In this case, we say we have a calculate function, and it's going to handle request to slash
3:50 API slash calculate. And right now, it doesn't take any arguments, we're going to work on that.
3:55 But right now, it just says, well, okay, you want some calculations? How about two plus two? That's cool. And we'll pass that back.
4:02 We're going to return this dictionary, which is automatically confirmed to JSON.
4:07 And also, if you look over here, and we go to the network, we do this request.
4:14 I look at this one, you can see that the response content type assumes that it's application slash JSON.
4:23 So it says this is JSON, which is all the more reason that the actual thing that we get over here should be JSON, not just the number four.
4:31 So it automatically returns this as JSON if we pass the dictionary there by default.
4:36 And then in order for our application to start, we're going to come down here and say, you vehicle run this application.
4:44 And we could also add in here port, I'll be explicitly call out what what is the default value. So you can see if you want to adjust them. Here we go.
4:52 That way, if you saw if we put like a one here to listen on, or one and so on, or 8000. Cool. All right. So this is this is simple, right?
5:03 This is incredibly simple to build a high performance API and get started, we just have
5:08 to have a project, set up the dependencies FastAPI, new vehicle, create a simple method, and call UV corn run done, you've built an API.
5:17 Pretty awesome, right?


Talk Python's Mastodon Michael Kennedy's Mastodon