Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Creating Your FastAPI App

In this lesson, you’ll visit http://127.0.0.1:8000 to check that your app works.

00:00 Creating Your FastAPI App. As is traditional, start with a Hello, World! implementation of FastAPI. This implementation is a FastAPI app that has one endpoint.

00:17 Create a file named main.py in the shortener_app/ folder and add the code seen on-screen. First, you import FastAPI.

00:29 You define app by instantiating the FastAPI class. The app variable is the main point of interaction to create the API.

00:38 You’ll refer to it multiple times during this course. Here, you use a path operation decorator to associate your root path with read_root() by registering it in FastAPI.

00:50 Now FastAPI listens to the root path and delegates all incoming GET requests to your read_root() function. The read_root() function returns a string.

01:00 This string is displayed when you send a request to the root path of your API. The code you just added to main.py is the start of your application.

01:10 As you know, to run your app, you need a server, and you’ve already installed uvicorn. Run the live server using the command seen on-screen.

01:27 You tell uvicorn to run app of your shortener_app package’s main.py file with the command seen. The --reload flag makes sure that your server will reload automatically when you save the application’s code.

01:41 This automatic reload is handy, as you don’t need to stop and restart the server each time you make a change to your code. Instead, you can just keep this terminal window running in the background.

01:55 Now that the server is running, go to the URL seen on-screen and check that you get the correct response.

02:04 When you navigate to this URL, you are sending a GET request to the root of the FastAPI app. The response is the welcome message that you defined. Congratulations, your app is working!

02:18 One of FastAPI’s great features is that the framework automatically creates documentation of the API endpoints for you. If you go to the URL seen on-screen, you will see the Swagger UI documentation that FastAPI has created.

02:36 Swagger UI not only gives you an overview of your API’s endpoints, but it can also be used to test the API. You’ll leverage this functionality throughout the rest of this course to keep tabs on the API’s behavior.

02:50 With a bare-bones FastAPI application up and running, in the next section of the course, you’ll decide what the app will do.

Become a Member to join the conversation.