Create A Python API in 12 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is an API well API stands for application programming interface and really this is a set of rules that allow different software systems to communicate with each other an API typically lives on some type of server and will manipulate some kind of database it can handle different requests so if I'm a user utilizing the API I send a request to it my request may contain some different data then the API is going to process this request and return to me a response that response will typically contain data that I was requesting from the API typically your API will manipulate some type of database and allow different access to the database values depending on the set of rules that you set up in this video I'll show you how to set up a simple API using Python and flask in under 10 minutes so that said let's go ahead and get started after a quick word from our sponsor if you're interested in rapidly creating apis then today's sponsored Dream Factory has you covered with Dream Factory you can create fully featured apis in seconds by simply connecting to an a data source Let Me Show You Begin by starting your dream Factory instance logging in and then connecting to a data source for this demo I'll use mongodb I'll fill in my connection details press save and in seconds Dream Factory has created a fully customizable and fully documented rest API that I can now use to access my data look I can query my reviews collection and view a result in a matter of seconds now that my API is created I can go ahead and add authentication by making a new rule connecting that rule to a new application and generating a unique API token now that I have this token I can access my API securely from anywhere look at me doing it here right in Postman now best of all Dream Factory apis are fully customizable can run anywhere and connect to your existing authentication systems like oauth spend less time writing back ends and more time developing beautiful applications by clicking the link in the description and getting started with Dream Factory today thanks again to Dream Factory for sponsor during this video alright so let's begin with our project setup for this video we're going to use flask this is a micro web framework for python that's very popular for doing web development and for creating apis to set up flask we first need to open some kind of terminal window in this case I'm in command prompt if you're on Mac or Linux open your terminal and we're going to type the following command which is PIP install and then flask now this should install the flask framework for you if you're on Mac or Linux you may want to try the PIP 3 command if pip was not working for you and if neither of those commands work I'll leave two videos on the screen that will show you how to fix that pip command now that we have flask installed we're just going to open a python file which I already have here in some kind of directory and we're going to create a basic flask application flask will be the server that's running our API we're going to begin by importing our dependencies we'll say from flask import and then we're going to import flask with a capital request and then the function jsonify which we're going to use to create a Json response now we need to create our flask application so we're going to say app is equal to flask and then underscore underscore name underscore underscore like that and then we're going to run our flask application by saying if underscore underscore name is equal to underscore and square Main app dot run and then debug equal true this will run our flask server now that our flask server is set up and we have the basic initialization what we need to do is create something known as a root now a root is essentially an endpoint this is kind of a location on our API that we can go to to get some kind of data we have different kinds of routes which I'll talk about in a second but for now let's create a simple one and test this out so you can see how it works so to create a root we're going to define a python function we're going to say Define in this case we'll go with something like home inside of our function we'll just return some data that we want the user to have access to when they reach this route so we'll say return and then in this case go with something like home now to make this accessible what we need to do is add a decorators we're going to use an at symbol and say app which is the name of our flask application here so use the same variable name dot root and then we're going to put the path that we want to access in this case we're just going to do slash which will be the default route now the root is really what comes after the slash in your URL address bar so that's what we're setting up here okay let's save our code and let's go ahead and run this to run this we're going to type Python and then main.pi which is the name of my python file if you're on Mac or Linux you're going to use Python 3. okay so let's run that you can see that it's now created a development server for us and I can open this URL by just right clicking on it here in Visual Studio code when I do that you can see that I get home appearing on my screen there you go you just created your first root inside of flask so now that we've created this demo route let's have a look at some other routes that we can create with different HTTP methods now whenever we're writing an API we're working with something known as HTTP which is essentially the way we communicate data over the internet now when we create different API routes we can mark them with different methods now the methods we can use are get post put and delete we have access to a bunch of other ones as well but these are the most common ones now get is used when we want to retrieve some value from the server post is used when we want to create something something new put is used when we want to alter or modify some existing data and delete is used we want to remove or delete data from a database or from whatever resource it is that we're accessing so let's create a more complicated get route here and see how that works so we're going to say at app dot root for this we're going to say slash get Dash user and then I'm going to implement something known as a path parameter now a path parameter is a dynamic value that you can pass in the path of a URL that will be able to access inside of our root so in this case I'm going to say user underscore ID then I'm going to make my function get underscore user and I'm going to accept a variable inside of my function parameters here that's the same as the path parameter I put here now when we do this this essentially means that I can do something like get user slash and then any value I want and this value will be the ID of the user that we're attempting to retrieve right so if I have something like 6226 then that's indicating that I want to retrieve the user with the AI with the ID story of 6226 okay that's a path parameter so how do we access the path parameter well we can just access it directly from here so what I'm going to do is essentially mock some data that we want to return to our user so I can paste this in user data is equal to the following and now I want to talk to you about something known as a query parameter now whenever we are accessing a root we have the ability to pass something known as a query parameter which is essentially an extra value that is included after the main path so if I have my path get Dash user slash one two three if I put a question mark here now I can pass different query parameters so something like extra is equal to hello world okay this is an additional variable that I can pass along to my root so how do I access that from flask well I can do the following I can say extra is equal to request request is the variable I imported up here dot orgs which stores all of my query parameters in a dictionary and then I can use dot get and try to access a parameter or sorry a value like extra okay now that I have extra I can check if this exists so I can say something like if extra then user data add to the extra key is equal 2 extra then I can return my data so I can say return jsonify and then this will be my user underscore data I'll pass the Response Code of 200. so whenever we are returning data from an API we're going to use Json now Json stands for JavaScript object notation which is essentially a collection of key value pairs very similar to a python dictionary so in flask we create a dictionary and then we jsonify that dictionary and that's what we can return to the user this allows flask to actually parse this value and return it as Json data the next value that we pass here is the status code 200 is the default status code of success you can pass other HTTP status codes here as well I won't go through all of them so now that we have this get root let's make sure our server is running so let me just bring up my terminal here and run my server and now we can actually test our get route from the browser by default when you actually type in a URL here in your browser you're going to be sending a get request so notice here that I have the URL of get user and then slash 123 question mark extra equals hello and then it returns to me the Json data of this which has my user ID and then what I passed here inside of the query parameter now that we've had a look at how to make a get root let's have a look at making a post root so to create a post request we'll do a very similar thing to what we did before we'll have an at app dot root we'll go here and we'll say slash and then this can be something like create Dash user however this time since we're not using the default get request we have to actually specify the accepted method for this root so in this case I'm going to put inside of an array here post now this means that I can accept a post request if I wanted to accept both a post request and a get request that I would put both of them inside of here for now all I want to accept is post that will Define my function create underscore user and if I wanted to check inside of this function what the method was that was being used I could do the following I could say if request dot method is equal to post and I would only do this if I had multiple methods but I just wanted to show you you can use request.method which is from this variable right here and it tells you if you're using post get Etc regardless we know it's going to be post what we want to do here is actually receive some data from the request that's in Json format so the user is going to submit us some Json which is kind of the user that want to create so how do we get that well I'm going to say data is equal to request.get underscore Json this is going to give me all the Json data that was passed in the body of the request I'm going to show you how we look at this in a second and what I can do is simply return this back to the user to indicate that this was created successfully so I can say return and then I need to jsonify the data and then I can return the status request of 201 and now we have successfully implemented a post request where we are receiving some Json data from the user obviously I'd probably do something more here like add this to a database but this is just a simple demo okay so now we've created this post request however I can't demo the post request for you in the browser so now we're going to quickly talk about how we can test our apis now there's a variety of different ways we can test our apis but I'm going to recommend we use a tool called Postman so please download this from the link in the description and then open up that software and let's look at how we can call this API alright so my python server is running so I'm going to copy the URL that it's running on right here which is localhost Port 5000 I'm going to open up my Postman software I'm going to click plus here to create a new request I'm going to paste this in change the url to create Dash user change the method here to be post and then I'm going to go to where it says body because this is where I'm going to include my Json data I'm going to change this to say raw data I'm going to go here to text and change this to not JavaScript but Json I'm going to pass a Json object so the way I do that is have a set of curly braces just like my python dictionary I'm then going to go and make a key my key will be username and I'll have this associated with the key or with the value sorry Tim now if I hit send you can see that we get this exact same data back with the status code of 201 and that means that this is working successfully so this is a great tool that you can use to test out your apis as you continue to do more development so with that said that will wrap up this video but I will encourage you to learn more by checking out this video here which is on Fast API or this video here which is creating a more advanced API in flask that uses authentication really The Next Step here is to authenticate your apis and connect them to a database obviously I didn't have time here with that said I hope you enjoyed and I look forward to seeing you in another one
Info
Channel: Tech With Tim
Views: 467,594
Rating: undefined out of 5
Keywords: tech with tim, build your first api in 10 minutes, api python tutorial, rest api in hindi, fastapi in python, api python project, fastapi python, fastapi tutorial, api python, python fastapi tutorial, python api fastapi, python fastapi project, python fastapi web app, learn coding, api tutorial, python, coding in flow, build api from scratch, learn programming, computers, learn python, coding tutorials
Id: zsYIw6RXjfM
Channel Id: undefined
Length: 12min 4sec (724 seconds)
Published: Tue May 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.