Getting Started and Installation
In this video I'm going to be showing you how you can create your very first API in Python using the FastAPI package. And FastAPI is just a very quick and convenient way to create an API, so I just wanted to show you the basics of how to use it so in the future you can create your own APIs very easily.
So the first thing you want to do in your new Python project is open up the terminal and type in pip install and you want to use quotation marks here. So quotation marks, fastapi, and we're going to install everything inside it, so [all], and close it with quotation marks. Then tap on enter and wait for everything to install. And once it is done indexing, you can close the terminal.
Creating Your First Endpoint
And we can import from FastAPI... FastAPI. And we're also going to import random since my API is going to return to the user random numbers that we query.
Now the first thing we have to do inside here is create the app, and this is the entry point to our API. So when we run this, we will be able to query information from the API. So FastAPI and we will instantiate it there.
Now let's create our main endpoint. So here we'll type in @app.get() and here we need to provide the path that we want to use to get this information. And we're also going to create an asynchronous function here. So async def root() and this is the root of our application. So every time the user loads the API, this will be the first response that user gets. So here we can return some JSON. And you can also insert a dictionary. The package takes care of passing it from a Python dictionary to the JSON format for us, so we don't have to worry about that. So you can either return JSON, or you can return a dictionary. I'm just going to return example and the example is going to hold the text of this is an example, and we're going to perform or return some data as well. And the data is just going to hold the value of zero.
So here's a basic API response. When we call this API without any endpoints, it's going to return to us this data that we can then later use in an application or we can share with our friends or other users so they can use it in their applications. So that was quite simple, and that's actually all we need to do to get started with using FastAPI.
Running the API with Uvicorn
So now let's open up the terminal again and I'm going to type in clear to get rid of that mess. And here we're going to have to use something called Uvicorn, so uvicorn, so that we can call our app and so that we can reload it automatically every time we make changes here. And this is one of my favorite features that comes with this FastAPI package. So here's the script and now we have uvicorn, then we need to call our script name which is main and we need to call the app as you can see right there. I'm using a colon between main and the app. We have to use this positional argument which is called reload. And when we tap on enter, it's going to start a local server for us.
So now we have this API up and running at our local server. And I'll explain real quickly what it means when we used this reload flag. It's actually very convenient and I love it. But let's go ahead and see whether this API actually works. And it does. As you can see, in this very small corner it says 'this is an example' and this is the response we get when we call our API. So now the API is live at this address here as you can see. If we were to reload this, we will get the data back once again.
Consuming the API with requests
And you can even play around with this data. I mean, you can copy this link here if you want and then we can create a new script and we can call this sample, it doesn't really matter in this scenario. And we can import the requests module. So here we'll import requests, install the package of requests so we can actually use it. Then we can close the terminal, close the sidebar, and we will type in request is going to equal requests.get() and I'm going to paste in my local address here, the one that we had inside the terminal down here. And now we can actually use this request. We can print request.json() and if we run this sample script, we're going to get the same data back as we got inside the web page. So it's working on our local address. Now we have the base of our API which we can test and continuously improve through FastAPI. But let's close this, that was just to show you that you can use it inside other projects immediately in case you want to do some testing.
Using Automatic Reloading
But otherwise you can just play around with it in your browser. Now as I mentioned earlier, inside the console we used this reload flag, and what does the reload flag actually do? Let's pretend we want to change something in our API, some functionality, some data. Let's say that the data should actually be 999. Now usually you would have to rerun your script or do some sort of refreshing, but thanks to that optional argument we added to the terminal, now when we edit our script and reload the page, it automatically updates it for us. So this code is automatically applied to the API as we update it, which is really cool, because if there's one thing that's really annoying as a Python developer is continuously having to rerun a program. So, so this was the root of our application.
Adding a Static Random Number Endpoint
Let's create another one. Let's type in @app.get() and here we'll type in random, and let's add the /random because that's the path we want to follow to get a random number. Now we can type in async def and we'll type in get_random. And here we can type in random_number of type integer, it's going to equal random.randint in the range of 0 to 100. And what we will return is some user-friendly JSON which says number, and the number is going to be the random number, and we also want to inform them about the limit that we used. So limit is going to be set to 100. And we have this new endpoint that we can use in our API. If we go back to our browser and here this time we type in /random, you'll see it's going to return to us a random number plus the limit. And we can refresh this as many times as we want and it's going to continuously give us that random number with this endpoint.
Creating an Endpoint with Path Parameters
But what if you want to set your own limit? So here we can go down and we can copy this and just paste it under. But this time we're going to add our own parameter. So here we'll add the slash and we'll add some curly brackets, and inside here we need to enter the variable name that we're going to use, which I'm going to call limit, and the limit is going to be of type integer. Now, instead of returning random number to the range of 100, we're going to return it to the range of the limit, and we will also replace limit with the limit variable that we've created. So now we're querying for this value here, which means that once we run this request and insert our limit here, it's going to insert it into our function and it's going to return to us the data that we have queried with the specifications that we have included. So going back to our API, you can see up here now we can type in /random/ and here we can enter a random number such as 999. And when we query it, it'll say that we entered the number of 999 and the random number we get back will be 569. And we can refresh that as many times as we want and it will give us a new response each time because it's applying that functionality. You can even put 100,000 if you want and it will still give us a random number back. Now it's also important to mention that if you have two paths that are named identically, the first one that it finds is going to be used first. So that's a detail you should keep in mind.
Auto-Generated API Documentation
And the last thing I want to cover is that when you are creating APIs with FastAPI, it automatically generates documentation for us. And to show you that, you just have to use the endpoint of docs and it's going to give you this default FastAPI documentation. Now, I'm not going to be explaining how you can edit this documentation in this lesson, possibly I will in another video if I see enough people are interested. But for today's lesson, I believe we've covered everything that we had to regarding how you can create your very first API using the FastAPI package.
And what we created essentially was a random number generator that allows the user to specify their own input. But now it's up to you to play around with this, to do a little bit more research, to maybe create a chatbot or something that returns a cool response based on the script that you've provided. Instead of having a random number here being returned, of course you can insert your own functions that maybe do some machine learning or maybe interact with another API so that you can get more information back.
Recap and Stopping the Server
And you can edit this accordingly to return interesting information for whoever wants to use this. And again, it's really cool that you can just install requests and play around with it inside here, because it also works if we type in, let's say /random, and we run this script one more time, you'll see we'll get the number of 44 and the limit of 100 back. Each time we run this, we will get a new result. So you can also use it to play around with data on your computer which doesn't come from a live API, which can always be good for testing. And you're also going to get a lot of the logs down here.
And at some point when you're tired of running this, you can just hold down Control + C to stop the server and it's going to end it there. So now whatever you edit inside here will not be reloaded and it's not going to be live anymore. But that just about covers everything I wanted to explain in today's video.
Do let me know what you think about FastAPI and whether you would like to see a follow-up video on this one, going into a bit more detail on how to create the documentation or how to create a more advanced project, of course. But with all that being said, as always, thanks for watching and I'll see you in the next video.