API or Application Programming Interface is a messenger that takes the request and tells the system
what to do.
Huh?
That doesn't mean much, does it?
In this video, I'm going to explain to you exactly what an API is without all this confusing wording.
I have over here "skyscanner".
If you've never used it, it's an Aggregator Web site.
I look for a flight from LA to New York and it searches all the Web sites all the airlines for the
best deals for me.
Now how is it able to do that?
It found all these flights from different Web sites. But Skyscanner doesn't really have the information.
I mean this information is somewhere in the database of each individual airline.
But as an airline you're interested in having this data on Skyscanner because people like myself that
want to book a flight might not know to go to AmericanAirlines.com
So by having their information listed on a website like this.
Well now they can increase their revenue.
So how is Skyscanner able to do this without having this information themselves?
Well,
That's where API is coming.
If you remember, APIs is what allows strangers to speak to each other, so that we aren't in a closed
system.
We can share data and work with each other and APIs' what allows machines, computers to communicate.
This might sound familiar it's kind of like how we had web browsers speaking with
servers, right?
Well yeah because API is a broad topic.
It applies to the way machines communicate.
If I open up my console here, well, I always mentioned that there is a window object. and the window object
has fetch. and fetch is an API.
It is given by the browser to use, so that I can communicate with this machine that the console can communicate
with the browser. In the same way,
although Skyscanner doesn't have access to the databases of American Airlines and Alaska Airlines all
the other airlines use APIs or give access to this information through APIs.
So that Web sites like Skyscanner can have the information as well and listed on their websites.
The best way to think about it is kind of like a waiter. You're sitting at a table about to order and
the waiter is the messenger that tells the kitchen what to do.
The kitchen is providing the waiter the restaurant, right?
The restaurant is saying "Hey, waiter, these are the menu items.
Please go ask the customer what he or she wants and let us know so that we can prepare."
And this might sound similar to this, right?
We're asking another machine for something using -let's say a waiter- and this machine gives us a response.
If we talk to it in a certain way that it expects. And yeah, a server has APIs ways they can communicate with it.
such as doing "users/about". In the same way,
we also have our code previously from when we built Robofriends. React which is a library built by
Facebook exposes an API a way to communicate with the React package
by doing react Component and using JSX. They tell us how to communicate with it and we're able to communicate
with it.
Hmm- let's, let's work on this a little bit more.
What about- what about this?
Is this an API?
It's a back of a TV.
Yeah, it's an API. A TV works in its own way but it gives you access to a couple of things such as the HDMI cable,
so you can plug into it and communicate with it.
Such as displaying your laptop into the TV. And we've been using APIs in the Robofriends demonstration as well.
We already talked about React, but you remember the JSON placeholder? the users that we got?
Well, if I go to their website, we see that it's a fake Online REST API for Testing and Prototyping.
It's an API, so it must give us
It must be giving us information about something that it has. If we go down,
we see that it has resources and the API tells us we can access them using these methods.
So by just writing "comments" here, I should be able to get 500 comments, and there you go.
I get a response in JSON,
And even with RoboHash,
Remember how we were able to get images of robots from this robohash.org? That's an API.
We can say robahash whatever.
And because they've said that the way to access this API is to just type in random numbers after robohash.org
we're able to access it. And people build these APIs usually for business reasons such
as airline companies, right?
And they have the incentive to comply to industry standards, such as having JSON responses.
And that way it makes it easier for a developer like myself to grab bits and pieces from different places.
For example here, I have the github API.
You can read up on it but I can access information from githup.
So I'm accessing the github repo of Facebook, and it tells me how many followers it has, what bio
it has.
If I go to apple, I can get the apple username as well.
And you can see that it's in JSON format because that is the standard.
If we want to build something with github, well, we can just send a request to this get this data and
create our Web site based on this data.
But it's not only JSON. If you remember there's xml as Well. This is a sample API that shows us
a breakfast menu and here we can request this data through HTTP and we get breakfast menu.
So as you can see APIs are not just web sites.
A TV's inputs and outputs,
That's an API. A NPM package can be an API.
Like I've said before it's a way that people communicate or machines communicate and share information.
Without it we really wouldn't have what we have now with the Worldwide Web.
But how is this useful to us?
Well, first off we can build something like this quite easily.
We can actually search API jetBlue, API American Airlines. And they'll have information of how we can
access this data so we can build our own thing.
This web site all it's doing is making in HTTP request to something like here.
And then grabbing this data and just adding some CSS to it to make it look nice. And there are ton of
APIs online you can search them,
you can read up on them.
Big companies like Facebook have APIs, Google has APIs, you might know Google Maps anytime you see
Google Maps on a Web site,
It's using the Google API.
We also have a silly API like this one, one of my favorites, it's the Star Wars API. It shows you over
here, if I do "people/2/", I get "C-3PO". If I do "people/3/", I get "R2-D2".
I can also do "planets".
This is a free nice to use API, so you can build a sample app using the Star Wars API and all you need
to do is just fetch this resource, get the JSON converted to a JavaScript object and then style it and
use it in your app whichever way you want.
There's also some really important API is that if you're building serious web sites are really useful.
This is the "stripe API".
If you want to incorporate payments into your website, well, this shows you exactly what to do.
And you can add to your server within whichever language you want.
In our case, we're interested in Node and it tells you how you can add "stripe" as a payment method on
your server.
It sounds scary but it's all documented in here.
It's step by step instructions of how to do it.
Another one that I really like is the "Twilio API". And Twilio allows sending of messages on phones. So using
something simple like this you can see there's not much to it other than using this client.messages.create
that they provide.
And we just say what we want the body of the text to be, to which number, from which number, if we want
to include maybe an a heart, then we can add that as well.
And let's say when a user signs up for a service. Well, we can send them a message by saying "hey we've
created a new user.
They've provided us their phone number.
Send them a text message."
Now you might be thinking this is almost too good to be true.
You have so much power now you can just research APIs.
And all the major Web sites have APIs data that they give to you.
Well things like robohash, and Star Wars API. They're silly and they're fun and there's no real cost
to them.
But when you start building real applications and companies like stripe and Twilio where there's some
real engineering effort behind creating these. Well, they charge. They charge a service and that's when
you use something like an API key. And an API key looks like jibberish.
It is a key, a private key so that every time you access their API they know that it's you.
And because let's say you have a plan for making a thousand requests. They charge you because they can
recognize you saying that "oh, this is Andreas key.
He's the one making the request from a server and we're going to charge him for it". And don't worry.
In our final project we're going to be using a really really cool machine learning API and we're going
to generate our API key and actually use it.
Luckily for us though it's free.
So it's going to be a lot of fun.
But from this video I want you to remember this. A server somewhere in the world for a company provides
an API. In order for other people to use it,
they usually tell you how to use it.
And they have documentation on how to use it.
But most of them comply with the standard like JSON to make it easier for developers like us to use.
And at the end of the day, an API is a tool used for data communication.
It's how a program or software or anything running code allows others to communicate with it.
It opens up a big possibility of making websites that do more than just list robots.
You can now with the same principles build Web sites that incorporate different APIs to have the functionality
of something like Skyscanner.
I'll see you in the next one.
Byebye