Create A Python API in 12 Minutes
Key Takeaways
The video demonstrates how to create a Python API in 12 minutes using DreamFactory and Flask, covering API creation, customization, and security, as well as implementing different HTTP methods and testing APIs with Postman.
Full Transcript
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
Original Description
Rapidly build and deploy APIs in seconds with DreamFactory ! https://bit.ly/3M1TAP7
Are you learning programming and wondering what an API is? An API is an Application Programming Interface that allows different software or systems to communicate with one another. I'll show you how to whip up your very own API in just 10 minutes using Python and Flask.
Post Man API Platform:
https://www.postman.com/
Fix Pip (Windows):
https://www.youtube.com/watch?v=AdUZArA-kZw&t=204s
Fix Pip (Mac):
https://www.youtube.com/watch?v=E-WhAS6qzsU
🎬 Timestamps⏱️
00:00 | What is an API?
02:02 | Dependency Setup
02:49 | Flask API Setup
04:49 | HTTP Methods
05:28 | GET Routes
08:46 | POST Routes
10:38 | Testing APIs
11:42 | Next Steps
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
👕 Merchandise: 🔗 https://teespring.com/stores/tech-with-tim-merch-shop
📸 Instagram: 🔗 https://www.instagram.com/tech_with_tim
📱 Twitter: 🔗 https://twitter.com/TechWithTimm
🔊 Discord: 🔗 https://discord.gg/twt
📝 LinkedIn: 🔗 https://www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: 🔗 https://techwithtim.net
📂 GitHub: 🔗 https://github.com/techwithtim
One-Time Donations: 💲 https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8
Patreon: 💲 https://www.patreon.com/techwithtim
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
⭐️ Tags ⭐️
-Tech With Tim
-Python
-Programming
⭐️ Hashtags ⭐️
#techwithtim #python #coding
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
atob() can't decode a JWT — the Base64URL gotcha (and the fix)
Dev.to · Daniel Cheong
Why Debugging Made Me a Better Developer
Medium · JavaScript
Mapping Go Domain Errors to HTTP Status Codes at the Boundary
Dev.to · Gabriel Anhaia
The dual-write problem in NestJS, solved with Drizzle: a transactional outbox + idempotent inbox
Dev.to · Rodrigo Nogueira
Chapters (8)
| What is an API?
2:02
| Dependency Setup
2:49
| Flask API Setup
4:49
| HTTP Methods
5:28
| GET Routes
8:46
| POST Routes
10:38
| Testing APIs
11:42
| Next Steps
🎓
Tutor Explanation
DeepCamp AI