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