FastAPI Tutorial | FastAPI For Machine Learning | FastAPI Explained | FastAPI Course | Simplilearn

Simplilearn · Beginner ·🔧 Backend Engineering ·6mo ago
🔥Professional Certificate in AI and Machine Learning - https://www.simplilearn.com/professional-aiml-program?utm_campaign=RkmYJURU5k0&utm_medium=DescriptionFirstFold&utm_source=Youtube ️🔥 Professional Certificate in AI and Machine Learning - https://www.simplilearn.com/professional-aiml-program?utm_campaign=RkmYJURU5k0&utm_medium=DescriptionFirstFold&utm_source=Youtube 🔥Professional Certificate Program in Generative AI and Machine Learning - IITG (India Only) - https://www.simplilearn.com/applied-generative-ai-course?utm_campaign=RkmYJURU5k0&utm_medium=DescriptionFirstFold&utm_source=Youtube 🔥Advanced Executive Program In Applied Generative AI - https://www.simplilearn.com/applied-generative-ai-course?utm_campaign=RkmYJURU5k0&utm_medium=DescriptionFirstFold&utm_source=Youtube FastAPI is a modern, high-performance web framework designed for building APIs with speed, reliability, and clean architecture. It leverages Python type hints to automatically generate documentation and reduce development errors, making it ideal for data-driven teams and backend services. Its asynchronous capabilities allow developers to handle large volumes of requests efficiently, similar to how successful learners optimize their workflow with the right tools. FastAPI’s built-in validation, dependency injection, and auto-generated Swagger UI help ensure credibility and maintainability of the products built on it. Whether you're creating microservices, ML model endpoints, or enterprise APIs, FastAPI delivers performance close to Node.js and Go while retaining Python’s simplicity. This tutorial breaks down its core features, workflow, and real-world use cases to help beginners build production-ready APIs confidently. Following are the topics covered in this tutorial video on FastAPI Tutorial For Beginners: Introduction to FastAPI Why FastAPI? Django vs Flask vs FastAPI What We’re Going to Build in This Video How GitHub Copilot Helps You Code Faster Step 1: Setting Up Our Python Environm

What You'll Learn

This video teaches FastAPI for machine learning applications

Full Transcript

[music] You've probably tried building a project or an app before and you know how frustrating it can be when things just don't come together. You're stuck spending hours on back-end setup trying to figure out what's wrong and suddenly you are overwhelmed. What if I told you that there's a way to make this process quicker, smoother, and way less stressful? That's exactly what Fast API offers. And the best part is you don't have to do it all alone. With tools like GitHub Copilot, you can code smarter and faster than ever before. In today's video, we are going to show you how fast API can help you build APIs with minimum effort, maximum performance, and a little help from AI. Now, before we dive in, you might be wondering what exactly is fast API, and why should I care? Now, when you're building a website or app, you need a back-end system that handles things like storing data, processing user input, and fetching information. Whenever someone clicks a button or submits a form on your side, you need the backend to respond by doing something. Whether it's saving data, retrieving information, or updating a database, fast API is a framework that makes this back-end process quick and easy. It's the brain behind your app and it's designed to make coding APIs as painless as possible. But here's the twist. With tools like GitHub Copilot, you don't have to write all the code from scratch. Copilot is an AI powered tool that can help you write code faster by suggesting lines, functions, or even entire blocks of code based on what you're trying to do. Imagine it as a co-pilot who's always one step ahead, helping you build your app with fewer mistakes and more efficiency. Now, you might be familiar with other Python frameworks like Django and Flask. Let's briefly compare them to fast API to help you understand why fast API is so special. So, first let's talk about Django. Now, Django is just like a huge toolbox for building web apps. It comes with a lot of built-in features like user authentication, admin interfaces, and database management. So, you don't have to build those things from scratch. Perfect for large scale projects, but it can feel a bit heavy and slow with smaller apps because of all those features. Now, the question is why should you use Django? Now, if you're building something complex like an e-commerce site, Django will give you everything you need to know. But there's a downside too. For smaller projects, it can feel slow and bloated with features you might not need. Next, let's talk about Flask. Now, Flask is more lightweight and flexible. It gives you the basic and let you add more features as needed. It's great for smaller projects or when you want full control over how things are built. So, why should you use Flask? Now, if you want simplicity and flexibility, Flask is a good choice. There's a downside, too. For bigger projects, you might need to add a lot of extra code for things like authentication, database management. So why should we use fast API? Well, it's modern, fast, and incredibly easy to learn. It combines the speed of flask with the power of Django, so you get the best of both the worlds. It's designed for high performance and makes building APIs faster than ever. Plus, it automatically generates interactive documentation and uses Python type hints for data validation, making your development process even smoother. Fast API is built for speed and ease of use. You can get your backend up and running quickly with fewer lines of code. Now, even though it's fast, it doesn't cut corners on security or other important features. It's great for both small apps and large scalable projects. Fast API automatically generates documentation which makes it super easy to test and debug. So now let's talk about what we are going to build in this video. Well, we are going to cover the following topics. First, we'll have a look at how to set up fast API. We'll start by installing fast API and getting our environment ready to go. Next, we'll be building the backend. You'll learn how to create simple fast API endpoints to handle product data. We'll also see how you can connect to a database. We'll connect our fast API app to Postgra SQL database to store product information, but you can use any database you prefer. Testing the API. We'll use fast API's built-in tools to test our API, ensuring everything works smoothly. We'll have a look at how we can connect both the front end and back end. I'll show you how to connect your fast API backend to a React front end. We'll also talk about handling cores which is cross origin resource sharing. So both the parts can communicate seamlessly. Now how does GitHub copilot helps you code faster? Now while we're going through the steps of building this app, GitHub copilot will be your AI coding assistant making things even easier. For example, when you need to define a root or model, Copilot will suggest entire lines of code. So you don't have to think about syntax or structure. It will even suggest helpful comments and functions that match what you're trying to do. Now, imagine you're writing a complex fast API route. Instead of goggling syntax or struggling with how to validate incoming data, GitHub Copilot will instantly suggest the perfect code for you based on the context of your project. This speeds up the process, helps you write cleaner code, and ensures you're using best practices. And by the end of this video, you will have learned how to build a functional fast API app with the backend connected to a database. And you'll have seen firsthand like how tools like GitHub copilot can make your coding journey faster and smoother. Now before we get started, here's a quick quiz question for you. The question is, what is fast API mainly used for? Your options are designing UI components, building and managing APIs, creating mobile apps, or hosting websites. Let me know your answers in the comment section below. Now, before we dive into coding with Fast API, there are a few things you might need to know. Now, think of this as the foundation that will help you get everything set up on your computer so that you can smoothly follow along with the course. We will be covering what you need to install and I'll explain this in a few terms along the way. Now the first thing we need is Python which is the programming language we will be using to build a backend app. You can think of Python as the language that tells your computer what to do. It's just like giving instructions to a robot and Python is the language that makes those instructions understandable to the robot. So why did we select Python? Because Python is easy to learn and understood by many companies and developers worldwide. It's versatile meaning you can use python to build anything from websites to AI programs. What you need to do is just go to this python.org and from here you need to download this file. So you can just download the updated recent version of Python from here. Now if you want to check the version number you can get it from here and then just hit on this download button and download it. Now since I have already installed Python in my system what we can do is we'll go to our terminal. So in your system just search for terminal. It will be there by default and after terminal just type a command there which is python - version. So this will check if the python is actually installed in your system or not. And if it's installed it'll show something like this python 3.137. Now this is the version number of my python. Now next you install nodejs. Next you install nodejs a helper for running react fronted. And this is optional. Now, if you want to work with a React front end in this course, which we'll be using to show product data, then you will need to install Node.js. Now, React is a JavaScript framework which is used for creating interactive user interfaces, the part of the app that users see and interact with. So, why NodeJS? Because NodeJS can help you run JavaScript code outside of a browser. It's very important if you want to run the React front end locally in your system. You don't need to know JavaScript for this course, but installing NodeJS will make it easier to set up the front- end part of the project. So just go to node.jsorg and then from here you'll get this option of Windows installer MSL and standalone binary. Make sure that you hit on this Windows installer button and then install from here. Now because I have Windows operating system, I'll just selected Windows. Now if you have Mac OS or Linux you can just select your OS from here and then you can download it like that. So I've already downloaded NodeJS in my system. Now again we'll go back to our terminal in similar way. Now to check for node what you'll do is you'll just type this command node version. So this will check the NodeJS version installed in my system. And here you have it. Now if it is already installed in your system successfully it will show something like this v22.19.0. O. So this is the version number of the node which we have installed in our system. So now that we have covered Python and NodeJS, let's move on to the key components we will be using to run which is fast API. So I've already told you before what fast API is. Well, I'll repeat it again. Fast API is the framework we will be using to build the back end of our app. You can think of it as a tool that will make it easy to handle request from users like when someone clicks on a button to add a product and then send back the right data like showing a list of the products. Now fast API is modern, easy to use and it's really fast which makes it a great choice for building web apps. Next is uicon not unicorn uicon. It is the server that runs your fast API app. It's just like an engine that makes the app come to life and it handles request from the outside world. Let's say for example a user interacting with the app. Now fast API and UION they both work together. Fast API will build the back end and UION will run it. So we need to install fast API and uicon as well. Now just go to this terminal section again and just type this command which is pip install fast API and this will tell Python to install fast API on your system. So as you can see from here I'll just enlarge this terminal. Now our fast API has already been installed and these are the packages it has installed for us. It's successfully done and to update run you can use python.exe m pip install to upgrade the version if you want to. Next we'll be installing uicorn and again for this we need to just uh type this command which is pip install uicorn and this will install the uicorn server. So we can run our fast api app pip install uvicorn and again our uicorn is also installed in our system. So now that we have installed uicorn and fast api let's move on to one important thing which is paidantic. Now if you guys are not familiar with this term let me explain it to you. Now, Paidantic is a tool fast API uses to validate the data you send it to and from your app. You can imagine if you're filling out a form with your name and age, paid make sure that the name is string, text, and the ages number. So, it will help ensure that the data is valid and it won't cause errors in the app. Now, what it does is it also formats the data in a way that makes it easy to work with. Now if you're sending product information like name, description, price, pyantic ensures that everything is formatted correctly. So in our app, we'll use Pantic to validate the product data when we add, update or delete products. So let's move on to our first step which is installing VS Code now. So I already have VS Code installed in my system. So I'll just go back to the Visual Studio Code from here. So make sure you have the latest version of VS code installed in your system and then from file you can open a file. Well before this here an important thing which is you need to install the Python extension in your VS code. So just in the extension section uh search for Python from here and you get this option of Python right not the debugger one the Python and just click on that. So I've already installed this in my VS code and if you haven't installed this, make sure you install this extension so that your code works perfectly and then we'll go back to our files. So this is already installed. We'll cross this. We'll go to a file. Now you can open a folder or you can open a text file from here. So I'll be opening a folder from here which I have already prepared in my downloads which is fast API demo products. I'll just select this folder from here. So as you can see on the left side uh in the explorer section we have all the files ready here right. So I'll show you everything step by step and on the right side you also have this GitHub copilot agent mode. I'll show you how can we use this as well now. So now that we have installed Python in our system we'll go to our terminal section and then we'll create a virtual environment to manage our dependencies. So in the terminal section just type this command python - m and then you have when v we are creating a virtual environment. You can name the virtual environment here. I'll just keep the name as v same and just type enter. So it says here it's unable to copy this file to this downloads fast api. So I've again given the name as hyphen m venv and the name here is fast api. Now we'll create a virtual environment again. So now that our virtual environment is created by a different name. So I can just view it here. Now in the fast API section, you can see this tab opening here in the file section. So you can see here all the live packages from here the scripts and all. We'll install the scripts as well. So let's now activate the scripts in our system. Now if you have Windows, you have to type command like when scripts activate. So just type give the name give the name as fast API/cripts /activate. Okay. So this we cannot uh sorry I've given the wrong name. So fast API activate. So now it says fast API scripts has now been activated. Now if you're on Mac or Linux in case of need to just enter bin which is fast API bin activate. if you're using Linux or Mac OS system. Now once again we have already installed fast DPI and Ubicon in our system. If not again from here in this terminal we can check if it's already installed in our system. So we'll type this command again which is pip install fast API uicon. So we're just checking for both uicon as well as for fast API install fast aon. And I'll just hit on enter. So it has already installed all the packages and all you can see from here. So we had already done this before as well. So just for checking out I check this command now. So our API uicon has been installed. Now let me show you a simple Python code. So I'll remove this terminal section from here. I'll go to file and I'll create a new file which is hello. py and I'll hit on enter. Now we need to understand the very basics of Python specifically how functions work. So here I'll be demonstrating a simple function example. So the code is def greet and bracket enter a name colon. So you get this automatically appearing tab here and again you can just see it from here return f hello name. So this really helps you while you're coding return f hello and then you can give a name here. This is the exclamation mark. And now we'll give the we can assign the name of the person. Let's suppose it's John. And then we'll print this statement by print greet name. Okay. So just inside this bracket greet and then you can write name from here. So here the def greet name. Now this will define a function which is called greet that takes one argument which is name. Now here the next function which is return f hello. Now this will return a greeting message using Python's f string which is the formatted string. It inserts the value of name into that string. Now you have name as John. We assign the string John to the variable name. And then finally we print greet name. So we are calling the greet function with the name as John and it will print the greeting message which is hello John. Let me show it to you. So we'll just run this code from here. We'll just click on run this Python file. So you can see your output right in front of you which is hello John from here. Well this is how you code a basic Python code. So we'll hide this panel now. Now if you're interested in mastering the future of technology then the professional certificate course in generative AI and machine learning is the perfect opportunity for you. This is offered in collaboration with ENI Academy. And it's an 11-month live and interactive program which will provide hands-on expertise in cutting edge areas like generative AI, machine learning and tools like chat GPT, DLU and even hugging face. You'll be gaining practical experience through 15 plus projects, integrated labs and master classes delivered by esteemed IT Kpur faculty. And alongside you'll earn a prestigious certificate from IT Kpur where you'll receive official Microsoft badges for assure AI courses and career support through simply learns job assist program. So guys hurry up and enroll now and you can find a course link below. So now that we have seen a basic Python code, let's move on and create a basic fast API application. It's very beginner friendly. So even if you're new to this, you'll understand it now. So I've already showed you how to install fast API and Ubicon in our terminal and how you run it. Now it's time to actually code. So we'll be coding uh using fast API. So we'll create one more file from here and we'll name it as fast API py. So we'll type the code. So we'll say from fast API import fast sorry this will be small letter API. So it's really case sensitive. So make sure that you're paying attention to the letters the u small and the capital letters here. So here we use the small letters and we using API and capital letters. So we are importing the fast API class which is the core of our app here. Next we'll be creating an instance of fast API. So I'll just give a comment here step create an instance of fast API and then we'll type app is equals to fast API you can see it from here right and it's absolutely correct so just click on this when you hover on this you get this option of accept tab just click it here and then you get your code ready in front of you now it makes our coding way more simpler now just by using these features then we'll define a simple root So we'll just mention it here. Define a simple root and then at the rate app dot and then mention backslash here def root brackets and colon and then we'll return this as message colon hello, fast API. So now let me explain you this code. So we have imported the fast API class which is the core of our app. Then by using this app is equals to fast API we have created an instance of the fast API class. This instance will handle all the incoming HTTP request. Next we have we define a simple root which is at the rate app.get slash. Now this is a decorator that will tell fast API to listen to the get request on the root URL. Then we have def read root. Now this function is called when we get request which is made to the root URL using the slash function and finally we return the message which is message hello fast API. Now this function returns the JSON response containing the message which is hello fast API. I hope it's clear to you and then to run this application we'll just uh hit on this run here. Now to run this file we need to actually give one command and the command is uicon main colon app reload. So I'll just type it here uicorn reload. So by doing this we'll just see this uh output coming here. We'll watch for the changes in this directories and uicorn is running on the http 127.0.08000. Right now this is really important. So you can just follow this link from here. So I'll just click on this link and it will open to our website. Well, it was uh it's saying some error. It can't read the page at this moment. But let me just explain something to you. Now this URL which is the HTTP 12708,000, right? This is the local server address for a fast API application running on your computer. When you run your fast API app using uicon, the server starts on your local machine and 127.0 0.1 which is also known as localhost is the address that refers to your computer. The port 8,000 is default port which is used by uicon. Now understanding the address 127.0.1 this is the IP address for local host meaning this computer or your own computer and the port here which is 8,000. This is the port number where the fast API app is running. Now when you run uicorn it defaults to port 8,000 but you can change there if needed. Example by running uicorn main app reload port. So once your fast API application is up and running you can start testing it interactively with the automatically generated swagger UI. Fast API provides this interactive documentation that allows you to see all your API roots and test them directly from your browser making it super easy to verify that everything works as expected. Now to access swagger UI you need to run the fast API app. So uh we can also take help of GitHub copilot here. So I'll just say copilot to run the swagger with this port using fast API app. All right, we'll just give it to Copilot agent and let's see what it actually does. Now it says that I'll search the workspace for fast API app definitions and server entry points. So it has searched for the workspace. Now it's opening fast API. py the application the code which we have already done before and yeah it's super amazing. It's actually reading and creating a task plan now. So let's see what it does to inspect the file. Well, it says I'll open fast API to inspect the file and all right. So, it's saying now I'll start the uicon server from the project virtual environment. So, swagger appear at docs and it says I've created a short todo plan. Then we'll start the server from the project's virtual environment. All right. So, it's creating a to-do plan for us and then it's saying that we'll start with the swagger UI. And now we just need to run this command from here. Reload host. All right. So we can also run this in the terminal here or you can see the agent mode to run it from here. Let's test with the help of agent mode. So I'll just select allow now and it's running the uicon server. So you can actually check the um everything whatever the agent mode is doing in your VS code terminal section. From here it says server has been started. I'll update the to-do list and then explain how to open swagger. And it says done. The fast API app was launched from the project virtual env. Open the swagger UI at this particular link and it has also given us quick notes and useful commands. Server starts run and this is the directory of our file in the workspace terminal. You can also stop the server by pressing control C in the terminal where the server is running and also given an alternative start from project root run which is hyphen m uicon fast API app reload and the port name. All right, next step I'll pick on this. Okay, so let's just run and open this file server from here. So here it says when I run this file it says the site can't be reached. Well, we'll go back again to VS Code Copilot and it says fast AP app was launched. Open the swagger UI at this. So, I'll just type to copilot that I'm unable to open the link. It says page not found. Okay, it says I'll restart you to capture startup logs again. I'll just hit on allow. So, it has imported few libraries. I can see it here in my terminal section. Okay, it's started a reloader process. Uicon running on this particular uh port from here. I'll restart it. So it's saying I'll add a new app. py avoids name conflict then restart uicon using this. All right. Can I'll just hit on this allow. It says run this push command and again I'll just select allow. So I've run this command once again and again it says it can't reach the base. So there might be some connection problem I feel. All right. Now it has done a lot of task for me. It also says you can restart the uicon command above. The next important thing which I wanted to show you guys is how you can test your roots. I've asked agent to test the roots where you will see all your API roots like get, post, put, delete, listed in the swagger UI and you can test each one of them by entering data like a new product and clicking execute. So let me give it to agent mode and see what it does. Okay. So here it says I'll add it to root testing. I'll start this over in the background and run quick request to verify the endpoints. Now it's thinking to set up the configuration from here. Just select allow from here. So it's starting the API server in the package folder and then running a few HTTP request to exercise the root list and create product endpoints. So it says uh I'll first launch the server in background and then run get post checks from here. So again it says web content make malicious. I'll just click on allow. And we have to select allow. So it's basically checking for the server connection first. And it's running and capturing the logs from here. It says I'll start u with sdt out sdr directed to files. Wait briefly and then I'll read well. Let's see. Okay. Continue to iterate. Allow does have it's throwing some error and please uh type help to copyright tags. So as you can see here that if you see the page not found are dogs. Now this can happen in your case also then you can confirm the server is running on port 8000 the uicon process and if not then you can start it using the commands above. You need to make sure that no firewall or other process are blocked by this particular port and if there are any import name collision at a file name which is the project root it says I have already added a safe uh app.py earlier but the package this contains intended API. Well says next steps I can stop the Python ripple in your terminal and can start the so I'll just uh say to stop the replicated Python files in my terminal and then it'll start again with the automated test and then it can report results and sample responses so that we won't be seeing that error again. So this might take some time till then the next step is the view response one. Now after executing a request, Swagger UI will show you the response directly which will let you see whether your API is returning the expected results or not. Let's say for example you can try testing the post products endpoint by filling in the required product details like name, description, price, quantity. Now once you click on execute the result will be displayed confirming that the product was added successfully. Let's move on to our third step which is creating crude operations for products. We will create crude operations which is create, read, update, delete for managing products in a fast API app. Now these operations will be handled using the HTTP methods like post, get, put and delete. So let's just move on to the coding section from here. So we'll just click on this new file and we'll just enter the file name as crude py. So we'll just type from fast API import again fast we can just select from here it will be more faster then type in from typing import list and then optional app is equals to fast API from here now this will be in memory storage for the products and this will be replaced with the database in the future so I'll just mention in hashtag from here in memory storage for the products and then products is equals to brackets. Then now I'll show you how to create a product at the rate app dot post products. Just click on this and def create product. You can give the name string. Then you can give the description. All right. So here I've created the product. You can see def create product name is string. Description string price as float quantity integer type. Then we've given the product name description and the products do append function using the product and then I've returned message product added successfully product. So let me just explain you this code. So here while creating a product you can see the post method. This endpoint will allow us to add a new product. The function accepts the product details which is the name description float quantity via the HTTP post and it will store the product in in memory list. Then you have this adorate post products. So in this you can create products name string description. This decorator will define the root that listens for the post request at product/ table. Then we have also have the parameters listed here. Then the function where it accepts the name, description, price and the quantity as input parameters to define a product. Next we have updated the product. Here this endpoint will allow us to update an existing product where you specify the product ID, the fields, name, price, description that you want to update. So after we have updated a product here, I've given the message as well. Let's see how you can delete that product. Now for deleting the product, I'll just mention it here in the comment section. Deleting. So for deleting a product, it should be appate app.de products and then the product ID. So again just then just mention defaf delete underscore product id colon product id is less than len products colon then we'll insert this function which is product pop product id and we'll give the message return and then we'll give the message as return uh product deleted successfully. One more statement will return which is return and bracket message product not found. So here in this particular decorator which we have mentioned here which is app.de delete. So this decorator will list in all the delete requests at products product ID. Now if the product ID exists in the list, the product is removed using the pop function. The API will respond with a success message as an error message if the product was not found. So now that we have seen how we use the crude functions to create a product, delete a product, update a product. Now it's time to see how to handle error and validation using pentic search. So now it's time to see how to handle error and validation using pyic. Now pyic help us to define data models and validate the data that's passed to our API. Fast API automatically checks if the data matches the model and raises an error if it doesn't. Let me share full code with piantic validation. So again from here I'll create one more file and name it piantic dot py. Now from here we have to import our files from pyantic. import base model. Next from first API import first API. Okay. From typing import list app is equals to fast API. So now we'll say for inmemory storage for products. Now we'll just put a comment here. In memory storage for products and then we'll keep products and with a bracket. So again I'll comment it here. Pantic model for the product. Okay. So we'll keep the name as string description string price float and the quantity as integer. So again I'll just accept this tab. Now I'll show you how to create a product using pentic. So again I'll put this hashtag and we'll do it for creating a product post and we'll keep it as the products then dev create product. All right, we'll dev create underscore product. We'll keep the product name. Then we'll put put products dot append product. Okay. And then we'll return this message as product added successfully. Product product. Now we'll get all the products from here. So again I'll add a comment and it says get all the products. Again I'll put this app get products response model list and I'll name it product. Then here again I'll add dev get products and then I'll return product and the products. So now let me explain uh this code to you. So here we have the product pantic model. Product base model as you can see this function here. This defines the schema for a product. It ensures that the name, description, price and the quantity fields are provided and that they match the correct data types. The correct data types are string, float and integer. Now we'll see how we use the model in our API by using the post product slash. The product data now comes in as a product object and fast API will automatically validate the data against the model. Now if the data is invalid for example sending a string for price fast API will return a 400 bad request error with the details of all the validation failure. So we have seen how this error handling works. So guys after setting up the fast API backend it's time to connect it to the front end such as the react or Vue.js. Now for this we need to ensure the cause is handled correctly so that the front end and the back end can communicate seamlessly. Now if you don't know what cause is I have already explained it before earlier. So you can just play back and see what cause actually means. So fast API allows easy course integration using the course middleware where you can configure and allow cross origin requests between your front end and back end. And this ensures smooth communication between different parts of your application. So let's look at the code now. We're going to create one more file from here. Co py. So we'll type from fast API. Again we're going to import this fast API. From fast API dot middleware dot course import course middleware. Then we'll type app is equals to fast API. We'll give origins. So we have given the origins as the host the local host which is the 8,000 port. Next after this just type app dot add middleware course middleware allow origins equals to origins allow credentials true and then allow the methods allow the headers. I'll also show you how you can call your fast API back end from the react front end. So just keep it um front end API calls example fetch sorry fetch the host from here the local port /roucts dot then response is equals to response JSON and then dot then data is equals to console.log data. Okay. Now this simple fetch request calls the get products routt on your fast API back end and it logs the respond in the browser console. Now we'll move on and see how we can connect to a database. So here we have taken the example of post SQL. You can use any other database if you want to. So again here we'll be creating one more file database py. So guys, now that we have our crude operations, it's time to connect to Postgra SQL database for persistent storage. Now we need to install Postgra SQL in our terminal. For that, we have a command ready. Just again go to our terminal section and just enlarge this a bit. And then from here uh just type this command pip install psy cop pg2. Just enter this command in your terminal. So this will actually install your postgra SQL and psycho pg2. So just wait for a few seconds till it's installed. Well, I think a new release of pip is available this time to update already. We don't have to update it now and it has already installed all our packages. The psychopg2 and it has also installed postsql. So guys uh since we have installed this thing, let's move on to our code. We'll go to our uh database py. All right, we'll close this terminal from here and then here we'll type import c o pg2. Okay, since we have imported this now we'll connect it to the postgraql database. So I'll just add it in a uh comment connect to post SQL database. We give the code as con is equals to dot connect and then now you can enter your database name. So I'll keep the DP name is equals to inventory DP. All right. User is equals to keep it as user itself. Comma. Password is equals to we'll keep it as password. Host we'll keep it as local host. Port will keep it as 5432. Then we type cursor is equals to con dot cursor. We're connecting it. Now next we'll see how we can uh create a table. Now to create a table here you can give the code create table if not exist product and just enter the ID serial primary key name as VCAD description text price float quantity conomit and cursor dot close and after this you have to just enter corn dot close. So uh here as you can see the psychopg2.connect connect function. This will establish the connection to your Postgress SQL database using the provided credentials which is the database name, your user password host and the port. Then we have the cursor.execute function. This will execute the SQL query to create a product table if it doesn't exist here. Convention, it will save any changes made to the database such as creating the table. And [clears throat] talking about cursor close function and con.t close it will close the database cursor and the connection. So this is how you can connect your database to Postgress SQL. Next step we'll see how you can test the API. Now to test the API you can use swagger UI which is automatically provided by fast API. You can run the app using the uicorn. Now let me show you which uh command you need to enter. So again we'll just go back to the terminal. Now let's start the fast API server. You can run this command uh using the uicon. [snorts] The command is uicon main app reload and this will start your fast API server. So right now it's saying error loading esgi. It could not import the module main from here. Now you will also see the roots like post, get, put, delete for managing the products and you can also access another view of the documentation by using the different port number. So we can also use GitHub copilot for this purpose. We'll just say GitHub copilot to create a root to get a product by ID. So as you can see copilot says I'll add a get true to the project's fast API py the file which we created that returns a product by ID using this particular database helper then I will update the to-do list. All right it says done. I've got uh get root. All right. So here's how you can test quickly. So it says you can start the inventory app if it's not running by giving this command. If you prefer I can start the server. Okay. I'll just say it to start the server. So guys, from here you can just uh live your u code whatever we were working on and you can just have a look at the um the directories which we created the virtual environments and everything. You can just see this fast API function from here the scripts and all. So there might be some there was some error before but right now you can see that we have included all our databases here. We have connected it to fast API. We have seen how we connected to Postgres SQL database. So you can just have a look at all the uh back end the database just from here. I think it's taking the GitHub group. I'll just taking more time to complete this manually. You can just code it and see how it works. So guys in this tutorial you have created a fast API app with crude operations. We have integrated pyantic for data validation. We have connected to Postgra SQL database for persistent storage. We have used GitHub copilot to speed up development and improve code quality. We also tested the API using fast API's interactive documentation. So guys, if you want the code for this fast API backend, I'll just attach the GitHub repository in the description box below. And with that, we have come to the end of our tutorial. If you have any doubts or questions, you can ask them in the comment section below. Our team of experts will reply to you as soon as possible. Thank you and keep learning with simply learn.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Simplilearn · Simplilearn · 0 of 60

← Previous Next →
1 Ethical Hacking Full Course 2026 | Ethical Hacking Course for Beginners | Simplilearn
Ethical Hacking Full Course 2026 | Ethical Hacking Course for Beginners | Simplilearn
Simplilearn
2 AWS Full Course 2026 | AWS Cloud Computing Tutorial for Beginners | AWS Training | Simplilearn
AWS Full Course 2026 | AWS Cloud Computing Tutorial for Beginners | AWS Training | Simplilearn
Simplilearn
3 Data Structures And Algorithms Full Course | Data Structures and Algorithms Tutorial | Simplilearn
Data Structures And Algorithms Full Course | Data Structures and Algorithms Tutorial | Simplilearn
Simplilearn
4 SQL Full Course 2026 | SQL Tutorial for Beginners | SQL Beginner to Advanced Training | Simplilearn
SQL Full Course 2026 | SQL Tutorial for Beginners | SQL Beginner to Advanced Training | Simplilearn
Simplilearn
5 Microsoft Azure Full Course 2026  | Azure Tutorial for Beginners | Azure Training | Simplilearn
Microsoft Azure Full Course 2026 | Azure Tutorial for Beginners | Azure Training | Simplilearn
Simplilearn
6 Shopify Tutorial For Beginners 2026 | Shopify Course | shopify dropshipping | Simplilearn
Shopify Tutorial For Beginners 2026 | Shopify Course | shopify dropshipping | Simplilearn
Simplilearn
7 Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Simplilearn
8 🔥Feeling Stuck? How Upskilling Can Boost Your Career! #shorts #simplilearn
🔥Feeling Stuck? How Upskilling Can Boost Your Career! #shorts #simplilearn
Simplilearn
9 Growth Hacking In Marketing | Learn Growth Hacking Marketing Strategies | Simplilearn
Growth Hacking In Marketing | Learn Growth Hacking Marketing Strategies | Simplilearn
Simplilearn
10 🔥Cracked 3 Job Offers with One AIML Course! | 20–30% Salary Hike #shorts #simplilearn
🔥Cracked 3 Job Offers with One AIML Course! | 20–30% Salary Hike #shorts #simplilearn
Simplilearn
11 Top 10 Must-Have Figma Plugins for UI/UX Designers in 2026 | Figma Plugins | Simplilearn
Top 10 Must-Have Figma Plugins for UI/UX Designers in 2026 | Figma Plugins | Simplilearn
Simplilearn
12 Business Analytics Full Course 2026 | Business Analytics Tutorial For Beginners | Simplilearn
Business Analytics Full Course 2026 | Business Analytics Tutorial For Beginners | Simplilearn
Simplilearn
13 Simplilearn Reviews | Getting future-ready with course in Artificial Intelligence | Roopam’s story
Simplilearn Reviews | Getting future-ready with course in Artificial Intelligence | Roopam’s story
Simplilearn
14 Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
15 Full Stack Developer Course 2026 | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Full Stack Developer Course 2026 | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Simplilearn
16 Simplilearn Reviews | How David Went From Seasoned Engineer to AI Innovator #GetCertifiedGetAhead
Simplilearn Reviews | How David Went From Seasoned Engineer to AI Innovator #GetCertifiedGetAhead
Simplilearn
17 Complete Social Media Marketing Strategy for 2026 | Social Media Marketing Strategy | Simplilearn
Complete Social Media Marketing Strategy for 2026 | Social Media Marketing Strategy | Simplilearn
Simplilearn
18 🔥Top 4 Cybersecurity Certifications You Need! #simplilearn #shorts
🔥Top 4 Cybersecurity Certifications You Need! #simplilearn #shorts
Simplilearn
19 🔥Cloud Engineer Salary in India 2026 | City-Wise Breakdown #shorts #simplilearn
🔥Cloud Engineer Salary in India 2026 | City-Wise Breakdown #shorts #simplilearn
Simplilearn
20 Digital Marketing Full Course 2026 | Digital Marketing Tutorial For Beginners | Simplilearn
Digital Marketing Full Course 2026 | Digital Marketing Tutorial For Beginners | Simplilearn
Simplilearn
21 Full Stack Java Developer Course | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Full Stack Java Developer Course | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Simplilearn
22 Social Media Marketing Full Course | Social Media Marketing Tutorial For Beginners | Simplilearn
Social Media Marketing Full Course | Social Media Marketing Tutorial For Beginners | Simplilearn
Simplilearn
23 How To Create LLM Chatbot Demo 2026 | Build a LLM Chatbot From Scratch | Simplilearn
How To Create LLM Chatbot Demo 2026 | Build a LLM Chatbot From Scratch | Simplilearn
Simplilearn
24 Digital Supply Chain Management Certification | Supply Chain Management Course | Simplilearn
Digital Supply Chain Management Certification | Supply Chain Management Course | Simplilearn
Simplilearn
25 AI Agents Full Course 2026 | AI Agents Tutorial for Beginners | How to Build AI Agents | Simplilearn
AI Agents Full Course 2026 | AI Agents Tutorial for Beginners | How to Build AI Agents | Simplilearn
Simplilearn
26 ITIL Full Course 2026 | ITIL 4 Foundation Course | ITIL Tutorial For Beginners | Simplilearn
ITIL Full Course 2026 | ITIL 4 Foundation Course | ITIL Tutorial For Beginners | Simplilearn
Simplilearn
27 Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
28 ITIL Full Course 2026 | ITIL 4 Foundation Course | ITIL Tutorial For Beginners | Simplilearn
ITIL Full Course 2026 | ITIL 4 Foundation Course | ITIL Tutorial For Beginners | Simplilearn
Simplilearn
29 Simplilearn Reviews | Integrating AI & Music | Diego's Story
Simplilearn Reviews | Integrating AI & Music | Diego's Story
Simplilearn
30 Digital Marketing Full Course 2026 | Digital Marketing Tutorial For Beginners | Simplilearn
Digital Marketing Full Course 2026 | Digital Marketing Tutorial For Beginners | Simplilearn
Simplilearn
31 SEO Full Course 2026 | SEO Tutorial for Beginners | SEO Training | SEO Explained | Simplilearn
SEO Full Course 2026 | SEO Tutorial for Beginners | SEO Training | SEO Explained | Simplilearn
Simplilearn
32 PMP Vs CAPM: Which Certification Should You Choose? | PMP Vs CAPM | Simplilearn
PMP Vs CAPM: Which Certification Should You Choose? | PMP Vs CAPM | Simplilearn
Simplilearn
33 Complete Data Analyst Roadmap 2026 | How To Become A Data Analayst In 2026 | Simplilearn
Complete Data Analyst Roadmap 2026 | How To Become A Data Analayst In 2026 | Simplilearn
Simplilearn
34 Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
35 🔥5 Jobs That Are Most Likely Safe from Layoffs in Today’s Market #shorts #simplilearn
🔥5 Jobs That Are Most Likely Safe from Layoffs in Today’s Market #shorts #simplilearn
Simplilearn
36 🔥Git vs GitHub – What's the Difference?
🔥Git vs GitHub – What's the Difference?
Simplilearn
37 What Goes Behind Building the Likes of Uber and Netflix? | Product Management Tutorial | Simplilearn
What Goes Behind Building the Likes of Uber and Netflix? | Product Management Tutorial | Simplilearn
Simplilearn
38 AI Agents Full Course 2026 | AI Agents Tutorial for Beginners | How to Build AI Agents | Simplilearn
AI Agents Full Course 2026 | AI Agents Tutorial for Beginners | How to Build AI Agents | Simplilearn
Simplilearn
39 Full Stack Developer Course 2026 | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Full Stack Developer Course 2026 | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Simplilearn
40 Product Life Cycle 2025 | Stages Of Product Life Cycle | Product Life Cycle Tutorial | Simplilearn
Product Life Cycle 2025 | Stages Of Product Life Cycle | Product Life Cycle Tutorial | Simplilearn
Simplilearn
41 Project Management Full Course 2026 | Project Management Tutorial | PMP Course | Simplilearn
Project Management Full Course 2026 | Project Management Tutorial | PMP Course | Simplilearn
Simplilearn
42 PCB Design Course 2025 | PCB Designing Explained | How To Make PCBs | Simplilearn
PCB Design Course 2025 | PCB Designing Explained | How To Make PCBs | Simplilearn
Simplilearn
43 Python Full Course 2026 | Python Data Analytics Tutorial For Beginners | Simplilearn
Python Full Course 2026 | Python Data Analytics Tutorial For Beginners | Simplilearn
Simplilearn
44 🔥Top Product Management Skills You Need to Succeed in 2026 #shorts #simplilearn
🔥Top Product Management Skills You Need to Succeed in 2026 #shorts #simplilearn
Simplilearn
45 SQL For Data Analytics 2026 | Essential SQL Commands | SQL Tutorial For Beginners | Simplilearn
SQL For Data Analytics 2026 | Essential SQL Commands | SQL Tutorial For Beginners | Simplilearn
Simplilearn
46 Simplilearn Reviews | Paving Way To Success With AI & ML Course | Soumik’s Upskilling Journey
Simplilearn Reviews | Paving Way To Success With AI & ML Course | Soumik’s Upskilling Journey
Simplilearn
47 Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Simplilearn
48 Learn Snowflake In 45 Mins | Snowflake Tutorial | What Is Snowflake | Snowflake Explained
Learn Snowflake In 45 Mins | Snowflake Tutorial | What Is Snowflake | Snowflake Explained
Simplilearn
49 🔥ML Career Tip – How to Start Learning Machine Learning in 60 Seconds! #shorts#simplilearn
🔥ML Career Tip – How to Start Learning Machine Learning in 60 Seconds! #shorts#simplilearn
Simplilearn
50 🔥Agile vs Waterfall in 60 Seconds #shorts #simplilearn
🔥Agile vs Waterfall in 60 Seconds #shorts #simplilearn
Simplilearn
51 Excel Full Course 2026 | Excel Tutorial For Beginners | Microsoft Excel Course | Simplilearn
Excel Full Course 2026 | Excel Tutorial For Beginners | Microsoft Excel Course | Simplilearn
Simplilearn
52 What Are AI Agents? | Types Of AI Agents | AI Agents Explained | AI Agents Tutorial | Simplilearn
What Are AI Agents? | Types Of AI Agents | AI Agents Explained | AI Agents Tutorial | Simplilearn
Simplilearn
53 How To Create a Product Roadmap In 2026 | Product Roadmap | What Is Product Roadmap | Simplilearn
How To Create a Product Roadmap In 2026 | Product Roadmap | What Is Product Roadmap | Simplilearn
Simplilearn
54 SQL Full Course 2026 | SQL Tutorial for Beginners | SQL Beginner to Advanced Training | Simplilearn
SQL Full Course 2026 | SQL Tutorial for Beginners | SQL Beginner to Advanced Training | Simplilearn
Simplilearn
55 🔥What Is Phishing? #shorts #simplilearn
🔥What Is Phishing? #shorts #simplilearn
Simplilearn
56 Cloud Computing Full Course 2026 | Cloud Computing Tutorial | Cloud Computing Course | Simplilearn
Cloud Computing Full Course 2026 | Cloud Computing Tutorial | Cloud Computing Course | Simplilearn
Simplilearn
57 Simplilearn Reviews | Overcoming Rejection & career plateau to finding a New Job : Bhaskar Banerji
Simplilearn Reviews | Overcoming Rejection & career plateau to finding a New Job : Bhaskar Banerji
Simplilearn
58 Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Simplilearn
59 Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
60 VLSI Design Course 2026 | VLSI Tutorial For Beginners | VLSI Physical Design | Simplilearn
VLSI Design Course 2026 | VLSI Tutorial For Beginners | VLSI Physical Design | Simplilearn
Simplilearn

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →