How To Integrate FastAPI With Supabase | Mastering Integration: FastAPI Meets Supabase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to the channel so in today's video I'm going to show you how to use how to integrate super base swi into Firebase so super base is more like Firebase but now it's just a different kind of Firebase let me just say it like that so super base basically provides you with a cloud database so it helps you to set up applications and get your applications running very quickly so basically you can have a a database storage in the cloud you can can provide with buet you can store different images different files and other stuff like that also provide things like authentication and just makes development really really simp simple so in this tutor I'm going to show you how to build a simple crow API using uh super base basically you're going to be able to store store stuff in the super base database retrieve stuff update stuff delete stuff and all that cred functionality with super base you're going to do this all with Firebase so we going to integrate superbase which is a database we're going to be using into Firebase so superbase is a cloud database we're going to integrate into our fast API API basically our first API application so uh let's get started with it so I'm going to go ahead and create a file right here so I'm going to go ahead and create a file and I'm going to call this file main.py so main.py so let me actually let me just before I even start this let me just go ahead and show you what you're going to be building so go back in here so I can just going to be a simple to do just for you to get the basic idea of C application if you guys want me to build something more complicated that includes authentication uh and as St different files let me know in the comment section below I do my best to create that video okay so let me just click on execute and it's going to go ahead and retrieve all the toos inside of our uh our super base app basically our super base database and you can see have it right here all the to-dos you can delete it to if you want let me just copy this todos uh U ID or basically the ID I'm go and copy it and I can just go and minimize I can go into um to-do get to-do and here you can get a specific to-do so I'm going to pass in the U ID here and then press enter and that's going to return to me this specific to from from Super based database so you can see in the ID and we get that specific to we can also delete a too so you can pass actually let's try to yeah try to delete a too so you go and just say uh try it out and simply paste it in here paste this right here and click on enter that's going to go ahead and delete that to do so you get back for 204 which shows the content has been deleted so if I go back in here and press uh execute now you should get back only one to because the other one has been deleted so that's basically how you can work you can also create a new Tod do if you want to you can say try it out and you can fill in the details say uh tutorial tutorial uh test and I'm then say tutorial test uh this let me get spell my test uh test description so right then I'm going to go ahead and click on execute and that's going to go ahead and create insert that to do into our super uh super super past database so if I go back in here and click on execute now you should be able to see that to-do right here so we can see the to-do right here has been created which is this to-do right here and it has been ordered basing on the date right so yeah that's basically it how you can create a simple to-do and now one of the thing you can you're missing in the CR St is the update functionality and I'm not going to I'm not going to go ahead and show you this upd the update functionality I'm going to leave that to as a challenge for you guys to go ahead and try to do that on your own so you can learn a super base and how you can work with it in Firebase if you guys have any challenge again you can let me know I'll create more videos on it okay so let's get start I'm going goe and create a simple file here I'm going to call this file main.py so instead of main. P main. main.py file first of all let's go ahead and install a couple of libraries before we even get started so I'm going to go back in here and I'm going to go go ahead and create a python virtual environment so I'm going to Simply say Python 3 hyen m VNV I'm going to call my virtual environment VNV and that's going to go ahead and create for me a python virtual environment so if I go once this is done it can take a bit of time so let's give it a bit of time and once this is done we can go ahead and actually install the libraries that we need so we need a couple of libraries we need first API so we can just go ahead and start getting that's if I go to First API uh here so go to first uh my computer is a b let make sure that it's in performance mode so let me just shift that into performance say first API docs just like that and let me run that so now a bit fast because it's in performance mode uh okay so once I have that I'm going to go ahead and go to documentation so you can just go ahead and search for fast api. do.com and from there you can go ahead and get the fast API official documentation so we need to install a couple of things and the instructions are right here so we need to install fast API just like this so install fast AP API so I'm going to copy that and I'm going to go back in here and once our virtual environment is created we can see have a folder called VNV which is the name that we give our virtual environment in here okay you can give it any name if you want okay so once I have that I'm going to gohe and activate my virtual environment okay so I'm going to Simply say source and then VNV for sorry uh Source VNV for SLB for/ activate and that's going to go ahead and activate my virtual environment right here and you can see it's activated if you can see any of the virtual environment in front of the line within the parenthesis okay so once I have that I'm going to go ahead and now install that fast AP I'm going to copy and paste that command that I copied and it's going to go ahead and pep install fast API on that virtual environment okay so let's go back in we also need to install uon which is a Ser that we need to use to run our first API application so I'm going to copy that and wait wait for this to install once inst going to install uh okay clear that and I'm going to install uvon and press enter okay so P install code uvon standards square bracket standards and then close the codes okay so that's going to go ahead and install that for us I'm also going to be using super base python Library so you can go ahead and find it right here I'm going to leave all this in the description of the video so you guys can have access to all this good so now you can see we have now we have successfully installed our uvon so now let me just go ahead and let's begin to install uh you need to install firear sorry fire base sorry not Firebase uh super base as well so this is a command to install super base so P install and then Super B so just going to copy that command and go back in here and just simply CLE the terminal and paste that in here and that's going to go ahead and install super base okay which isbr that we need to interact with the super base database that you're going to create online okay so once we have that done we also I'm also going to install one more library to call python decou for us to read our virtual environment variable so I'm going to say sorry pip uh pip three install uh python I decouple just like that and then install that so once python decouple is installed uh we are good to go so once I have this done what I need to go ahead and do is now we need to go ahead and get our our API keys and then use to create a a simple super based project get the API keys and the uh URL and then simply connect to it so let's just go that let's go ahead and do that so I'm going to go ahead and create a file in here I'm going to call this file EnV okay so that's basically it so now once we have actually let's write the simple first AP application before before we add super base to I'm going to say uh from first uh API so my bad so I'm going to write this code instead of my main. pile say uh from uh fast API I'm going to go ahead and import uh fast API okay so it doesn't pick up the library so let me just go ahead and try to change my interpreter so let me just change my interpreter right here so my interpret I want to use is not this one right here uh it doesn't sure for some reason okay so let me see yeah uh if I'm using the right interpreter that should work okay now that's let us pick up that interpret let's go back in here and let's simply say from first API I'm going to go I'm going from Fast AP I'm going to import fast AP I'm also going to import something called okay let's just import fast API for now and once I have that import I'm going to go ahead and simply create an fast API app which going be called Fast API and just instantiate that so once I have I'm going to go create a function I'm going say get or let's say let's call it test and I'm going to Simply this uh the is going to be for Slash and I'm going to go down here and then up filling the content I to Simply say return I'm going to in Python dictionary say message and then message going to be a simple hello world just like that so once we have that done uh we are good to go okay so uh it doesn't seem to pick this up I don't know why okay so let me just leave it to be empty for now okay and let's go ahead and try this out and let's see if it's going to work so I'm going to go into my terminal and then clear that and I'm going to go into project Dory and simply run I need to say uvon so U uh uon Main and then app Hy hyen reload to run that server so we got an error it says okay because I'm running the older ones which I used to show the demo so I'm going to close that up and then clear this and then run this again because I was using that Port that's why I didn't open up so once once you have it open up I'm going to go back into my browser and then simply uh type in this so Local Host at P 8,000 B which is basically what you can get here you simply copy this from right here or you can simply click on it control and C control uh control C and then click on this link as it says right here or you can simply copy this and paste it inside of your browser of your choice and you can go to for/ do so if you P if you open that link it should look like this right so now what you want to do is just say for/ docs and it should bring you to the official documentation which is this uh open AI uh sorry open API templating format okay so once we have this done now let's go ahead and create a Firebase uh Firebase API I actually have an an issue right here it's not showing this route right here and the why you need to go and say at and say up and then do route uh sorry up do be app.get and then simply say for slash basically like that so that's error I forgot okay so to say up this python decorator and if you know the decorator I have a video on decorators on my channel so decorator is t a as an input it returns a coll an output that's Bally how you can think of a decorator as but it's it's much more comp complicated that complicated than that and I have a video on decorators on the channel so you can check that out so this have semi pass decorator that's going to make this first API rout so if I go back in here and I refresh this now we should now see a fast API route it says test and if I goad and click this click this we just get back a message it says hello world which is this dictionary that you return here right here okay so that's how to set up a simple route in fast API good so once you have that now let's go ahead and create a fire super base project and have everything set up so let's go create an account and then see me pass create a project so create an account once you have the account you can go ahead and um say new project right here and then you can go ahead and fill in the project let me say uh C API okay uh API just like that and I'm simp going to go ahead and select the passing the password so I'm simply say passing the pass password okay so I'm going to type in a random password okay that password is not strong enough I hope okay let me just simply do this way okay so I hope that password is not strong enough still so uh I don't know let me just see now let me just uh type this out again I'm just trying to generate a random password uh okay that's still not strong okay that's strong now okay I hope I remember that password because I really can't remember that password I just generated right now okay but basically that's it and you're going to give it a bit of time to create for us the super base project and once that project is created we have the project details right here that you can use to uh log into our application so now we need to go ahead and get the URL and finally we also need to go ahead and get this other key so let's copy the URL which is this one right here and I'm going to go back in here uh into my basically myv file and I'm going to create create a key right here say super base and this going be URL and I'm going to say equals to and paste in that URL okay so I'm going to delete all this after the video so I don't think you can you can use M okay so I'm going to delete all of it as well so I'm going to go ahead and reveal this and simply copy this this is my um my API the access key so make sure that you you you copy this one and not the wt1 or not the a non one okay so make sure you copy the right one so once I have that copied I'm going to go back in here and then simply say uh Super basore Key and then equals to and paste in the key that I just copied once I have done I'm going to go ahead and close my EnV okay good so once we have that done how do you read that into our python uh application so to read that into our python application what you're going to Simply do is the following so uh what I'm going to go ahead and do instead to read this I'm going to go ahead and import the C so I'm going to say from okay let me just make sure I turn off my cup key so from uh decouple I'm going to go ahead and import config so config will help us read the value also going to import uh import or going to say this as an environment okay we don't actually need the OS for now okay you don't need the OS so just say from the couple I'm going to go ahead and import config so for some reason it's giving me an error I can find this I don't know why that's the case so I'm going to go ahead and make sure I have the will properly install so close this and simply say uh pip install uh python I de couple okay I think we install this Library uh yeah it's already satisfied so I don't know why you can't uh pick it up okay so sometimes if happens like this I'm just going to close my vs my vs code and have it open again and that era should go away hopefully so let me just go ahead and close this and have it open again and I'll be back recording okay guys so I just have that my code reseted and opened up so now let's go ahead and and continue from what we left so when to go ahead and get the real the read the URL from our basically our super Bas URL from EnV F so I'm say URL basically URL is going to be equals to it's going to say config and then fin I need to go ahead and open up this in here and I'm going to go ahead and get the the the key name that you use which you call it uh super Bas so I'm going to copy that and paste it in here so I can close this as well and I'm also going to go ahead and also so let me just duplicate this so copy this and paste again and now you're going to call this one this going to be the the key so the key and I'm going to go ahead and get the the value the for the key so copy this and go ahead and paste it in here okay so once you have that done you have read the we have read both of of the two but basically the URL and also the the the key okay so once you have that done let's go ahead and simply write the uh code to connect to our so once you have that done you're good to go so now let's go ahead and write the code to connect to our super base datab B so I'm going to go ahead and simply say from super uh from super base I'm going to go ahead and import Cent uh CL create Cent right here I'm going to import that so once I have create C I'm going to go ahead down here and create a c so I go ahead and keep it below the the the first API instan instantiation so I'm going to go ahead and say super super base and then super base going to be equals to and I'm going to go ahead and say super base and this going to be of type c so you can also you can also use type P here if you want to so I'm just going to use typee hint because that's recommended in the documentation so say of type it's going to be of type Cent uh Cent and it's going to be equals to create centore Cent pass in the URL and also the key so when pass in the URL and the key that's all we need to do so you have created a Cent and you have passing the URL which is what you read from here from the EMV file and you passing the key which is what you read from the Ile so that's going to create for us a super based uh uh super based client that you can can connect to so once we have that now we need to go ahead and add in a table okay so we need to access a table from our super base project so now let's go and say go ahead and click on tables right here or you can also go back in here and you can click on edit table so whatever works for you so I'm going to go in here and say add a new table so to add a new table call this table todos right you're going to call it todos and just say uh to do uh C API just like that and and finally once you have that you can go ahead and add in the keys that you want so I'm going to leave this to be an instance you can also use a u uid if you want to I'm also going to leave leave created art I'm also going to add a new column this column is going to be this primary key by the way so the ID is going to be the primary key and also we can go ahead and also add in the following I also want to add in the title of the to-do and this is going to be of type text so I'm going to use text right here uh text text text right here okay I'm also going to add another one uh which is going to be called description just description of the to-do that you're creating right now this also going to be of type text so scroll down here and find text and then use text right here so you're going to use title and the description created date and ID so that's all I want and the ID is going to be the primary key so that's all I want for the table is going to be very simple table so create on Save click on Save and that's going to go ahead and create for us the table right here and the table name is Todo so make sure remember the name of the table which is called to do you're going to use this to connect to this specific table okay so now once we have that done we are good to go so now the table is go ahead and uh it's going to go go ahead and create the table and you can see you have no roles whatsoever so now let's go ahead and actually write the application to be able to add stuff into it okay first let's go ahead and you can also write stuff uh you can go ahead and say insert a row here and go ahead and manually insert a row so ID is going to be one and the date you can go ahead and choose any date you want so this is this this this and this just any random date title going to be the first to do and then say first to do uh description just like that and once you have that you can click on Save and that's going to go ahead and save the date save this so you can see now we have the fast row right here so now let's try the uh fast AP application to connect to this database and retrieve the fast row information so how do we do that now we go back in here and we can gohe and create another route if you want to so you can actually delete this route if you want to let me just get rid of it or let me just modify it a bit I'm going to Simply call this be uh toos so call uh todos for slash okay then you're going to to change the name to be called getor to-dos meanjust all the to-dos and what you're going to Simply do is let me just go in here going to Simply say todos it's going to be equals to uh super base which is what we the instance instantiation of the super base client right here dot uh sorry Dot and I'm going to Simply say dot table uh which table do you want to connect to we want to connect to our table called to todos right we call our tables todos so yeah so let connect to our to-do table which is right here to-dos okay so connect to our to-do table to do right here you're going to say dot and then finally pass in what you want to do so say select select everything so what do you want to select you want select everything okay just like basically normal SQL you pass in AR meaning all rows going to execute this query so execute and that's going to execute the query and run the query and retrieve for us everything from the Todo table one once you have that you can see go ahead and return that to do Todo so say todos right here so once I have that done I'm just going to go ahead and test it out so we have our swaga UI running right here I refresh this okay so I sto that API so let's go ahead and run it again so I'm going to Simply say uh uvon main app then Hy hyen reload to just to reload every time you make changes the reload part is not the reload flag is not important but you can keep it just to automatically start the application time you make changes so we have this right here and you can go ahead and click on try and if you click execute that should return to us the to do which is basically we are fast to do fast to do description which is exactly what we have inside of our uh super base uh database right there so good everything works fine so now let's go ahead and write the the the route to be able to retrieve a specific to-do basically passing the ID of the to-do and returns that specific to do okay so let me just copy this right here and we're going to modify it soe this right here and you're going to Simply say uh along with this you also need the ID of the to that you want to receive and the ID is going to be of type an integer right there and then finally I want to change this to be too to do not todos okay so the function name needs to be different so this is todos and this one is to do okay and this one is to do and not to do and let me also change it right here okay so now for the retrieval what you want to do you want to say super base. table do toos because of the table do select and then you're going to add in another we're going to say dot equal so matching two things we're going to say equal we're going say ID is the ID column is going to be equals to the ID that you receive from the user then fin you execute that query so selecting every every row that much in which this uh ID colum has this ID details that the user pass in is going to return it to us so that's basically it so once we have that done our API should automatically reset due to the reload flag and if I refresh this if you if you didn't use the reload flag make sure that you restart your API manually again so try this out and I'm going to pass in ID of one and that should return to us the to-do with an ID of one which is the to-do the first to-do from our our super based AP super based Cloud database you can see right here we have the first Tod do being returned great now let's go ahead and write the function to be able to uh delete a to-do okay so okay let's write the function to be able to uh create a too instead of delete one so let's write the function to be able to create a to-do instead of delete so I'm going to go ahead and simply say at app do uh this going to be a post request so post and you're going to go ahead and send it to do for slash okay and then going to pass in the status uh the status code if in case everything went went well so status code is going to be cost to I'm going to go up here and I'm going to import uh status from first API so I'm going to go down here and simply say uh status uh basically status Dot and then I'm going say HTTP and this going to be uh two 20 which say for created okay so once you have that I'm going to go and say F uh and I'm going to create a python function right I'm to call it create uh create uncore too just like this and what we need to create the to-do we need to pass in let me just pass this for now we need to pass in the too ID and the to-do details okay so the ID we canate a random ID right so I'm going to Simply say ID is going to be equals to Let's import random so I'm going to say uh import random so we say from random okay let me just import random okay so I'm just going to go ahead and import uh random right here okay so once I have a random import I'm going to do go ahead and generate a random uh dot this going to be Rand uh Rand in so a random integer and you're going to pass in the range of we want the Val to be so just pass in this ridiculously large number and once we have that you're going to go ahead and use that as the ID of the thing of the to-do so now the user need to pass in the description of the to-do and the description we need the description the to be automatically set so you need a description of the to-do also you need the title of the to-do so now let's let's create a schema that you can the user can use to uh pass this information in so basically let's go ahead and import uh the schema from right here so say from uh p pantic uh pantic I'm going to import uh base model okay so good so once you have that you're going to go ahead and use that base model to create a schema that uh you're going to pass to to our our response okay that is I need to pass in okay I to call it to do schema and this schema is going to inherit from the base model that you just imported from ptic and what you need to do here is you need to pass in the title and the title needs to be a python string also need to pass in the description this also needs to be a string so that's all we need to do so need need to pass in a to-do and a to-do needs to be of type uh to-do uh description so so sorry to do uh schema okay so to do uh schema yeah I call it schema so right there so to do schema so that to do is going to be of type to-do schema and what you're going to do they do we have the ID already so now we going to go ahead and generate that to-do so to go down here and simply say Tod do is going to be equals to uh super base uh super base do table and the table that you want to insert into going to be called to do the table from here you can see it's called to do so that's the table that you're looking after looking looking for to do and then say do insert and in here you need to pass in a python dictionary and then when pass in we need to say the ID and the ID is going to be equals to the ID that was we just created above so actually this need to be a string python string right here ID and the ID needs to be the ID you just generated above there you need to pass in the the title so title and the title needs to be there to do which the user passing you're going to get the title part of it out so that's title just like that also you need to go ahead and pass in the following so you also need to pass in the the description so description description is going to be uh too do uh description okay so I hope I got that spelling right your description right there so description right there okay so that's all we need to do so we passing the ID the description and then the title the date to be automatically figured out in the back end on our cloud cloud storage okay so once we have that you're going to Simply return you're going to return the to-do that you just created so that's all we need to do so that's all is required so I'm going to go ahead and now once you have this code you haven't really executed it so going to say execute uh okay and then basically execute this query that's going to go ahead and insert data into our cloud storage so that's basically the code right here so yeah that's all we need to do so let's go ahead and actually test this out and make sure it's working so make sure your application is running and you have enabled the reload flag if not then make sure you stop it and again rerun it again okay so once you have this right here I'm going to go ahe and click on try it out and I'm going to pass in this going to be let's say second to do and let's say a second to do description right there and I just simply execute this and that's going to go ahead and insert that into our database and it's going to return to us it back right here if you go back in here and let's try to like you can see we have it right here the second too has also been in been inserted into uh the cloud storage right here so good that's that's how you can insert so now let's go ahead and move on to how do you delete something from the database so to delete something from the database actually very simple what you need to do is get the ID of what you want to delete and then use the ID to delete that specific information so now let's go ahead and create it right here so I'm going to Simply say uh create the D creator app do this going to be delete delete route and it's going to be for/ toos for/ and pass in the ID of that specific to-do and once we have that you're going to say Def and you're going to simply say Def and say delete underscore too and you're going to pass in the ID and the ID is going to be a python uh string okay so once you have say to- do is going to be equals to uh super base do uh table going to get the to-do table and you're going to simp say dot uh Delete and then delete you're going to go ahe and pass in uh just par that and pass in the equal the equal operator and pass in you're going to look for an ID an ID that is matches the idea the user pass in and once you have that you're going to go ah and execute uh this query to delete that and once you have return uh going to a status code of everything it's okay so we're going to a status code of uh let's say status and I'm going to say dot okay let me just return a dictionary or message saying here message saying uh let me just say deleted so deleted okay so let me just uh close this up I think I the wrong key so deleted just like that and now you're going to them to us you can specify the the the status code of the response stat code be equals to basically status do HTTP you're going to return a 204 of no content form because you have deleted the content right so that's basically it so once we have this done uh let's go ahead and test this out so make sure that your API is running and if it's not make sure you have reload or stop it and run it again okay so once you have that now let's try to delete this specific uh one a second to do so I'm just going to go ahead and copy uh copy s content and I'm going to go here and simply uh say try this out and then simply paste the S content in here and then execute this now that should go ahead and delete everything and and uh we get back a string okay so that has been deleted so let me see okay so I don't know why it's not returned to as this string content information but okay this ISS deleted so now let's go back in and see if it's really deleted so now you can see has been successfully deleted so that's basically how you can create a simple crow API so now the only part which is left is the update part of the crow right we have run you have done create the C the the retrieve the r the update the U which you haven't done we have also done the delete part which is the D so C right so the only part you have left left to do is the update functionality and I really I want you guys to really experiment it and find out how to do this update functionality and there's a lot of T of documentation out there you can also read the of official documentation of how you can delete data or how you can update data in all is all here actually this just the code that you need to do so you can just copy this code change this to be the to-do table pass in the details that you want and that's basically specify the ID and that's that's all you need to do to do the update of the database so I'm going to leave that to you basically this is the code that you have here so you can just pause the video and get this code that's all you need to do so yeah uh that's all for this video and I hope you guys have found this helpful and now you have learned how to integrate fireb sorry super base into your fast API application I don't know why I Contin I continuously say Firebase because Firebase and super base actually very very similar so this basically that's how you are able that's how you can be able to create a simple crow API using super base and fast API okay so if you guys have any any questions recommendation or you want me to make take this into a more much advanced level you can create a whole authentication system login system file storage all using super base is actually very very simple to do so if you guys are actually interested in that let me know in the comment section below I'll do my best to get back to you and create something very simp simple like that so it just it really speeds up development instead of having to do everything uh by yourself having to encode passwords uh do hashing and all that super Bas just makes everything simple for you so if you're building an application like application that involves authentication C functionality I probably go with uh super base nowadays instead of having to do everything from scratch by myself which I usually I I have a video on that on how to do all that manually by yourself using post post post database everything from the hashing of the password user authentication JWT tokens and everything so you can check that video out if you want but if you guys also want me to make a video on how to build a whole system using a whole API using super base authentication file storage at the C functionalities let me know in the comment section below I'll do my best to get back to you thank you guys for watching so much and you in the next one keep safe
Info
Channel: Code With Prince
Views: 1,270
Rating: undefined out of 5
Keywords: codewithprince, programmingchannel, python, python devs, funcoding, fastapi, fastapi tutorial, fastapi python, fastapi with supabase, fastapi and supabase, supabase tutorial, supabase, how to integrate fastapi and supabase
Id: sS6Qe8svDfc
Channel Id: undefined
Length: 32min 0sec (1920 seconds)
Published: Thu Dec 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.