Create A Python API in 12 Minutes

Tech With Tim
23 May 202312:04

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

00:00

😀 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.

05:00

😊 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.

10:00

📝 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

API stands for Application Programming Interface. It is a set of rules that allow different software systems to communicate with each other. An API typically lives on a server and manipulates a database. It handles requests and returns responses with data.

💡Flask

Flask is a popular Python web framework used for building APIs and web applications. It is used in the video to create the server that runs the API. Flask provides functionality to define routes/endpoints and return JSON responses.

💡Route/Endpoint

A route or endpoint is a URL location in the API that is associated with some functionality or data. When a request hits a route, the associated Python function executes to process the request and return a response.

💡HTTP Methods

HTTP methods like GET, POST, PUT, DELETE are used to perform different operations on resources/data via APIs. GET retrieves data, POST creates data, PUT updates data, DELETE deletes data.

💡JSON

JSON (JavaScript Object Notation) is a common format used to encode/serialize and transmit data in APIs and web applications. Flask returns Python dictionaries as JSON automatically.

💡Parameters

Parameters like path parameters and query parameters allow passing additional data to API routes. Eg: /users/:userId has a path param, /users?type=paid has a query param.

💡Postman

Postman is an API testing tool used to send requests like GET, POST to API endpoints and inspect responses. It provides an easy way to test APIs without needing frontend code.

💡Authentication

Authentication mechanisms like API keys allow securing access to APIs. The video mentions it as an important next step after creating basic API functionality.

💡Database

A database is used to persistently store and manage data consumed/produced by an API. Connecting the API to a database allows saving, modifying, deleting data.

💡Deployment

Deployment refers to hosting the Flask API on a public server to make it accessible over the internet. The video runs it locally for simplicity.

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

play00:00

What is an API?

play00:01

Well, API stands for application

play00:03

programing interface,

play00:04

and really this is a set of rules

play00:06

that allow different software systems

play00:07

to communicate with each other.

play00:09

An API

play00:10

typically lives on some type of server

play00:11

and will manipulate

play00:12

some kind of database.

play00:14

It can handle different requests.

play00:15

So if I'm a user utilizing the API,

play00:18

I send a request to it.

play00:19

My request may contain

play00:20

some different data.

play00:22

Then the API is going

play00:23

to process this request

play00:24

and return to me a response.

play00:26

That response will typically contain data

play00:28

that I was requesting from the API.

play00:30

Typically,

play00:31

your API will manipulate

play00:32

some type of database

play00:33

and allow different access

play00:35

to the database values

play00:36

depending on the set of rules

play00:37

that you set up.

play00:38

In this video,

play00:39

I'll show you how to set up a simple

play00:40

API using python

play00:42

and flask in under 10 minutes.

play00:44

So that said, let's go ahead

play00:45

and get started

play00:46

after a quick word from our sponsor.

play00:48

If you're interested in rapidly

play00:50

creating APIs,

play00:51

then today's sponsor

play00:52

Dream Factory, has you covered.

play00:54

With Dream Factory,

play00:55

you can create fully

play00:56

featured APIs in seconds

play00:58

by simply connecting to a data source.

play01:00

Let me show you.

play01:01

Begin by starting your Dream

play01:03

Factory instance.

play01:04

Logging in

play01:05

and then connecting to a data source.

play01:07

For this demo, I'll use MongoDB.

play01:09

I'll fill in my connection details.

play01:11

Press Save and in Seconds.

play01:12

Dream Factory has created

play01:14

a fully customizable

play01:15

and fully documented rest API

play01:18

that I can now use to access my data.

play01:20

Look, I can query

play01:21

my reviews, collection and view a result

play01:23

in a matter of seconds.

play01:25

Now that my API's created,

play01:26

I can go ahead and add authentication

play01:28

by making a new rule.

play01:30

Connecting that role

play01:31

to a new application

play01:32

and generating a unique API token.

play01:34

Now that I have this token,

play01:36

I can access my API

play01:37

securely from anywhere.

play01:39

Look at me doing it here

play01:40

right in Postman Now.

play01:42

Best of all, Dream Factory

play01:43

APIs are fully customizable,

play01:45

can run anywhere

play01:46

and can connect

play01:47

to your existing authentication systems

play01:49

like off.

play01:49

Spend less time

play01:50

writing back ends

play01:51

and more time developing

play01:52

beautiful applications

play01:53

by clicking the link in the description

play01:55

and getting started

play01:56

with Dream Factory today,

play01:57

Thanks again to Dream Factory

play01:59

for sponsoring this video.

play02:01

All right.

play02:01

So let's begin with our project setup

play02:03

for this video we're going to use FLASK.

play02:05

This is a micro web framework for Python.

play02:07

It's very popular

play02:08

for doing web development

play02:09

and for creating APIs to set up FLASK.

play02:12

We first need to open

play02:13

some kind of terminal window.

play02:14

In this case, I'm in command prompt.

play02:15

If you're on Mac or Linux,

play02:16

open your terminal

play02:18

and we're going to type

play02:18

the following command,

play02:19

which is PIP, install and then flask.

play02:22

Now this should install the FLASK

play02:23

framework for you.

play02:25

If you're on Mac or Linux,

play02:26

you may want to try the PIP three command

play02:28

if PIP was not working for you

play02:30

and if neither of those commands work,

play02:31

I'll leave

play02:31

two videos on the screen

play02:33

that will show you

play02:33

how to fix that PIP command.

play02:35

Now that we have FLASK installed,

play02:37

we're just going to open a python file,

play02:38

which I already have here

play02:40

in some kind of directory

play02:41

and we're going to create a basic flask

play02:43

application.

play02:43

Flask will be the server

play02:45

that's running our API.

play02:46

We're going to begin

play02:47

by importing

play02:48

our dependencies,

play02:48

we'll say from flask import,

play02:51

and then we're going to import

play02:52

FLASK with a capital request.

play02:54

And then the function

play02:55

Jason a PHI,

play02:57

which we're going to use

play02:57

to create a Jason response.

play02:59

Now we need to create our flask

play03:01

application.

play03:02

So we're going to say

play03:02

app is equal to flask

play03:05

and then underscore, underscore,

play03:06

name, underscore, underscore like that.

play03:08

And then we're going

play03:09

to run our flask application

play03:10

by saying if underscore, underscore name

play03:13

is equal to underscore and square main

play03:16

app dot run

play03:18

and then

play03:18

debug equal true,

play03:20

this will run our flask server.

play03:22

Now that our flask server is set up

play03:24

and we have the basic initialization,

play03:26

what we need to do is create

play03:27

something known as a root.

play03:28

Now a root is essentially an end point.

play03:30

This is kind of a location on our API

play03:33

that we can go to

play03:34

to get some kind of data.

play03:35

We have different kinds of routes,

play03:37

which I'll talk about in a second.

play03:38

But for now, let's create

play03:39

a simple one and test this out

play03:40

so you can see how it works.

play03:42

So to create a root,

play03:42

we're going to define a Python function.

play03:44

We're going to say

play03:45

define in this case,

play03:46

we'll go with something like home

play03:48

inside of our function.

play03:49

We'll just return some data

play03:50

that we want the user to have access

play03:51

to when they reach this root.

play03:53

So we'll say return,

play03:55

and then in this case,

play03:55

go with something like home.

play03:57

Now, to make this accessible,

play03:59

what we need to do is add a decorators.

play04:01

We're going to use an app symbol

play04:02

and say app,

play04:03

which is the name of our flask

play04:04

application here.

play04:05

So use the same variable name, dot root

play04:08

and then we're going to put the path

play04:09

that we want to access.

play04:10

In this case,

play04:10

we're just going to do slash,

play04:12

which will be the default root.

play04:13

Now the root is really

play04:14

what comes after the slash

play04:16

in your URL address bar.

play04:18

So that's what we're setting up here.

play04:19

Okay, let's save our code and let's

play04:21

go ahead and run this.

play04:22

To run this, we're going to type Python

play04:24

and then main py,

play04:26

which is the name of my python file.

play04:27

If you're on Mac or Linux,

play04:28

you're going to use Python three.

play04:31

Okay, so let's run that.

play04:32

You can see that it's now

play04:33

created a development server for us

play04:34

and I can open this URL by just right

play04:36

clicking on it

play04:37

here in Visual Studio Code.

play04:39

When I do that, you can see that

play04:40

I get home appearing on my screen.

play04:42

There you go.

play04:42

You just created

play04:43

your first root inside of flask.

play04:45

So now that we've created this demo root,

play04:47

let's have a look at some other roots

play04:48

that we can create with

play04:49

different HTTP methods.

play04:51

Now, whenever we're writing an API,

play04:53

we're working with

play04:53

something known as HTTP,

play04:55

which is essentially the way

play04:56

we communicate data over the Internet.

play04:58

Now, when we create different API routes,

play05:00

we can mark them with different methods.

play05:02

Now the methods we can use

play05:03

are get post, put and delete.

play05:07

We have access

play05:07

to a bunch of other ones as well,

play05:08

but these are the most common ones.

play05:10

Now get is used

play05:11

when we want to retrieve

play05:12

some value from the server

play05:14

post is used

play05:14

when we want to create something

play05:16

something new

play05:17

put is used

play05:18

when we want to alter or modify

play05:19

some existing data and delete is used.

play05:21

We want to remove

play05:22

or delete data from a database

play05:24

or from whatever resource it is

play05:26

that we're accessing.

play05:27

So let's create

play05:27

a more complicated get root here

play05:29

and see how that works.

play05:30

So we're going to say

play05:31

at app dot root

play05:33

for this, we're going to say slash

play05:35

get dash user,

play05:37

and then I'm going to implement

play05:38

something known as a path parameter.

play05:40

Now a path parameters,

play05:41

a dynamic value that you can pass

play05:43

in the path of a URL

play05:45

that we'll be able to access

play05:47

inside of our route.

play05:48

So in this case, I'm going to say user

play05:49

underscore ID,

play05:51

then I'm going to make my function

play05:52

get underscore user

play05:54

and I'm going to accept a variable

play05:56

instead of my function parameters here.

play05:58

That's the same

play05:59

as the path parameter I put here.

play06:01

Now, when we do this, this

play06:02

essentially means that I can do

play06:03

something like get dash user slash

play06:06

and then any value I want.

play06:07

And this value will be

play06:08

the idea of the user

play06:09

that we're attempting to retrieve. Right?

play06:12

So if I have something like six 2 to 6,

play06:14

then that's indicating that

play06:15

I want to retrieve the user with the

play06:16

I with the ID sort of six 2 to 6,

play06:19

that's a path parameter.

play06:21

So how do we access the path parameter?

play06:22

Well, we can just access it

play06:23

directly from here.

play06:25

So what I'm going to do

play06:25

is essentially mark some data

play06:27

that we want to return to our user

play06:28

so I can paste this in user

play06:30

data is equal to the following.

play06:32

And now I want to talk to you

play06:33

about something

play06:33

known as a query parameter.

play06:35

Now, whenever

play06:35

we are accessing a root,

play06:36

we have the ability to pass

play06:37

something known as a query parameter,

play06:39

which is essentially an extra value

play06:41

that is included after the main path.

play06:43

So if I have

play06:44

my path get dashed user

play06:46

slash one, two, three.

play06:48

If I put a question mark here now,

play06:50

I can pass different query parameters.

play06:52

So something like extra is equal

play06:54

to Hello World.

play06:55

Okay, this is an additional variable

play06:57

that I can pass along to my route.

play06:59

So how do I access that from FLASK?

play07:01

Well, I can do the following.

play07:02

I can say extra is equal to request.

play07:05

Request is the variable I import up here

play07:08

dot args which stores

play07:09

all of my query

play07:10

parameters in a dictionary

play07:11

and then I can use dog

play07:12

get and try to access a parameter

play07:15

or sorry a value like extra.

play07:17

Okay.

play07:18

Now that I have extra,

play07:18

I can check if this exists

play07:20

so I can say something

play07:20

like if extra then user data

play07:24

at the extra key is equal to extra,

play07:29

then I can return my data

play07:31

so I can say return JSON or fi

play07:33

and then this will be

play07:34

my user underscore data.

play07:37

I'll pass the response code of 200.

play07:40

So whenever we are returning data

play07:41

from an API, we're going to use JSON.

play07:43

Now JSON stands for JavaScript

play07:45

object notation,

play07:46

which is essentially a collection

play07:47

of key value pairs,

play07:49

very similar to a Python dictionary.

play07:51

So in flask

play07:52

we create a dictionary

play07:53

and then we just sort of

play07:54

fi that dictionary

play07:56

and that's what

play07:56

we can return to the user.

play07:58

This allows flask

play07:59

to actually pass this value

play08:00

and return it as JSON data.

play08:03

The next value that we pass here

play08:04

is the status code

play08:06

200 is the default

play08:07

status code of success.

play08:08

You can pass other FDP

play08:10

status codes here as well.

play08:11

I won't go through all of them,

play08:13

so now that we have this get root,

play08:14

let's make sure our server is running.

play08:16

So let me just bring out my terminal here

play08:18

and run my server.

play08:19

And now we can actually test our get root

play08:21

from the browser by default.

play08:23

When you actually type in a

play08:25

URL here in your browser,

play08:27

you're going to be sending a get request.

play08:29

So notice here

play08:29

that I have the URL of get user

play08:31

and then slash 1 to 3 question mark.

play08:34

Extra equals Hello.

play08:35

And then it returns to me

play08:36

the JSON data of this

play08:37

which has my user ID

play08:39

and then what

play08:39

I passed here

play08:40

inside of the query parameter.

play08:41

Now that we've had a look

play08:42

at how to make a get root,

play08:43

let's have a look at making a post root.

play08:45

So to create a post

play08:46

request will do a very similar thing

play08:48

to what we did before.

play08:49

Will have an at app dot root.

play08:52

We'll go here

play08:52

and we'll say slash

play08:53

and then this can be something

play08:54

like create dash user.

play08:57

However, this time,

play08:58

since we're not using the default

play08:59

get requests,

play09:00

we have to actually specify

play09:01

the accepted method for this root.

play09:04

So in this case

play09:04

I'm going to put inside of an array

play09:06

here post.

play09:07

Now this means that

play09:08

I can accept a post request

play09:09

if I wanted to accept both a post request

play09:11

and a get request

play09:13

that I would put both of them

play09:14

inside of here.

play09:14

For now, all I want to accept is post.

play09:17

Now define

play09:17

my function, create underscore user

play09:20

and if I wanted to check inside

play09:21

of this function

play09:21

what the method was

play09:23

that was being used,

play09:24

I could do the following.

play09:25

I could say

play09:25

if request dot method is equal to post

play09:30

and I would only do this

play09:31

if I had multiple methods,

play09:32

but I just wanted to show you

play09:33

you can use request method,

play09:35

which is from this variable right here

play09:37

and it tells you

play09:37

if you're using, post, get, etc..

play09:40

Regardless, we know it's going to be post

play09:41

what we want to do here

play09:43

is actually receive

play09:44

some data from the request

play09:46

that's in JSON format.

play09:47

So the user is going to submit a

play09:48

some JSON,

play09:49

which is kind of the user

play09:50

they want to create.

play09:52

So how do we get that?

play09:53

Well, I'm going to say

play09:53

data is equal to request dot

play09:56

get underscore.

play09:57

Jason,

play09:58

this is going to give me

play09:59

all of the JSON data

play10:00

that was passed

play10:00

in the body of the request.

play10:02

I'm going to show you

play10:03

how we look at this in a second.

play10:04

And what I can do

play10:05

is simply return this back to the user

play10:07

to indicate

play10:08

that this was created successfully.

play10:09

So I can say a return and then I need to.

play10:12

Jason ify

play10:14

the data

play10:15

and then I can return

play10:16

the status request of to a one.

play10:18

And now we've successfully implemented

play10:19

a post request

play10:20

where we are receiving

play10:21

some JSON data from the user.

play10:23

Obviously I'd probably do something

play10:24

more here, like add this to a database,

play10:26

but this is just a simple demo.

play10:28

Okay,

play10:28

so now we've created this post request.

play10:30

However, I can't demo the post

play10:31

request view in the browser.

play10:33

So now we're going to quickly

play10:34

talk about how we can test our APIs.

play10:36

Now there's a variety of different ways

play10:37

we can test our APIs,

play10:38

but I'm going to recommend

play10:39

we use a tool called Postman.

play10:41

So please download this

play10:42

from the link in the description

play10:43

and then open up that software

play10:45

and let's

play10:45

look at how we can call this API.

play10:47

All right,

play10:48

so my Python server is running,

play10:49

so I'm going to copy

play10:50

the URL that it's running on right here,

play10:52

which is local host port 5000.

play10:54

I'm going to open up my postman software.

play10:56

I'm going to click

play10:57

plus here to create a new request.

play10:59

I'm going to paste this in,

play11:00

change the URL to create dash user,

play11:03

change the method here to be post

play11:05

and then I'm going to go

play11:06

to where it says body

play11:07

because this is where

play11:08

I'm going to include my JSON data.

play11:10

I'm going to change this to say raw data.

play11:12

I'm going to go here to text

play11:13

and change this to not JavaScript,

play11:15

but JSON.

play11:16

I'm going to pass a JSON object.

play11:18

So the way I do

play11:19

that is have a set of curly braces

play11:20

just like my python dictionary.

play11:22

I'm then going to go and make a key.

play11:24

My key will be username

play11:25

and I'll have this associated

play11:26

with the key or with the value.

play11:28

Sorry Tim.

play11:29

Now if I hit send,

play11:30

you can see that

play11:31

we get this exact same data back

play11:33

with the status code of Tool one,

play11:35

and that means that

play11:35

this is working successfully.

play11:37

So this is a great tool

play11:38

that you can use to test out your APIs

play11:40

as you continue to do more development.

play11:42

So with that said

play11:43

that, we'll wrap up this video,

play11:44

but I will encourage you

play11:45

to learn more

play11:45

by checking out this video here,

play11:47

which is on Fast API

play11:48

or this video here,

play11:49

which is creating a more advanced

play11:51

API in FLASK that uses authentication.

play11:54

Really,

play11:54

the next step here

play11:55

is to authenticate your APIs

play11:56

and connect them to a database.

play11:58

Obviously, I didn't have time here.

play12:00

With that said, I hope you enjoyed

play12:01

and I look forward to seeing you in

play12:03

another one.

Rate This

5.0 / 5 (0 votes)

Вам нужно краткое изложение на английском?