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
Learn how APIs are used in real world applications, and how they differ from traditional server side applications.
JavaScript's native Fetch API and the Axios library are used to perform requests for resources on behalf of a client. For example, if you were building an application using a popular front end framework like React, you might use Fetch or Axios to pull in data from a REST API into your React application.
To get a better sense
of what a REST API is,
0:00
let's first talk about how information
is transferred on the web.
0:02
First, a client requests information
from a server using a URL.
0:05
Client is a word you'll
hear a lot in this course.
0:09
It's used to describe an application,
often JavaScript running in a web browser
0:12
that is requesting
information from a server.
0:16
The REST in REST API stands for
Representational State Transfer, and
0:18
it's essentially a set of ideas about how
the server should respond to a request
0:23
from a client.
0:27
A traditional server side
application responds with HTML.
0:28
A REST API simply responds with data.
0:31
It's then up to another application,
a front-end application built with
0:34
a framework like React or
Vue, or a mobile device, or
0:39
even a command line tool to format and
display the data from the API.
0:42
Let's look at a couple
of examples of an API.
0:47
The Random User Generator,
as the name suggests,
0:49
generates information about random users.
0:51
This could be useful if you need mock
data to design and build user interfaces.
0:54
Let's take a quick look
at the documentation.
0:58
Here under the how to use section, we can
see that if you make a request to this
1:02
URL, randomuser.me/api, you'll receive
data about a randomly generated user.
1:08
If we make a request to this URL
directly from the browser, so
1:14
let's copy and paste this into a new tab,
1:18
You can see we'll receive a JSON object
with a ton of information about a user,
1:22
first and last name, location, email,
fake login information, and so on.
1:27
This is where the flexibility
of a REST API comes in.
1:32
Once we’ve requested this data,
we can do whatever we want with it,
1:35
like build the example user
interface shown on the front page.
1:40
Let’s look at one more example,
the GitHub API.
1:44
I’ll go to developer.github.com.
1:47
From the Docs menu at the top of the page,
I'll choose REST API v3.
1:53
Don't worry if what you see is
different than what's on my screen.
1:58
Things are always changing and
this is just a demonstration.
2:01
Notice here if we click
on Current version,
2:04
this is the root URL for
the GitHub API, api.github.com.
2:08
We can add to this URL to receive
virtually any information we want as JSON.
2:13
On the right side of the screen,
I'll click on Users.
2:18
And if you look here
under Get a single user,
2:21
you can see that we can request any
single user by tacking on a username.
2:24
This colon indicates that this is
a placeholder or parameter in the URL.
2:29
I can replace :username with
any username on GitHub and
2:34
get back that user’s information.
2:37
So in my browser, I’ll go to
2:40
api.github.com/users/treehouse.
2:43
And you can see that I get back a bunch
of information about the treehouse GitHub
2:49
account.
2:53
In REST API speak,
this is known as requesting a resource,
2:53
the GitHub data in this
case being the resource.
2:57
The URL from which I request
the resource is called an endpoint.
3:00
By entering this URL into the browser,
I'm making a GET request.
3:04
And in response,
3:08
GitHub's REST API is sending me
back the information I requested.
3:09
In order to do anything
useful with this information,
3:13
though, I need a way to make a GET
request programmatically with jQuery,
3:17
the Fetch API, or a library like Axios.
3:22
For information and examples,
see the teacher's notes.
3:24
As you can see by looking
at the menu on the right,
3:27
you can do a lot more with the GitHub API.
3:30
You can access data about repositories,
pull requests, issues, projects,
3:32
organizations, pretty much any kind of
data you can think of is available for
3:37
you to request and
display however you'd like.
3:42
You need to sign up for Treehouse in order to download course files.
Sign up