Loading video player…

Getting Started With FastAPI

Resources mentioned in this lesson:

00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll get you started in FastAPI with a longstanding programmer’s tradition, “Hello, world!” Fast API is a framework that you can use to quickly write web-based REST API services.

00:15 Frameworks are a little different from libraries. When you use a library, you’re in control of the flow of your code calling it. By contrast, when you use a framework, it is in control of the flow, invoking your code when it sees fit. This kind of makes sense, as FastAPI is actually a server which listens on a network port.

00:35 When someone hits that port and requests a URL, FastAPI maps that to your code invoking it. The key to writing a FastAPI app is telling the server which URLs you want to support and what code to call for each of them.

00:51 You do this by using decorators on top of regular old Python functions. The decorator for your function declares the URL that you want to associate with the code, and Fast API registers that URL against your code and calls your function when someone hits that URL on the server.

01:09 The whole point of an API is to send something back to the caller, so Fast API also handles data serialization. For example, if you return a dictionary from your function, FastAPI automatically turns that into JSON for you.

01:24 FastAPI is a third-party framework, which means you’ll need to install it. As always, when dealing with third-party code, it’s important to use a virtual environment.

01:33 FastAPI is packaged in a few different ways. The bare library needs a server to run on top of. If you’re already using something like Uvicorn in your code, then you might want to use the bare library.

01:45 To avoid the complications of that additional install, I’m using the standard package instead, which includes a server in the installation. To install the standard package, you include square brackets, [standard] in your call to pip, or whatever installer you’re using.

02:01 Note that some Unix shells see square brackets as special characters, so you might need to put quotes around it to get it to work. For more information on the different installation variants, see the FastAPI documentation here.

02:15 During the course, I’m going to be using curl, a command-line tool for interacting with servers, including HTTP. If you don’t have this client, you can use a regular old browser, but by using something on the command line, I can show you code and its results easily at the same time in the same terminal.

02:32 Depending on your OS, you might have curl installed already, but if not, this is the URL where you can get it. It doesn’t change that much, but for the record, I’m using curl 8.7.1.

02:44 Once you’ve got FastAPI standard installed, it’s time to write your first app. Let me show you “Hello, world!” This is main.py.

02:55 A few things to note about the name. First, you typically put FastAPI applications in their own directory. You don’t have to, but it’s common practice, so I’ve put this one in a directory named intro/.

03:07 Second, the choice of main.py is helpful. FastAPI will look for a file named main.py, meaning you can type less when you run the server.

03:16 That’s another reason for the directory. Since all my examples are named main.py, well, they can’t all be in the same place. Not surprisingly, if you want to use FastAPI, you need to import it.

03:29 Remember, FastAPI is a framework. It invokes your code, not the other way around, and the entry point to the server is an instance of a FastAPI object. The constructor here supports arguments as well if you need to do something fancier and configure the server’s behavior.

03:45 Once you’ve got an instance of the app, you use it to register URL routes against your functions. The app object has a number of decorators, each of which corresponds to a different kind of REST call.

03:58 The .get() decorator says to use the HTTP GET method, which is the same method your browser uses when it fetches a webpage for you. The argument to the decorator is the URL that you want associated with your code.

04:11 In this case, I’m associating it with plain old /, meaning the base URL, the one which is just your host name and port. Because your function is decorated, the name actually isn’t important.

04:24 You can call it whatever you want. It is still best practice, though, to name it something descriptive. Seeing as the webpage that is associated with the base URL is known as the homepage, home() seems like a reasonable choice.

04:37 Your API calls need to return something. Typically that’s JSON data. Fast API automatically serializes content for you. So if you return a dictionary from your function, Fast API turns that into JSON and sends it back to the caller.

04:53 Technically, you can just return a bare string if you like, but your API is likely to use structured data, so it’s good practice to always use a dict so that all of your calls return the same type of data.

05:04 It makes it easier for the folks writing a client to talk to your service if you’re consistent. Okay, let’s try this out. Remember, FastAPI is a framework so your code isn’t a script.

05:14 You’ll be running the FastAPI server, which will load and invoke your code. When you installed Fast API standard, you got the FastAPI program with it.

05:26 This program takes commands. I’m going to use the dev command to start the development server pointing, it at our main.py file.

05:36 That’s a lot. Let me just scroll back here.

05:40 The information on the screen tells you what the server is doing. It started up in my intro/ directory and went looking for some files. It found the main.py file that I told it about and then imported the app object from it.

05:52 It then started a server on 127.0.0.1 port 8000, which is the localhost network address on your machine. You’ll be able to hit that with your browser or API client, but other machines won’t be able to get to it. In production, you’d configure this to sit on an address that other people could actually use.

06:13 Down at the bottom here it tells you everything is going and that it’s running a reloader. That means it’s watching main.py, and you can live edit your code and it will keep the server going reloading after you make any changes and save it out.

06:27 This is very helpful if you’re developing. It also tells you down here that you hit CTRL+C if you wish to stop your server.

06:35 As you use your API, debug information will also show up on this screen telling you what URL got hit. If your code calls print, the output would show up here as well, which is useful when you’re trying to make sure things are working.

06:48 I’m going to open up a web browser now, and I’ll try this out.

06:52 Let me enter the URL for our homepage.

06:59 And there’s the result. I’m using Firefox, which has JSON handling tools built in, which is why this is kind of pretty-printed. If I click on the raw data tab, you get to see exactly what came back, which in this case was a dictionary with a key of "message" and a value of Hello, FastAPI! If you are using Chrome, there are plugins available to do formatting like this, which you might want to do if you’re playing with JSON a lot.

07:28 Or you can also use API exploration tools like Postman instead. I want to show you one more thing, so I’m going to keep the server running and then open another terminal window

07:39 rather than popping in and out of the browser. During this course, I’m going to be using the command-line web client known as curl. To use curl, you simply give it a URL, and it will grab the content from there, and there you go.

07:53 The same result. You don’t have to use curl when you’re experimenting yourself, but the text-based interface means I can keep code and the results on the screen at the same time, so you can better see what is going on.

08:07 You saw me use FastAPI dev to start the development server. Although I explicitly typed main.py, you don’t have to. If your app is in app.py, api.py, or main.py, the server will automatically load it.

08:23 I prefer to be explicit about it though, and as I mentioned, the development server will watch for changes in your code, so you can leave it running and develop as you go.

08:32 This is a development server only, though. You shouldn’t use it in production. Instead, for that, you use the run command. In production mode, the reloader is disabled, and certain settings are turned on by default to optimize for production use.

08:49 If you want to mount your server against something other than localhost, the default, you can use the -host argument to FastAPI to specify the IP address.

08:59 And likewise, the -port argument allows you to choose something other than the default port 80.

09:06 Your API may need more complex data structures than dictionaries. To help with this, FastAPI integrates with the Pydantic library. I’ll show you that next.

Become a Member to join the conversation.