Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Set up an account to get your own API key.
API Terms
- Key - an authorization key to let the API know this request is allowed
- Token - an authentication key to let the API know that you are requesting information
API Rate Limits
API Security
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Hello again.
0:00
In this video we're going to work on
gathering data from a weather API.
0:01
In the teacher's notes below you
can find a link to this website.
0:05
Like, many APIs in order to send
a request, we have to create an account.
0:10
This is so they can give us
a special key called an API key.
0:15
That lets the API know the requests
that we're going to make are authorized.
0:20
And the API can also track the number of
requests that you're making to make sure
0:25
you're not abusing their service.
0:28
Most APIs have a rate limit.
0:31
Let's find out what our limit is.
0:34
A lot of this information can be found on
the pricing page or in the documentation.
0:35
So here for
a free account it looks like we can make
0:43
60 calls to the API in a minute,
or a million calls a month.
0:47
Which I think we're definitely
gonna be underneath that limit, so
0:51
we won't need to worry.
0:54
But it's always good to
know that information.
0:55
Now, let's create an account.
0:59
Again, we're creating a free account,
so it's not going to cost you anything,
1:01
you won't be charged anything.
1:05
So you can see here, I'm already on
the sign up page, users slash sign up.
1:07
You can always do sign in, and
then there's a sign up link there as well.
1:12
Go ahead and create a new account, and
I will see you when you're logged in.
1:18
Perfect.
1:23
Now that you're logged in, we're
gonna navigate over here to API keys.
1:24
Now that we're on API keys, you can see
there's already a key here ready for
1:30
you to use.
1:34
This key is unique to you, and
should not be shared with anyone.
1:36
If you accidentally do share it,
you can get a new API key by
1:42
clicking on create key,
just call it practice, generate.
1:47
And then it will give you a new key, and
then you can just delete the old one.
1:52
And this process of creating new keys
a lot of times is also limited in how many
1:57
times you can do it, and
say, like an hour or a day.
2:02
So make sure you keep those keys safe, so
that you don't have to come in here and
2:06
do this a whole bunch.
2:10
So if you want to
continue following along,
2:11
you will need to open up
an account at open weathermap.org.
2:15
And go to your API keys, and copy your
specific API key that's for you, perfect.
2:20
Okay, let's pop into the code.
2:27
I'm going to copy my key.
2:28
Hit minimize.
2:31
Come back over here.
2:33
And let's bring this down, and
I'm gonna add a file, yeah?
2:36
Okay, I'm gonna call this keys.py.
2:41
So inside of here,
I'm gonna do api_key =.
2:44
And this typically needs to be a string,
and I'm going to paste my key in here.
2:48
Hit Save, and then I'm gonna close that
file, cuz I don't need it anymore.
2:53
I'm also going to create
a .gitignore file.
3:00
Make sure I spell that correctly,
and hit Enter.
3:04
And in here I'm gonna
put in our keys.py file.
3:07
I'm also gonna add in
our environment folder.
3:10
So that if I ever create a repo, both of
these things will not go up to GitHub.
3:14
Because I do not want this key to be
public at all for people to see it.
3:21
This is one way of protecting your key and
is great for learning and playing around.
3:26
For greater protection,
environment variables are the way to go.
3:32
I'm not going to get into environment
variables in this workshop,
3:37
but I will put some resources
in the teacher's notes below.
3:41
So that you can learn more about
what they are and how to use them.
3:44
Just to be clear, again,
3:48
how we have our projects set up here with
our API key just sitting in this file.
3:50
Is great for practice, but I wouldn't
recommend this for a live project.
3:55
Now that we have our key,
we're ready to play around.
4:01
So I'm gonna do new file app.py from
4:05
keys import api_key, and save.
4:11
Perfect, we already have our api_key
ready to go in our app.py file.
4:17
See you in the next video.
4:22
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up