Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
Lecture: The most basic API
0:00 Well, this was fun, but "hello FastAPI" is not exactly what we were hoping.
0:05 We were hoping to build a web application that our programs and other services could talk to. So let's go and build that. Now,
0:12 if you've ever worked with Flask, FastAPI is sort of a Flask derivative style of API. It's not the same,
0:20 but a lot of your intuition will work for it there. So what we're gonna do is going to create, either call it "app", or I've also seen it called "api",
0:26 let's go with "api". We'll come over here and say "fastapi" and want to create a instance of FastAPI like that.
0:33 We'll drop that magic there. And then we're going to define some function. This is gonna be an API endpoint. So we're gonna have a calculator,
0:41 and we'll just say "calculate" like that and let's say it's gonna do something incredible like return two plus two. And then in flask
0:53 you would go over here and say "run", but that's not what happens here.
0:56 What we need to do is provide an external server and there's a really awesome production level high performance one recommended here,
1:04 and we're going to use "uvicorn". Now, notice there's an error here because uvicorn is not necessarily included when we install
1:13 FastAPI. So that's the next thing, to go over here, again It's not misspelled. This time, I'll just press the button and
1:20 let it do the install. You'll see it happen down at the bottom, and we're good. Now that's up there. And so then we just say "uvicorn run"
1:28 and we pass it the api. That's it. 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 gonna do is come over here and say "api dot" now be careful. If you've been doing flask, you might type route. That's fine.
1:46 But what you really wanna probably say is, I wanna only respond to get requests, http get requests, and then we'll pass over,
1:54 you can see there's a few options here, a lot of stuff going on, but our initial usage is simple, we're just gonna want to do a get against
2:01 "api/calculate" like this. Alright, now, let's try to run it and see what happens. So let's just open this up and see what we get.
2:11 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. So we're gonna have to go to "api/calculate"
2:24 We can fix this, like opens as a crash sort of thing in a minute but, calculate, spelling is hard, but once you get it right,
2:33 yes, look at that, 4, the answer is 4, and maybe we want to respond with some sort of JSON, right? That's how API's are.
2:41 So we could come over here and we could say "result =" let's just say, here we go,
2:57 we're gonna calculate the value then we're gonna store it into this thing that we're gonna return,
3:01 and actually, let's go ahead and inline that right there, so we'll just straight up return that value,
3:07 that dictionary, there. So we'll do our work and then we're going to return the dictionary. Run it again, and now you can see if we go look at the raw
3:15 data we have proper JSON. So when you're talking to API's, it probably makes sense to have some kind of schema, some kind of
3:22 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.
3:34 So that's it. Let's just review real quick what we've got to do. We import FastAPI, we create an instance of the API, and on
3:42 there we use that to decorate the functions, in this case we say we have a "calculate" function and it's going to handle requests to "/api/calculate"
3:52 and right now, it doesn't take any arguments. We're gonna work on that. But right now, it just says, "Well, okay,
3:56 you want some calculations? How about two plus two? That's cool". And we'll pass that back.
4:02 Then we're gonna return this dictionary, which is automatically converted to JSON, and also, if you look over here and we go to the
4:12 network and we do this request, and we look at this one, you can see that the response content type assumes that
4:21 it's application/json. So it says this is JSON, which is all the more reason that the actual thing that we get over here should
4:28 be JSON, not just the number four. So it automatically returns this as JSON if we passed the dictionary there by default,
4:36 and then in order for our application to start, we're gonna come down here and say "uvicorn, run this application". And we could
4:44 also add in here "port", I'll explicitly call out what is the default value so you can see if you want to adjust them. Here we go. That way
4:54 if you saw, even if we put like a 1 here it'll listen on port 1 and so on, or 8001. Cool. Alright, so this is simple, right? This is incredibly simple
5:04 to build a high performance api and get started, we just have to have a project, set up the dependencies
5:10 FastAPI and uvicorn, create a simple method and call uvicorn run. Done. You've built an API. Pretty awesome, right?