query

REST API concepts and examples



Sharing buttons:

welcome today I want to talk about what

an API is and why it's important in web

development API stands for application

programming interface and basically it's

something that allows one piece of

software to talk to another now there's

lots of different kinds of api's but

when you hear people talk about

Twitter's API or Google's API what

they're talking about is a REST API and

a REST API stands for representational

state transfer now it doesn't have to be

the case but usually a REST API works

pretty much the same way a website does

you make a call from a client to a

server and you get data back over the

HTTP protocol now I think one of the

best ways to show the many similarities

between a REST API call and loading a

normal web page can be found with

Facebook's graph API for example let's

pull up YouTube's face bowd facebook.com

slash YouTube and we're all familiar

with what a facebook page looks like it

shows how many likes the YouTube page

has things like that but now let's

change the WW part to graph dot

facebook.com slash YouTube and what we

get back is a response to our API

request we've made an API request in our

browser to Facebook's graph API now what

we get back might appear to be gibberish

to the human eye but it's actually

JavaScript object notation or JSON

formatted data it's structured data

organized according to key value pairs

the same way an Excel spreadsheet is

structured with key value pairs and you

might ask for the data that's in cell

a16 you can ask a JSON array for the

data if you want to know how many likes

this Facebook page had for the data

contained under the key likes and all

modern programming languages will be

able to interpret this JSON response one

more concept I want to introduce is the

idea of parameters let's reload that

same Facebook API request but this time

let's add fields equal ID name and likes

now when we refresh the page you see

that only ID name and likes have been

returned that's because these parameters

have filtered the data that we get out

of this response now let's take a look

at another API example one that I

think's really cool is provided by

Google Maps and it allows you to take a

city name

or even an address and turn it into a

set of GPS coordinates as comm / maps /

api / geocodes last jason so if you

remember what we just talked about the

server that we're calling is maps out

Google API is calm and then the

particular resource we've drilled down

it's the Maps resource and then the API

resource and then the geo code resource

and we've even added Jason as a resource

and that's because the Google Maps API

can return data in a number of different

formats and then we'll add the following

parameters will add address equals

Chicago and sensor equals false and as

you can see we got another JSON response

and if we look in the JSON array and we

go to the key results and then the key

geometry and then the key location we

see the latitude and longitude

coordinates for the city of Chicago now

that's great in oli we got the geo

location coordinates for Chicago but

what do we do with them

well let's mash to api's together and

take those geo coordinates over to

instagram and plug them into instagrams

media search in point so in order to

access the Instagram media search in

point without having to write any code

we're gonna head over to Instagram comm

slash developers and then on the left

hand side click on API console and we'll

be able to use this API console that's

provided by API G or Apogee in order to

make requests to the Instagram API

without needing to write any code and

you can see if you click on the drop

down on the Left there's a whole bunch

of API is that this thing is set up to

allow you to play with but for now we're

gonna use Instagram and we're gonna be

using the Instagram media search in

point which you can see is that API

Instagram comm / version 1 v 1 / media /

search and then we're gonna set the

following parameters and you can see

that they're added to our URL request up

at the top of the screen we're gonna set

lat equal to 41.87 and longitude equal

to negative 87 0.62 and we'll set a

distance of 20 meters we click send and

we get back this information about a

request we see it's a get request and it

shows where it was sent and what

parameters we passed in one of the

parameters that we didn't say it was the

access token because that was

automatically set by the apogee

interface and then below that in sort of

the blue and purple you have key value

pairs of header

that we sent as part of our request and

then if you look over at the response

you can see we got an HTTP status code

of 200 which means success that

everything went okay and then there's

some header information as well we've

got ex rate limit limit which is 5,000

which is the total number of requests

you can make to the Instagram API using

one access token during one rate limit

period and then down below you can see

x-ray limit remaining which is a 40,000

994 which is how many requests we have

remaining in this rate limit window and

then there's some information about

cookies and the content-type application

JSON things like that

and then if we scroll down we'll see the

body of the response which is a JSON

encoded array of all the images that

matched the geo coordinates that we

passed in and there's information such

as you know what filter was used in

order to take the integrand picture how

many likes it receives how many comments

information about the user who posted it

how many followers they have things like

that and then of course there's also the

images themselves and if we copy that

image link and we take it up into our

you all browser and we paste it in then

you can see there's a picture from that

location in Chicago you can even see the

Chicago skyline in the background pretty

cool right there's literally thousands

of api's out there that you can tap into

and take their data and mash it up pass

it to another API chances are whatever

website you want to work with they have

some kind of API that you can use in

order to consume their data api's that

are available

check out programmable web comm up until

now we've only been consuming data from

api's but you can also write data to

api's but before we go down that road we

need to talk a little bit about the

concept of HTTP request methods I've

linked to documentation in the

description below but the big two that

you really need to know are get and post

now a get request is what you use to

consumed it and that's what you've seen

us do so far by passing these URL

parameters in order to get data back

from the API but a post request for

writing data to the API in the best

practice is to actually put the data in

the body of the request now normal web

browser doesn't allow you to put data in

the body of a request but what you can

do is you can install this handy

extension called postman restclient the

nice thing about working with postman is

you can make more complex API requests

for example you can choose any one of

the available HTTP requests method

and you can see a list of them here

second you can add a body to your HTTP

request and finally you can add headers

so let's use the postman client in order

to send a tweet out over the Twitter API

before we do that though let's talk

briefly about the concept of

authentication obviously you need to

give some kind of event ocation if

you're gonna be saying tweets out

because otherwise you could send tweets

from my Twitter account and I could send

tweets from yours and what a lot of

these big online websites are using for

their authentication is what's known as

oo auth or OAuth 2 let me just say

briefly basically what you're doing is

you're getting credentials kind of like

a username and password although they're

called a client ID and a client secret

and then you're exchanging those for

what's known as an access token and then

you pass that access token to Twitter

and Twitter knows that the request to

make the tweet is coming from you so it

sends out the tweet from your Twitter

account okay so let's go ahead and send

out a test tweet using the postman rest

client we'll select post as our HTTP

request method and then you'll see that

the server rack sesang is at API Twitter

comm and then as we drill down to our

tweet sending resource we got our at

version 1.1 of the Twitter API and then

slash statuses slash update dot JSON and

again that tells the Twitter API that we

want JSON formatted data as a response

and we've set the authorization header

to our olaf 1.0 credentials and then

down in the body of our post request

we've set the key status to test tweet

from postman and when we click send

you'll see that there's information

about what time the tweet was created

the tweet ID that it was assigned

information about my Twitter account of

the Twitter account that the tweet was

sent from and then if we click over on

my timeline and we refresh the page

you'll see sure enough that the test

suite we sent out shows up

[Music]

you