Create A Python API in 12 Minutes
Summary
TLDRThe video explains what an API is and how to create a simple API using Python and the Flask framework. It covers API concepts like routes, HTTP methods, path parameters, and query parameters. A get route and post route are demonstrated to retrieve and create data. The video recommends using Postman to test APIs and curl requests. Additional topics mentioned are authenticating APIs and connecting them to databases for more advanced capabilities.
Takeaways
- 😀 API stands for Application Programming Interface and allows software systems to communicate
- 💡 APIs typically live on a server, manipulate a database, and handle requests and responses
- 📝 Flask is a popular Python web framework for building APIs
- 🌐 APIs have different routes that correspond to endpoints that return data
- 🔍 GET requests retrieve data, POST creates data, PUT updates data, DELETE removes data
- 📎 Path parameters allow passing dynamic values in a route URL
- 📋 Query parameters add extra values after the main route path
- 📤 POST routes receive JSON data in the request body and can return it in the response
- ✅ Postman is a useful tool for testing API requests and responses
- 🌟 Next steps are adding authentication and connecting APIs to databases
Q & A
What does API stand for and what does it allow?
-API stands for Application Programming Interface. It allows different software systems to communicate with each other through a set of rules.
Where does an API typically live and what does it manipulate?
-An API typically lives on some type of server and manipulates some kind of database.
What are the main HTTP methods used when creating API routes?
-The main HTTP methods used are GET, POST, PUT and DELETE.
How can you access path parameters and query parameters in Flask?
-Path parameters are accessed directly as function parameters. Query parameters are accessed using request.args which contains a dictionary of the parameters.
How do you return JSON data from a Flask API?
-Use the json.jsonify function to convert a Python dictionary to JSON and return it along with an appropriate status code like 200.
What tool does the speaker recommend for testing APIs?
-The speaker recommends using Postman for testing APIs.
What code do you need to create a basic Flask application?
-You need to import Flask, create an app variable set to Flask(__name__), and run the app using if __name__ == '__main__': app.run().
How do you specify which HTTP methods a route accepts?
-Pass the allowed methods like ["GET", "POST"] as the second argument to the app.route decorator.
How can you check the request method inside a route function?
-Use request.method to get the request method string like 'GET', 'POST' etc.
What is typically the next step after creating a basic API?
-The next typical steps are adding authentication and connecting the API to a database.
Outlines
😀 What is an API and how it works
Paragraph 1 explains what an API is - a set of rules that allow different software systems to communicate. It typically lives on a server, manipulates a database, handles requests and returns responses. The speaker will demonstrate creating a simple Python/Flask API.
😊 Different HTTP methods for API routes
Paragraph 2 introduces key HTTP methods for API routes - GET to retrieve data, POST to create new data, PUT to update existing data, and DELETE to remove data. Example GET and POST routes are implemented.
📝 Testing APIs and next steps
Paragraph 3 shows how to test the API routes using Postman. It encourages further learning on advanced features like authentication and connecting to databases.
Mindmap
Keywords
💡API
💡Flask
💡Route/Endpoint
💡HTTP Methods
💡JSON
💡Parameters
💡Postman
💡Authentication
💡Database
💡Deployment
Highlights
API stands for application programming interface, and is a set of rules that allow software systems to communicate
An API typically lives on a server and manipulates a database, handling different requests and returning responses
We'll use Flask, a popular Python web framework, to set up an API server
A route is an API endpoint that returns data when accessed
Common HTTP methods used are GET, POST, PUT and DELETE to retrieve, create, update or delete data
Path parameters allow passing dynamic values in a route URL to access in the function
Query parameters are extra values included after the path to pass additional data
Data is returned from an API as JSON for easy parsing
Use tools like Postman to test sending requests and handling responses from an API
POST creates new data by having the user submit JSON that gets processed by the route
Request data like methods and JSON body are accessed via the imported Flask request object
Responses return status codes like 200 for success to indicate results
Authentication should be added to lock down API access
Connect APIs to databases to enable full CRUD functionality
FastAPI and advanced Flask techniques allow building more complex, production-ready APIs
Transcripts
What is an API?
Well, API stands for application
programing 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 sponsor
Dream Factory, has you covered.
With Dream Factory,
you can create fully
featured APIs in seconds
by simply connecting to 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's created,
I can go ahead and add authentication
by making a new rule.
Connecting that role
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 can connect
to your existing authentication systems
like off.
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 sponsoring this video.
All right.
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.
It'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 three 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
Jason a PHI,
which we're going to use
to create a Jason 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 end point.
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 root.
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 app 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 root.
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 py,
which is the name of my python file.
If you're on Mac or Linux,
you're going to use Python three.
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 root,
let's have a look at some other roots
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 root 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 parameters,
a dynamic value that you can pass
in the path of a URL
that we'll be able to access
inside of our route.
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
instead 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 dash user slash
and then any value I want.
And this value will be
the idea of the user
that we're attempting to retrieve. Right?
So if I have something like six 2 to 6,
then that's indicating that
I want to retrieve the user with the
I with the ID sort of six 2 to 6,
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 mark 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 dashed 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 route.
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 import up here
dot args which stores
all of my query
parameters in a dictionary
and then I can use dog
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
at the extra key is equal to extra,
then I can return my data
so I can say return JSON or fi
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 just sort of
fi that dictionary
and that's what
we can return to the user.
This allows flask
to actually pass 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 FDP
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 out my terminal here
and run my server.
And now we can actually test our get root
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 1 to 3 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 will do a very similar thing
to what we did before.
Will 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 requests,
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.
Now 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 a
some JSON,
which is kind of the user
they want to create.
So how do we get that?
Well, I'm going to say
data is equal to request dot
get underscore.
Jason,
this is going to give me
all of 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 a return and then I need to.
Jason ify
the data
and then I can return
the status request of to a one.
And now we've 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 view 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.
All right,
so my Python server is running,
so I'm going to copy
the URL that it's running on right here,
which is local host 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 Tool one,
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, we'll 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.
Voir Plus de Vidéos Connexes
Build an API with Django // Part 1 - What is an API?
Access Perplexity API In n8n To Scrape Live Website Data Through HTTP Request Authorization 🤖
Express JS #3 - Route Parameters
FastAPI, Flask or Django - Which Should You Use?
What Is A RESTful API? Explanation of REST & HTTP
HTTP | Mastering React: An In-Depth Zero to Hero Video Series
5.0 / 5 (0 votes)