From the course: Advanced Python: Practical Database Examples

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Solution: Develop a GET endpoint with FastAPI and a Database

Solution: Develop a GET endpoint with FastAPI and a Database - Python Tutorial

From the course: Advanced Python: Practical Database Examples

Solution: Develop a GET endpoint with FastAPI and a Database

(upbeat music) - [Instructor] Let's add a dynamic get endpoint to our book API application. To start off, we'll add a get route. The route will be book and then a path parameter representing the book's ID. To add a path parameter, we'll need to do some research. Let's Google path parameters in FastAPI. Here, it says we can declare the path variable using the same format as we do Python strings. We'll use two curly braces and then also use it as input to the function. Let's add that to our implementation. When someone uses this route, we'll want to run a function. We'll call it retrieve_book. For the input, we'll have book_id from the path. It'll be an int. Now in order to retrieve the book, we'll need to interact with our database. We'll call a function called get_book from our database file. We'll also pass in the book ID so it knows which book to retrieve. Let's implement this get_book function in the database…

Contents