Intro to APIs using Python! Build a Weather Look-Up Application!

LeMaster Tech
182 followers
16 Video Views·Apr 21, 2025
This video introduces the concept of APIs (Application Programming Interfaces) in Python Code. The example in this video uses a free weather API from openweathermap.org that allows the user to look up a wide variety of weather information for any city!
APIs are extremely versatile tools allowing for large database lookups and a variety of data processing and data analytics applications! What sort of things are you looking to do with APIs?? Let me know in the comments below!
Be sure to check out the channel for tons of other tutorials! Leave a like if you found this video useful and don't forget to subscribe to the channel to stay up to date on all the latest!!
Thanks for watching and good luck with your code!
what's going on you guys welcome back to
the channel and today's video is going
to be an introduction to apis in python
by building a weather lookup app
[Music]
okay so if you're unfamiliar with what
an api is it basically stands for
application programming interface
and it's really any plug-in that you can
uh use in your programming to gather
information from the outside world so
data sets exist on the internet of
sports statistics weather statistics all
sorts of information is out there
and to look it up and use it inside your
python files you use what's called an
api
and so uh to use apis in python the
first thing you're always going to want
to import is the requests module and so
if you don't have it installed run pip
install requests and you'll need it
inside of your file and then the second
thing is depending on the api
application that you're using some of
them require an api key some of them are
totally free to use even without
credentials
the one i'll be doing in this video is a
weather lookup program and it's
completely free to use but if you go to
openweathermap.org
it is going to want you to sign in to
give you an api key so uh all you have
to do is make an account um give it an
email and a username and then they're
going to email you an api key it's still
totally free to use they just require an
account
that's what we'll be doing in this video
i have my api key stored in another uh
dot pi file in my program just because
um i would rather everybody who watches
this video not use my api key because
it's only like 60 lookups per
hour um so just go get your own it's
completely free
but we're going to be using that in the
program so what i'm going to do is i'm
going to just real quickly set my api
key equal to the variable that i made in
my other dot pi file
it's this api key dot weather but what
yours is probably going to look like
is something like this i'll copy in an
example so this is just a made up one
just to see the format but you're going
to get an email with something very
similar to this
and you should put your api key in this
variable
for the rest of the tutorial um just
know that this is where you want to
store your api key
okay
so um what we'll do we're going to
create a and on openweather.com after
you do the tutorial there's a whole
bunch of different um types of api calls
that you can do so you could look up
whether by just city name you can look
up whether by latitude and longitude zip
code all these different things and
they're all slightly different api calls
and they give you the format of how to
do each of those on the api that's
pretty typical
but what we're going to do is we're
going to do it by city name so what
we'll do is we'll create an input for
the user to enter their city name so
we'll just say
enter your
city name
and we'll get that right at the
beginning and then what we're going to
do is set this variable that we'll call
link equal to what we need to be able to
look it up
and what's nice again going back to the
website they tell you the exact call
that you need to make for your api
so we can grab this and copy it down
into our
program
and
put
quotes around that so it's a string and
go ahead and make this a formatted
string because what we're going to do
is
make these the same as our variable
names and you can see because i i kind
of knew what we were doing um
i already sort of had names that would
fit right in there with them city name
and api key
and that's really all we have to do for
the base of it but what you'll see and
you don't have to do it right now if you
want to get through the tutorial and
then see this this is going to give us
the
weather in standard units which is going
to be kelvin and most of you regardless
of where you're watching from are going
to want this in fahrenheit or
celsius
so what you're going to want to do is
and
units and then set it equal to imperial
or metric depending what side of the
pond you're on and
then that's all you have to do now we're
actually going to call that link so this
is where that requests module comes in
it's going to be response
equals
and that's basically apis there's a call
and a response so what we're doing is
we're going to call for information
using this requests.get
call and then we're going to put the
information that we get back into a
variable that's typically called
response but you can call it whatever
you want
um and then what we put in there is our
link so you could have put all of this
inside of the dot get if you wanted to
it's a little bit cleaner to put it in
an intermediate variable
and then to just format it properly
you're going to want to do dot json with
the parentheses and if this is your
complete first introduction to apis
don't worry about the dot json too much
it just stands for javascript
object notation
and it's it's just a formatting of how
almost all apis
present their data it essentially looks
a lot like a python library
but that's a little more in-depth than
we're gonna get in this tutorial
and then what we're gonna do uh for now
is we'll just print the response okay
not in parentheses so i want you to see
what's going on if we just do this step
for now so let's go ahead and run it
api intro
enter our city name and i'm in
pittsburgh do you know wherever you are
whatever you want to do
and what's cool is it gives you
coordinates of your city it gives you
weather information so you can see
clouds uh it gives you the description
it's overcast
it'll tell you things about
um
all sorts of stuff but you can see
there's this main section
that's got uh what we want in it i guess
what i i'm really interested in is the
current temperature and it gives us a
few things here temp feels like minimum
maximum
gives some pressure and humidity and
this is where you can really look
through all the data you're getting and
decide what you think is most important
um and you can kind of parse through it
but what i want to do is i want to take
a look here and see okay in this main
section there's then temp feels like
temp min and temp max so let's use those
and now
that data can essentially be called as
if it was a python library so response
main which is going to get us into that
main section and then this is similar to
an index within an index so like a list
within a list
because now we want the specific
temperature
and we got to put that in a variable and
we'll say temp
so then feels like we'll do the same
thing for all four of those
feels like is coming out of the response
main section as well but that was a
different uh key so we're putting in the
dictionary keys essentially um
feels like
and then we want to do temp min
equals response
main
temp man
make sure you get your uh
make sure you get all of your
brackets in the correct spot it's very
annoying troubleshooting when you
have
typed everything in properly but you put
a bracket closed at the wrong spot
okay temp min temp max this will be it
and then we'll get back to the fun stuff
main
and
temp
max
okay
so what we did is we just put um four
really important things and i'm gonna go
ahead and pause this for now or uh uh
get just comment out the print statement
for now um
and what i'd like to do is do a printed
formatted string that kind of presents
that data so
i'd like to say at the location
and then use curly brackets and we'll
say city name so wherever we looked it
up comma the temperature is currently
and then
temp
uh and then i'm gonna put units on that
degrees fahrenheit you might if you're
metric you might want to do degree c or
heck you could do kelvin if you wanted
to um but i'm just going to leave it as
this before i put those other three in
just to make sure it's working properly
but so now if i run this
and i say my city is
denver let's do a different city let's
say denver it should just tell us the
temperature in denver currently there we
go 69 degrees wow that is significantly
nicer than pittsburgh
um
okay so now what we can do is we can
kind of say
but it feels like and then do
feels like
and this is getting kind of long so
let's put it on a new line we'll do
backslash n
and then we'll continue and we'll say
there is a we'll put a period after
feels like
there is a max
temp
of
time max
and minimum of
temp
min
today okay now let's go ahead and run
this let's do some
nice place let's say miami
okay so it's currently 80 degrees but it
feels like 79.75
and uh max temp of 83 and min of 77.9
so
this is obviously uh just one example of
what you can do with apis they're
amazing tools for looking up sports
statistics for
looking up any kind of data that exists
in these massive libraries online you
can use it for all sorts of science and
data analytics applications
and this is just your first introduction
to it i hope you found it useful if you
did please consider leaving a like on
the video subscribe to the channel it
helps me out a ton and let me know about
uh what you want to see more of on the
channel in the comments below and if you
had any questions with what you saw here
today feel free to ask them in the
comments below as well and i'll get back
to you as soon as i can as always good
luck with your code and thanks for
watching thanks bye
