📺 Develpreneur YouTube Episode

Video + transcript

How Do I Call An API From Python?

2023-08-03 •Youtube

Detailed Notes

The proliferation of websites and APIs in modern development makes it a common requirement to be able to call them from with code. This video focuses on API development and creating a Python application that utilizes the API.

You can find out more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list and you will periodically receive coupons for courses as well as notifications of the latest releases.

Transcript Text
foreign
[Music]
well hello and welcome back if you are
continuing along uh sort of going in
order then last episode we talked about
using Postman to work with an API and so
we had that up we would do stuff like
for example we would post and we'd give
it a let's give it an address
like this one
we're going to take that and we're going
to turn around and we're going to give
it that address
and when we post it
it's going to send us back a shortened
URL it's in our logic here in shorty
that basically all it does is just each
time it's going to give us another ID
counter
we have done this separately but right
now we're doing it just in a very simple
thing so if I come in to get if I get it
with a zero it gives me one that was
there a minute ago uh if I give it with
the one
then it gives me
whoops
why did you give me the same thing
interesting
sorry take this address and let's just
put it here for now and let's take
another one uh we'll go back to there
and we'll do
our calendly one because that's a big
nasty one
so those are two we're going to work
with so let's work with those real quick
let's get this fixed and figure out why
it's not working so if I come in and I
run
and I post with
so if I add the first one well let's add
this one
and
there we go it tells me what that is so
now if I post a second one
should give me both yep so if we look
here
uh
oh for some reason it was grabbing
older one where is he grabbing that from
oh
I wonder if
that's params instead of body
that's our problem
that's not what we want
so we don't want it to be here we want
it to be here
uh
is that right
this is the post okay
let's try this
nope
uh form data there we go okay so that's
what we want
that's why it wasn't working so now if I
send that
so short two should get me the calendly
one and
if I go back over here
this is why we got to keep track of what
we're doing
and uh here we go
and short three
should give us this one so we're posting
that and so if we do short zero I think
that was I don't remember what that one
was
that was that quick start one is going
to be the quick start thing
there you go two is going to be the
only and then three gives us the RB site
okay
so what we want to do now is we're going
to build a little application to grab
to basically do the call so that we can
actually see that so we're going to call
this thing
let's make this collar
at py
and then we're going to need to do a
couple things to get ourselves set up to
make that call out to out to our API
now the easiest way to do this is take a
look at the get
as I showed you before
and if we come over here
I'm looking at cheat
is if we do python with requests
watch what's going to happen if I come
into here
so I'm going to import requests which I
think is already there let's see
so if I do Python 3
caller
boom
so notice that I'm getting the same
thing here
that I got here so if you want a
shortcut that's the best way to go but
let's examine this a little bit so we're
going to actually turn this into
something a little more useful
and so requester
we create request
is going to be uh
let's do well let's make it pretty uh
how cool do we want to do this it's a
good question
we can either store it or we can do
something with it so let's go ahead for
now and we're going to actually do
a
we actually put this together a little
bit so what we're going to do is we're
going to do uh we're going to call Base
URL
and it's going to be equal to that guy
and it's going to get us all the way up
to
let's do here we're going to actually
break this down a little bit
and then we're going to have the um
let's say get
short
let's do the
short end point
it's just going to be
shorten
now this is going to be for the get this
is going to be a little different we do
the the put and we'll show that in a
minute
so what we want to do here is we're
going to do let's see
the URL equals
input
which URL
push to use
and then we're going to have a little
function that we're going to call
get resolve get short uh no get
real
get real URL let's do get full URL
and what it's going to do is it's going
to take
a URL
push these up here
and then we're gonna do this and then
we're going to take this stuff
notice we don't need to worry about a
payload or headers this would be if you
were doing something like let me do this
real quick uh if you're doing something
like having form date or something which
we're going to show in a minute when we
do the post
so we're gonna come in here we're going
to say which URL do you wish to use
and then it's going to take the URL
and this is going to be pretty simple
um
response.txt
and
which is assuming it's we should
probably are going to want to wrap some
stuff around it but we'll check that in
a minute uh so print let's see get for a
little URL
so now if we do this
and we say
what is URL you wish to use and so we
could use
short three
and it's going to blow up because it
doesn't like what we said it so let's go
take a look at this because this is
going to be
where we're going to start running into
some issues so let's go look at it so
let's see
connection refused
oh wait
uh
whoops
oh that's the problem is because here uh
let's see so URL
is actually going to be so the uh
let's call that API URL
oops so it's going to be equal to
base URL
Plus
a short endpoint
whoops
if I can write that oh there we go
that's it short end point
and so that gives me that plus short and
then I'm going to need to do plus
Uh u r l equals
and then I'm going to add the URL
so now if I run it
um
see this now if I run it
with the three boom it gives me
that if I run it with a
if I run it and I do things same thing
and I give it a two
then notice I get a different
let's do it again so if I do that and if
I call it with a 3 notice I either get
the https or I get this one
so I get something a little bit
different from each of these now what I
want to do here is now I want to take
this out because in an API call what you
want to do
is you want to have something that's a
little more
valuable essentially in an API you don't
want to just send strings back like this
so what we really want to do here
is we're going to come in and we aren't
sure well we're going to we'll hold this
off until our next episode we're going
to talk about cleaning these up a little
bit
what you've got here is that is our
where'd that go collar so this is going
to be to get for the get full URL but
now what we want to do is we're going to
do something similar that is going to be
um
Escape get shortened
URL
and now for this we're still going to
cheat because this is so easy to do so I
come over here
for the Post
which there we go we're going to take
that
and it's going to look about the same
but what you're going to see here
return response that text
so you can see the same kind of thing
except because it's the the URL is going
to be the same combo
so we can do it like this
we don't need that we already have
requests up at the top so we don't need
that again
our payload now is going to be and so
this is that form we're setting the URL
equal to
the URL that they send in
so this is where we have a payload we
have for running files we're not seeing
those in we're not sending anything
that's a header of any sort
come in make this the API URL but
because it's a post we're going to do
that just to be clean
we could send some of these things and
work with them that's beyond what we're
going to talk about at this point and
they're going to return the response
text
so now if I come in and say
let's see
we're going to do that we're going to
print it and then we're going to come
out and say which well do you wish to
shorten
and it's going to do get
shortened URL
uh we'll do your the URL again
so now
first one we do we'll just do let's get
number four and see what that was
so we get number four
then that's what it was but if we take
this long thing
here and say that's what we want to
shorten
then we're going to get short five so
now to see do we get that back
if we call
short five
oh which is this we can just take this
and
uh oh and then it kicked it back here
this is what you sent short five this is
the actual URL okay
so
we have fairly easily and this is the
the breakdown of what we do is she
people with requests all you have to do
if you note is you're going to on
requests make a request tell it to type
and we'll talk another time about
various types are out there you send it
to whatever its address is in this case
it's our API address send some headers
if you need to payload which you might
need to do like for example in a form
and then it's going to send you a
response back based on whatever is
returned by that by that API call
so this is really simple and as I
mentioned before and I've shown you if
you want to use Postman that is going to
be the fastest way to get you just like
flying through setting up both sides and
testing your API which what we want to
do with our shortener here is to just
get this thing working because then we
can put it out on our server and have
our own little API our own little urls
shortener that we're going to need so
we're going to come back next episode or
we're going to clean these things up a
little bit and start tying them back
into the database but for now go out
there and have yourself a great day and
we will talk to you next time
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology our how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.development or we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
thank you
Transcript Segments
0.42

foreign

18.89

[Music]

27.06

well hello and welcome back if you are

29.58

continuing along uh sort of going in

31.74

order then last episode we talked about

34.8

using Postman to work with an API and so

38.76

we had that up we would do stuff like

40.98

for example we would post and we'd give

43.68

it a let's give it an address

47.28

like this one

50.52

we're going to take that and we're going

52.2

to turn around and we're going to give

53.64

it that address

55.739

and when we post it

58.739

it's going to send us back a shortened

60.539

URL it's in our logic here in shorty

64.92

that basically all it does is just each

66.54

time it's going to give us another ID

68.1

counter

69.54

we have done this separately but right

71.58

now we're doing it just in a very simple

73.68

thing so if I come in to get if I get it

77.28

with a zero it gives me one that was

81.42

there a minute ago uh if I give it with

84.24

the one

86.7

then it gives me

88.92

whoops

91.2

why did you give me the same thing

92.759

interesting

94.979

sorry take this address and let's just

97.439

put it here for now and let's take

100.14

another one uh we'll go back to there

103.14

and we'll do

105.479

our calendly one because that's a big

107.34

nasty one

109.86

so those are two we're going to work

110.939

with so let's work with those real quick

112.38

let's get this fixed and figure out why

114.119

it's not working so if I come in and I

115.799

run

117.479

and I post with

121.32

so if I add the first one well let's add

123.78

this one

125.84

and

127.68

there we go it tells me what that is so

129.899

now if I post a second one

135.48

should give me both yep so if we look

138.54

here

139.86

uh

143.58

oh for some reason it was grabbing

148.44

older one where is he grabbing that from

152.4

oh

153.9

I wonder if

156.78

that's params instead of body

159.739

that's our problem

163.739

that's not what we want

167.4

so we don't want it to be here we want

169.44

it to be here

173.879

uh

175.14

is that right

177.5

this is the post okay

185.22

let's try this

186.78

nope

193.519

uh form data there we go okay so that's

196.26

what we want

197.34

that's why it wasn't working so now if I

199.86

send that

201.18

so short two should get me the calendly

203.76

one and

207

if I go back over here

209.04

this is why we got to keep track of what

210.599

we're doing

212.22

and uh here we go

217.379

and short three

220.319

should give us this one so we're posting

222.72

that and so if we do short zero I think

226.739

that was I don't remember what that one

228.18

was

229.26

that was that quick start one is going

231.42

to be the quick start thing

234.06

there you go two is going to be the

239.459

only and then three gives us the RB site

243.959

okay

245.819

so what we want to do now is we're going

249.54

to build a little application to grab

252.78

to basically do the call so that we can

255.18

actually see that so we're going to call

256.859

this thing

259.019

let's make this collar

263.22

at py

267.06

and then we're going to need to do a

268.56

couple things to get ourselves set up to

271.32

make that call out to out to our API

275.88

now the easiest way to do this is take a

278.22

look at the get

280.56

as I showed you before

282.9

and if we come over here

285.12

I'm looking at cheat

287.28

is if we do python with requests

291.78

watch what's going to happen if I come

293.52

into here

296.639

so I'm going to import requests which I

298.74

think is already there let's see

302.34

so if I do Python 3

305.04

caller

306.96

boom

308.28

so notice that I'm getting the same

310.08

thing here

312.3

that I got here so if you want a

314.46

shortcut that's the best way to go but

316.38

let's examine this a little bit so we're

319.139

going to actually turn this into

320.16

something a little more useful

322.68

and so requester

325.38

we create request

328.5

is going to be uh

332.479

let's do well let's make it pretty uh

336.24

how cool do we want to do this it's a

337.5

good question

338.52

we can either store it or we can do

341.22

something with it so let's go ahead for

342.78

now and we're going to actually do

344.639

a

347.039

we actually put this together a little

348.539

bit so what we're going to do is we're

349.62

going to do uh we're going to call Base

351.12

URL

354.66

and it's going to be equal to that guy

357.66

and it's going to get us all the way up

360.18

to

362.46

let's do here we're going to actually

364.68

break this down a little bit

366.24

and then we're going to have the um

370.44

let's say get

372.24

short

374.22

let's do the

376.919

short end point

381.479

it's just going to be

386.94

shorten

391.62

now this is going to be for the get this

393.72

is going to be a little different we do

394.979

the the put and we'll show that in a

396.78

minute

397.56

so what we want to do here is we're

399.12

going to do let's see

402.3

the URL equals

406.139

input

410.94

which URL

416.4

push to use

424.68

and then we're going to have a little

426.06

function that we're going to call

428

get resolve get short uh no get

432.78

real

434.4

get real URL let's do get full URL

440.759

and what it's going to do is it's going

442.8

to take

443.94

a URL

446.58

push these up here

450.12

and then we're gonna do this and then

452.039

we're going to take this stuff

456.84

notice we don't need to worry about a

458.099

payload or headers this would be if you

460.139

were doing something like let me do this

463.02

real quick uh if you're doing something

464.759

like having form date or something which

466.199

we're going to show in a minute when we

467.639

do the post

469.44

so we're gonna come in here we're going

471

to say which URL do you wish to use

473.639

and then it's going to take the URL

477

and this is going to be pretty simple

480.12

um

482.039

response.txt

485.12

and

486.9

which is assuming it's we should

488.52

probably are going to want to wrap some

489.539

stuff around it but we'll check that in

490.919

a minute uh so print let's see get for a

494.22

little URL

500.699

so now if we do this

503.4

and we say

507.18

what is URL you wish to use and so we

509.759

could use

513.3

short three

516.419

and it's going to blow up because it

518.039

doesn't like what we said it so let's go

519.599

take a look at this because this is

520.979

going to be

523.919

where we're going to start running into

525.3

some issues so let's go look at it so

526.8

let's see

528.98

connection refused

534.12

oh wait

538.38

uh

540.6

whoops

543.54

oh that's the problem is because here uh

547.38

let's see so URL

549.54

is actually going to be so the uh

553.92

let's call that API URL

556.38

oops so it's going to be equal to

560.339

base URL

565.86

Plus

567.959

a short endpoint

569.64

whoops

570.72

if I can write that oh there we go

572.519

that's it short end point

574.56

and so that gives me that plus short and

576.42

then I'm going to need to do plus

579.779

Uh u r l equals

583.5

and then I'm going to add the URL

586.26

so now if I run it

591.54

um

592.68

see this now if I run it

595.5

with the three boom it gives me

598.019

that if I run it with a

602.94

if I run it and I do things same thing

604.74

and I give it a two

606.3

then notice I get a different

609.48

let's do it again so if I do that and if

611.88

I call it with a 3 notice I either get

614.16

the https or I get this one

618

so I get something a little bit

620.279

different from each of these now what I

621.779

want to do here is now I want to take

624.3

this out because in an API call what you

626.1

want to do

627.779

is you want to have something that's a

629.459

little more

631.5

valuable essentially in an API you don't

634.2

want to just send strings back like this

635.94

so what we really want to do here

638.94

is we're going to come in and we aren't

640.44

sure well we're going to we'll hold this

642.36

off until our next episode we're going

643.56

to talk about cleaning these up a little

644.76

bit

645.839

what you've got here is that is our

647.82

where'd that go collar so this is going

650.22

to be to get for the get full URL but

653.1

now what we want to do is we're going to

654.54

do something similar that is going to be

657.42

um

658.8

Escape get shortened

661.44

URL

663.12

and now for this we're still going to

664.8

cheat because this is so easy to do so I

667.74

come over here

668.959

for the Post

672.98

which there we go we're going to take

676.14

that

677.7

and it's going to look about the same

682.44

but what you're going to see here

689.519

return response that text

693.18

so you can see the same kind of thing

694.62

except because it's the the URL is going

698.1

to be the same combo

702.12

so we can do it like this

704.1

we don't need that we already have

705.54

requests up at the top so we don't need

707.1

that again

708.3

our payload now is going to be and so

711.959

this is that form we're setting the URL

713.94

equal to

716.399

the URL that they send in

719.339

so this is where we have a payload we

721.5

have for running files we're not seeing

722.82

those in we're not sending anything

723.959

that's a header of any sort

727.56

come in make this the API URL but

730.5

because it's a post we're going to do

732

that just to be clean

733.62

we could send some of these things and

735.24

work with them that's beyond what we're

737.339

going to talk about at this point and

738.42

they're going to return the response

739.86

text

741

so now if I come in and say

744.24

let's see

745.92

we're going to do that we're going to

747.06

print it and then we're going to come

748.079

out and say which well do you wish to

750.6

shorten

754.079

and it's going to do get

759.18

shortened URL

761.16

uh we'll do your the URL again

764.82

so now

766.8

first one we do we'll just do let's get

769.68

number four and see what that was

772.32

so we get number four

775.62

then that's what it was but if we take

778.62

this long thing

785.76

here and say that's what we want to

787.74

shorten

789.6

then we're going to get short five so

791.639

now to see do we get that back

794.22

if we call

796.2

short five

797.88

oh which is this we can just take this

803.54

and

805.079

uh oh and then it kicked it back here

807.06

this is what you sent short five this is

810.12

the actual URL okay

813

so

814.2

we have fairly easily and this is the

816.899

the breakdown of what we do is she

818.519

people with requests all you have to do

820.74

if you note is you're going to on

822.36

requests make a request tell it to type

825.66

and we'll talk another time about

827.04

various types are out there you send it

830.16

to whatever its address is in this case

832.079

it's our API address send some headers

834.839

if you need to payload which you might

836.82

need to do like for example in a form

839.579

and then it's going to send you a

841.26

response back based on whatever is

843.6

returned by that by that API call

847.38

so this is really simple and as I

849.42

mentioned before and I've shown you if

852.42

you want to use Postman that is going to

854.1

be the fastest way to get you just like

855.779

flying through setting up both sides and

858.48

testing your API which what we want to

861.18

do with our shortener here is to just

863.7

get this thing working because then we

866.399

can put it out on our server and have

868.56

our own little API our own little urls

872.22

shortener that we're going to need so

874.139

we're going to come back next episode or

875.459

we're going to clean these things up a

877.139

little bit and start tying them back

878.579

into the database but for now go out

880.92

there and have yourself a great day and

882.24

we will talk to you next time

885.42

hello this is Rob with developmentor

887.82

also known as building better developers

889.92

wanted to announce that we have

892.339

school.developmentor.com feel free to

894.6

check it out if you like any of this

896.76

information any of the content that

898.68

we've sent and you would like to see

899.88

more you can come out you can enroll for

901.98

free we have free courses we've got

904.26

places for you to get better at just

906.899

learning a technology our how to's you

909.6

can work on your business skills we can

911.639

help you with becoming a better

913.079

developer as encoding and things like

915.48

that a lot of the stuff you've seen on

917.16

YouTube we also have out at

919.339

school.development or we just have it a

921.6

little more of a educational format and

924.12

a way for you to track your progress as

926.639

you move forward becoming a better

928.8

developer

939.079

thank you