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
API stands for "Application Programming Interface", and believe it or not, we are already using them. But what exactly does that mean, and what about APIs over the web?
Definitions
- API: Application Programming Interface - the interface through which an SDK or system is used by our code
- SDK: Software Development Kit - all the pieces required for a software system
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
Not so long ago making our weather app or
frankly, many websites,
0:00
apps, or services, would have been
incredibly challenging because
0:05
access to unified weather
data wasn't available.
0:09
We would of had to go through the
cumbersome task of pulling data together
0:13
from different sources.
0:17
Imagine trying to get temperature,
humidity, precipitation, and
0:18
more from locations all over the world.
0:23
[SOUND] What a task.
0:25
Fortunately for us, someone else
has already tackled this task and
0:28
made the information
available through a service.
0:32
Better yet, it's free for small apps, and
0:36
if our app goes viral,
the fee still isn't too large.
0:39
We take another look at darksky.net,
scroll back down to our Dark Sky API link.
0:44
Scroll down just a little bit here,
there's a learn more button.
0:51
Here we're taken to a page that tells
us about the API and how we can use it.
0:57
What exactly is an API though?
1:01
API stands for
application programming interface.
1:04
It's the interface we use to write
code against all sorts of systems.
1:08
We've actually been working
with APIs all along.
1:13
For example, in previous Android courses,
1:16
we used Android APIs to interact
with activities, views, and buttons.
1:19
When we look at documentation for a class,
1:24
we're looking at
the Android API reference docs.
1:27
An API is literally the interface used
by our code to interact with the system.
1:30
These interfaces can be
a separate third party system,
1:35
like Dark Sky, or
smaller subsystems in Android.
1:39
Such as the parts involved to
determine our physical location.
1:42
Although it may seem very different to us,
using a third party API is very similar to
1:46
using the Android APIs we're
already comfortable with.
1:51
Our interactions with a third party
API will just be slightly different.
1:54
We'll also need to discover what's
available for us from the API.
1:59
Before we jump into that though,
let's take a few moments to talk about
2:04
the concept of an interface in
application programming interface.
2:08
In different fields people
use the term black box,
2:13
to refer to a thing that does some work
that we can't see from the outside.
2:16
A black box, in this context,
is simply something that we,
2:21
as developers,
don't need to concern ourselves with.
2:24
Let's think outside
the world of programming and
2:28
use a bakery as an example.
2:31
The bakery's sous chef is looking
at a recipe for a particular item.
2:32
She goes out to the market,
purchases the ingredients, and
2:36
delivers them back to the bakery.
2:40
The bakery is our black box.
2:42
As far as we're concerned,
ingredients go into the bakery and
2:44
come out as a delicious baked item.
2:48
For example, our bakery gets an order for
a specific cake, a gateau fraisier.
2:50
If our sous chef brings eggs,
flour, sugar, baking powder, cream,
2:56
milk, vanilla, almond paste, and
strawberries from the market,
3:01
we can expect to get the classic
french strawberry cake.
3:06
We aren't necessarily concerned with
how this particular bakery goes about
3:10
producing the final product.
3:14
We don't need to know their exact
procedure for making the sponge cake,
3:16
pastry cream, etc,
as long as the output the gateau fraisier,
3:21
is an agreed upon outcome by
both the bakery and customer.
3:24
Similarly, if this particular bakery
goes out of business, we can replace
3:28
this bakery with another one, and
should expect to get the same product.
3:32
This is similar in nature to how
things are modeled with an API.
3:37
We provide a service some inputs,
our ingredients,
3:40
to the API, the bakery in our example.
3:44
Which does some work and
provides us with some output, our cake.
3:46
How does this help us as developers,
though?
3:50
Let's imagine that in our application,
3:53
we have a method that handles getting us
a strawberry cake called getGateauFraise.
3:56
Whenever we want to get some cake,
we call the method,
4:01
which handles how we go about this.
4:04
If we had been using Gabriel's Bakery for
our cake, and then, for
4:06
whatever reason,
we want to change it to Louie's bakery,
4:10
we can do that inside our
getGateauFraise method.
4:13
We can still call and use it the same
way without changing our inputs, and
4:16
still get the same outputs.
4:20
Pretty convenient and powerful, right?
4:22
This makes code and project maintenance,
well, a piece of cake.
4:24
Let's think of a truly different idea.
4:29
We could change the getGateauFraise method
to interface with a 24th-century food
4:31
replicator, to get
an instantaneous strawberry cake.
4:36
Okay, that's a bit out of this world, but
4:39
it shows the power of
the interface concept.
4:41
We don't need to change anything
with our inputs and outputs, but
4:44
the work being done inside
the black box is very different.
4:48
The best part?
4:51
The rest of our code doesn't care.
4:52
Okay, let's get back to reality a bit.
4:55
Forget about our food replicator and,
sadly, cake altogether, and
4:57
get back to grabbing data from the web.
5:02
In programming, this black box concept
shows up in a lot of different ways.
5:04
A well-designed system is programmed so
5:09
that the interfaces between
components are clearly defined.
5:12
Each component then can look,
and act, like a black box for
5:16
the other parts of the system.
5:20
let's change this diagram to reflect what
we'll be building in stormy with Dark Sky.
5:22
We'll utilize the Dark Sky API and
pass in an API key,
5:27
along with a location as our input.
5:31
Our black box then will be
a getForecast method, and
5:34
that method will get the forecast
from the internet and
5:37
output the forecast data to use and
display in our app.
5:40
Actually, there are two important
interfaces at work here.
5:44
The first is the interface
of the getForecast method,
5:47
which includes the properties we'll need.
5:51
The second interface, is actually between
getForecast and the Dark Sky API,
5:53
this is what's known as a web interface.
5:58
One last thing to mention here, hopefully
it provides some additional clarity.
6:01
We also talk about the Android SDK,
or software development kit.
6:06
What's the difference between an SDK and
an API?
6:10
Well, an API is part of an SDK.
6:14
Recall that a API is just the interface,
6:16
the interface to the rest of
the SDK where all the work is done.
6:20
The API is the way in which
you use something, and
6:24
the SDK is where the work is accomplished.
6:27
Sometimes we use an API alone
without any access to the underlying
6:30
components of the SDK.
6:34
Which is what we're doing when we
request data from the Dark Sky API.
6:36
We simply make a request to their
interface without needing anything else.
6:40
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