This course provides a comprehensive introduction to Application Programming Interfaces (APIs) for beginners, covering their fundamental concepts, practical applications, and how to interact with them using various tools and programming languages. It emphasizes understanding APIs as interfaces that abstract complexity, enabling developers to build powerful applications by leveraging existing services.
Mind Map
Genişletmek için tıkla
Tam etkileşimli Mind Map'i keşfetmek için tıkla
This course is an excellent introduction to APIs for beginners.
This is an updated version of what is already one of
the most popular API courses on the Internet.
Craig Dennis is back as the instructor and in my opinion he is one of the best developer educators out there.
- Hello, I'm Craig and I'm a developer.
In this course, we are going to be exploring
a term that you've probably heard quite a bit.
That term is API or application programming interface.
In addition to being able to recite what it stands for,
when you complete this course,
you'll be able to discuss what an API does.
You'll understand why they exist and you'll be able
to list the many benefits that they provide.
You'll even get some hands-on experience using
a few popular web APIs, which will give you
a taste of what all this excitement is about.
We'll be starting from the very beginning,
so there really isn't too much I'm gonna
be expecting you to know before starting this course.
I would like to request that
you have a touch of experience with coding
and a wee bit of knowledge about the web.
If you've written any program at all,
even the typical first program,
you know the one where you write the words, "Hello, world."
If you've done that in any programming language,
you'll pretty much be ready for this course.
It will be helpful if you have a basic understanding
of how the web works with web servers and browsers,
but don't sweat it, we'll touch on that too.
Now, if you haven't met these requirements, please check
the notes attached to this video for where to get started
with some beginning web and programming concepts.
That reminds me, you should take some time
to get used to your learning environment.
The notes that I just discussed are attached to this video
and I'll also drop more information into that section
to keep you updated on what might have changed
from my right now to your right now and I'll do my best
to remind you to look in there when there's something
I think you just have to see, but you really
should get in the habit of checking that area out.
There'll be tons of juicy information there.
You might be getting a real bad case of deja vu right now.
This is a refresh of a course from 2019,
which these days seems like about 30 years ago or something.
If you saw that version, welcome back.
Technology is constantly evolving and just updating
the notes wasn't cutting it, so this is version two.
Thanks for all the feedback, by the way,
a lot of yours and your fellow learner's suggestions
are implemented in this new updated version.
All that to say, we got you.
These videos have captions, so if you feel like
you might benefit from having those on, please use them.
This space is all yours.
If you want me to speak slower,
please use the speed controllers to slow me down,
and if I'm taking a long time, speed me up.
I make a pretty good chipmunk.
And last but certainly not least, take advantage
of the fact that you are in a video-based course.
If you need me to repeat myself,
use the video controls to rewind me.
If you feel like you need to take a break to let
something sink in, by all means, please pause me.
I'll be right here waiting for you.
I know that I've gotten into some pretty bad
viewing habits with all this streaming media.
Now, while binging might be a great way
to consume an entire season of your favorite show,
it's not the best way to learn something new.
Education isn't meant to be binged and because of this,
I'll probably remind you quite a bit to take breaks
and encourage you to let what you just learned marinate.
All right, let's get to it.
API, those three little letters
generate quite the buzz, don't they?
First, I'd like to take some time upfront here
and clearly break down what is meant
by application programming interface.
By understanding its purpose and a touch of its history,
I think you'll start to see its importance and begin
to understand why this term keeps popping up in your life.
After we get a good grasp on the what and the why of APIs,
we'll go and learn how to explore two existing ones.
We'll get familiar with several handy tools
that you can use to interact with web-based APIs.
We'll use those tools to hit an external web API
to send actual text messages and we'll get some
inteRESTing data back from a streaming music service API.
Once we get some practice using the APIs, we'll use them
in a web application that you'll actually publish.
The application that we're gonna be building is going
to receive user submitted photos via a text message,
and then we'll use an API to display a collection of all
these photos and you get to come up with the prompt.
Send me the latest photo on your phone, dog pics,
please, what's for dinner, whatever you want.
I can't wait to see what you build
and what your family and friends submit to it.
We are going to be strictly using APIs in this course,
we won't be writing our own.
While you most likely are gonna end up
learning some best practices in API design,
we won't be covering that specifically.
There are many great courses available that show
the creation thought process, and I've linked to some
of my faves in the notes attached to this video.
In my opinion, the best way to start learning about
any acronym is to focus on each individual letter.
For API, I think we should start on
that last character, the I, interface.
Let's talk a bit about the interfaces
that you've seen already.
Interfaces are all around us.
For instance, this radio here, it has a familiar interface.
The interface allows me to do things like change the volume,
change the station, I can turn it off and on.
I literally have no idea how anything inside
this thing works, but still the interface allows me
to control the way that it works through the options
that are exposed and that's pretty great, isn't it?
The magic that is happening is
completely abstracted away from me.
I'm still in control of what has
been determined that I, a user can handle.
Interfaces usually evolve over time,
like an alarm clock radio for example.
It has my familiar volume and station controls
and it also adds some new features like
changing the time and setting the alarm.
I still have no idea how this works implementation wise,
but the interface is something that I can understand.
Usually, and as technology evolves
and new options come into existence,
they make their way into the new versions of the interface.
Still abstracting away things for us,
but enabling us to be in control of the new features.
Sometimes these physical interfaces get so recognizable,
they even make it into GUIs.
Hey, did you hear that?
There it is again, GUI, G-U-I, graphical user interface.
This is my streaming music app on my phone.
There's my familiar Play button in the user interface or UI,
and I know what the Play button does when I press it,
but I have no idea how it does it
and I'm okay with that level of abstraction.
What would I do in public transit without music?
Like, talk to people? Hi, how's it going?
Now, the developer who wrote this
streaming music application, of course,
understands how the Play button works.
She coded the button so that when it gets clicked,
the music starts playing.
Here's the thing though, our developer is also
working with quite a few interfaces that are
abstracting the way some of the details for her.
For instance, she wrote code
that happens when the button is clicked.
She probably did that using an event handler
on the button, like onClick.
What she didn't have to do, however, was write the code
that makes the button interact and change with the press.
She didn't have to write the code
that makes that familiar clicking noise.
It was already there for her.
Buttons are, in fact, an interface.
They provide the developer the means of controlling
its interactions while abstracting way how
that actually works, implementation wise.
This button is part of the API
or application programming interface
and it's available from the application framework
that was used to create the app.
Because most apps need buttons,
this interface has been created and provided
to our developer or application programmer to use.
By implementing the button interface, our developer
was able to make her code run when the button was clicked.
It's worth noting that the code probably also
uses a media player API that is provided with
the operating system of the device like Android or iOS.
This media player API abstracts away needing to understand
details of how to send audio data to hardware.
The developer knows they just want to call
the play method on the media player API.
I suppose too, the song isn't stored on the device,
it's being streamed over the network, so a web-based
API call has been made over the internet to get that data.
It's actually APIs all the way down to the ones
and zeros providing abstractions at many levels,
so let's take a look at what all
of these interfaces have in common.
They all define ways for us to interact or communicate
with an object, whether that object be physical
or software, and as a user of the interface,
we don't need to understand the implementation,
we don't need to know how it works.
We just need to know what we've been allowed to change
or see, interfaces abstract away the implementation.
Now, while the UI or user interface is made for
the user of the application, the API is made
for the application programmer
to use and extend in their applications.
With the I covered in our API,
let's take a deeper look at the REST of the API acronym.
An API is a contract of sorts.
It defines how it is expected to be used
and it defines what you can expect to receive by using it.
Think of it as a set of tools designed
for software developers like yourself to use.
An API is created to help make your life easier
by giving you access to data and by providing you
with an abstraction of the implementation, you know,
not needing to know how it's doing, whatever it's doing.
They save you from needing to create everything yourself,
like when designed well, they make nearly impossible things
happen with just a few lines of code.
Now you're probably starting to see why
it's important to get familiar with them, right?
I want to do a quick word of warning here about some
naming issues that you'll probably run into.
These days, the term API is almost always used
as a way to explain web-based APIs.
In fact, it's probably safe to assume that when
you hear API in the wild, it's most likely
that people are talking about a web-based API.
We'll explore those here in a few.
However, if we focus on all types of APIs
instead of just the web-based subset,
we'll get a better look into why web-based APIs are
so popular, so hang tight, we're getting there.
APIs are everywhere in your software journey just
about everywhere you look, your programming language,
whichever one that might be, provides many APIs.
Let's take a look at one that is common in all of them.
They all provide some way of working with strings,
you know, like a series of characters.
Wanna make your text all uppercase?
Just call the API method and voila.
You could probably create the uppercase string all
by yourself by doing some sort of low level bit math
on each character, but you don't need to
because your programming language has abstracted away
that for you and provided a handy string API.
It exists because this is
a common enough problem that developers run into.
Because these programming language specifications are
just interfaces, different operating systems can supply
their own implementation so that the code
works properly in their environment.
I think an easy way of picturing this is to think
about how different operating systems
like Windows or Mac OS maintain their files.
They're totally different, yet your programming language
provides a single API for working with the file system.
You can write one script
and it works both in Mac and Windows.
It's the implementation that takes care
of all the details for you, pretty cool, right?
Definitely something to be thankful for.
It makes our lives so much easier not having to worry
about what operating system is being used and the importance
of an API tool language cannot be understated.
There have been some fascinating legal cases where
it's been questioned if the language API itself,
again, not the implementation can be copyrighted,
check out the notes for more on that.
Along similar lines, there are different web browsers,
you know, like Google Chrome,
Microsoft Edge, Safari, Firefox, Brave.
Now, you might not have pondered this before,
but isn't it amazing that you can write code
for the web and it will work in any of 'em?
This works because there's a set of web APIs
that web browsers all must implement
in order to make that magic happen for you.
We are standing on the shoulders of giants, folks,
and we really should thank those giants more often.
There are authors and maintainers of libraries
that package up code to solve common problems so
that you don't need to do it yourself.
For instance, do you know how to programmatically
make a photo black and white?
Me neither, but I can download and import
an image package into my application and suddenly I can.
I just call a method and boom,
I wish real life skills worked this way.
I'd import dancing so I could
be more fun at weddings, cha cha cha.
Frameworks provide an API that allow you to extend
what has been provided to make it your own.
Like for example, most web frameworks provide you with
the ability to add a new route to your web application.
It allows you to write out dynamic data
to a template that you specified, and you do all
of this with just a handful of lines of code.
You don't need to understand how it's doing things,
but you do need to know how to write an implementation
to what the framework is expecting.
In this case, you're using the API, the contract,
to write a valid implementation that meets the requirements.
Now, all these APIs that we just talked about,
they're local to our code base, they exist within our code.
We use them to avoid recreating the wheel.
That is to say they assist us in accomplishing tasks
that we need to perform by abstracting away
a ton of the work for us, the stuff that
we developers shouldn't be worrying about.
We should be focusing on the business problem at hand,
and that means a big part of your job is figuring out
how to leverage these APIs to your advantage.
Now, I think this is an excellent time
for you to take a break.
I just gave you a lot to think about
and I'd like you to take some time and breathe.
Why don't you jot down some APIs that you've used today?
Even if you weren't coding,
I know that you've seen some in action.
If you're having a hard time thinking of some,
I'll give you a hint.
You're watching a video right now inside of a web browser.
When we return, we'll explore the concept of remote APIs,
ones that aren't executing locally in our code base
and why that is such a powerful concept.
(upbeat music)
It took me a while to find this remote control.
It was in the couch. It's always on the couch.
Anyways, this remote has an interface on it, right?
There's a Power button and when I press the Channel button,
it changes the channel, when I press the Volume button,
it sends the request to my radio and the volume changes
as if I turned the volume up using the interface on
the device itself, and we know why that's important.
I want to remotely control my device.
I don't want to get up from my chair.
There are of course, more or less lazy reasons as
to why we'd want to remotely control objects.
This cute little robot, for instance, has a remote API.
You can send commands to it and it responds
and it even sends back sensor information.
These traffic signs can change based on an API call.
This drone here is remotely controlled
by an app on my phone, see?
(drone whirring and crashing)
And it's not just physical objects
that benefit from remote APIs.
Oftentimes we don't have space on our local machines
for all the data that's available, like for example,
have you ever used one of those apps
that helps you figure out what song is playing?
You send up a snippet of a song that you heard playing
at a grocery store using your microphone,
and it sends back exactly what song it is.
It's usually Phil Collins.
It turns out that both grocery stores
and yours truly love Phil Collins.
You don't have room for all the songs in
the world on your phone and with this remote API,
you don't need to have all of them.
It does all the searching and processing
and it's surprisingly quick.
How can it possibly do that?
I don't know, but I love the interface. It's so fast.
Ooh, that's another benefit
of remote APIs, computational power.
Because an API removes the limitations of your local device,
you can gain access to seemingly infinite amounts of power.
A good example of this is the AR app of Google Translate.
You can use the camera on your phone
and in near real time it's able to translate directly
to your screen, like instantaneously.
It's using a ton of processing power
to make that magic happen and that processing
is happening elsewhere, "en la Nube."
It took quite a bit of experimentation
and some evolution of ideas, but over time,
we finally turned this remote API dream into a reality.
Now, I'm gonna drop some history into the teacher's notes,
but I just want you to know that the struggle
to achieve this concept of remote APIs was very real.
Now, most of the problem was that
there wasn't a standard that everyone loved.
Things were pretty clunky for a while as
these ideas were sussed out, but then it happened.
A new architectural style appeared that was clear
and allowed us developers to interact
with specific resources over the web
and it quite literally changed everything.
This style rapidly emerged as a winner
and just about everyone began offering
and consuming external APIs over the internet.
Now, what I'm talking about here is REST,
or representational state transfer.
It was the approach that we needed and honestly,
it's the abstraction that we developers deserved.
Like we talked about already, the popularity of REST spread
so rapidly that it nearly completely overtook the term API.
It's like how we use the word Xerox to mean photocopy,
even though Xerox is just a brand of photocopier.
Xerox does other stuff too, I think.
That's how pervasive REST is.
REST is for sure not the end all, be all of remote APIs.
There are definitely some limitations
which we'll discuss here in a bit.
Technology's ever evolving and of course
there's movements that are still advancing
the concept of the best remote APIs.
Some terms you might even have heard, like GraphQL,
more on this in the teacher's notes.
Now, for the rest of this course, we are going to do
a deep dive into APIs that are written using the REST style.
When APIs embrace the style and constraints of REST,
they are said to be RESTful, cute, right?
If you feel like you need a little rest,
go ahead and pause me before we do a deeper dive
into what it means to be RESTful.
Sorry, that was a bad and obvious joke,
but as a developer you kind of have to make it.
You don't really have an option.
Because REST sits on top of web technologies
like a really nice hat, we should probably
do a whirlwind refresher of how the web works.
(upbeat music)
It might have been a while since
you thought about how the web works.
There are a smattering of terms that I'd like
to reload into your working memory before
we talk more about how REST uses them.
So let's use the web, I've got my computer here and on it
is an internet browser, the browser is a web client
and I'm going to use it to connect to a server.
I'm going to do this by putting
a location in the address bar,
a universal resource locator or URL to be exact.
You might also hear this referred to by
its superset term URI, universal Resource Identifier.
Now our URL here has a scheme portion
and this one happens to be HTTP.
HTTP stands for hypertext transfer protocol.
Let's break that down a bit.
Let's grab that last part, protocol.
A protocol is also often likened to a contract.
It's a little lower level than an API,
but it's similar in concept.
You can think of it as defining
the expectations of how to communicate.
Like an example that I think really sticks
with me is if you've ever been to a rally
and someone does one of those chants where
they're like, "When I say 'Fu,' you say "Bar."
Fu, bar, one of those. Here, let's do one.
When I say HTT, you say P, HTT, HTT.
Oh, I just realized I might have made you yell "P" out loud.
Hope you aren't in a coffee shop. Awkward.
Anyways, we, you and I just set up a real quick protocol.
I told you how to respond to my specific request
and you would've been breaking or agreed upon protocol
if you yelled anything else but P, so the browser
creates an HTTP request for you, along with the URI,
it also specifies a particular HTTP verb.
In this case, it is using the get verb, which is used
to clarify that this request will only receive data.
That is, it won't be making any changes.
The server receives the request
that was transferred from the client.
There's that second T.
The server then does the work that it needs to do.
Finally, it generates the resulting webpage
and sends back a response to the client,
my browser, in this case.
The most important part of the response is the body.
For a webpage, that contains
the HTML, hypertext markup language,
which gives us the remaining parts, the HT of our acronym.
Hypertext is text that can link to other texts.
In HTML, we perform that linking with anchor tags,
so with that response, the browser renders the page.
When one of those links are clicked,
they kick off a brand new HTTP get request
and the process repeats itself over and over.
In between those clicks though, the protocol's done,
we've made the request, we got the response.
HTTP, as it was originally designed,
is a stateless protocol.
Once that request receives a response, it's all done.
If you wanna keep some sort of state, your client will have
to manage that itself and send it up with each new request.
Check out the notes for more on this.
There are more http verbs, also known as request methods
besides get, most common scenario you see is when you
encounter a form that is used to take information from you.
Typically you'll see the verb post used to explain
that you are submitting or posting data to the server.
Okay, so our protocol that's used to transfer
this hypertext is admittedly a little
more complex than our rally chant was.
There's quite a bit of important information in there
that is included in the request and the response.
You've probably seen query string parameters in URLs
and you know the stuff after the question mark?
This is one way to pass information about the request.
Another common way this information is passed around
is through specific key and value pairs called headers.
Both requests and responses have headers
and they're used to help further communicate
what is wanted as well as what has been sent back.
Some of the popular request headers allow you to specify
information about the content that you expect back,
like the language or the type of content.
You can also state that you want the page
if it has changed since a certain time.
This technique allows for caching,
which is a powerful concept.
Why get the whole page if you already
have it in your local cache?
You can embed authentication information
in each request using a header.
In this way, you're essentially keeping
your client logged in across requests.
This response also contains headers.
A super important one is the status code.
This lets you know what happened on the server side
and can convey things like missing documents.
I'm sure you've seen the old 404 or maybe the page
has moved, but hopefully everything is a 200 OK.
It can also explain the content type,
how large the data returned is, and many more things.
Okay, got it?
Are you feeling good about that little whirlwind tour?
Remember, feel free to make me repeat myself
if that was too much, I totally get it.
That was a lot of information.
It's important to have a basic understanding
of these things because REST sits on top of them.
It uses the concepts that were introduced with the web
to expose resources that can be accessed and manipulated.
Let's take a look at how the RESTful approach
takes advantage of the web and its tooling
for your benefit, profit.
REST stands for representational state transfer.
APIs that meet the REST architectural
style constraints are considered to be RESTful.
Now, a typical way to teach about RESTful APIs is
to cover the constraints that define it all upfront.
Now, I don't know about you, but personally I find
approaching learning that way pretty overwhelming,
especially from the beginning.
I mean, we haven't even looked at an API yet.
On top of that, most of the REST APIs
that you'll use will meet all of these constraints.
So here's the thing.
I promise that we'll go over all the constraints,
but let's do it on our own time,
like don't learn 'em all right this instant.
We could even make it a fun journey,
like a RESTful constraint scavenger hunt,
complete with prizes, avoid where prohibited.
Here are the guiding architectural constraints required
for an API to be considered RESTful.
We'll check them off when we discuss them
and then we'll loop back once more at the end to review.
Gotta collect them all, sound fun? Good.
Let's get started talking about how REST API sits on top
of web technology, like a little hat, much like the web,
the client makes a request to a server.
The client in this case is typically going
to be your program and you'll probably be using
some sort of library to create the request to the server.
The protocol used, you guessed it,
it's HTTP and it's stateless.
The server won't remember anything
about the particular client.
If you wanna maintain state like your login details,
you must send it with each and every request.
You'll do this using headers and just like that,
we talked about two of our REST constraints,
client server architecture and stateless.
You got two knocked off already. You're on a roll.
Now, we should probably talk about
what that request was requesting, right?
Well, it most certainly was
requesting information about a resource.
Now a resource is a little abstract and that's intentional.
It's the R in the URL or URI.
We use the term resource to reference an object.
Also happens to be pretty abstract, right?
And this is because we can use resources
to describe just about anything that we build.
For instance, let's imagine an e-book store website.
On the homepage, there's a list or collection of new books.
Each of these books is a resource.
We click in to take a closer look at that book resource
and we see a link to the author, which is also a resource.
We click into the author resource and we see
a collection of all the other books they've written.
Maybe we even see a group of fans, again,
another collection, and each of those fans are a resource.
Clicking into a fan resource, we see a collection
of their favorite authors and books.
Everything is a resource. Let's change gears.
How about an image sharing application on your phone?
So we have user resources and those users post photos,
which are resources, and those photos can be loved.
So there's a lover resource,
maybe there's an album that could be created.
There's an album resource that contains photos
and albums have a collection of contributor resources.
Turns out almost everything we ever want to build
can be expressed in terms of resources and collections.
Most of what we want our applications to be able to do
to the resources can be expressed through the
unfortunate sounding acronym CRUD, and that stands
for creating, reading, updating and deleting.
Now, as a user of the e-book site,
all we did was a bunch of reading, no pun intended.
I actually did intend that one.
We were reading, right?
Just getting information to display it like
we retrieved the author page, we made a read request.
The web browser used a get request
to the specific author page.
The RESTful API follows the same logic,
your program sends a get request to a URI.
Then the server responds with data
and of course all the HTTP headers
you've come to know and love.
The body is typically represented
these days as JSON or JavaScript Object Notation.
JSON provides a great way to structure and nest your data.
Every programming language that I can think of provides
some means of turning a JSON string into a native object.
The content type is typically something that you
can specify in your request if JSON isn't your thing.
If you recall, there are other HTTP verbs available.
These verbs are used in REST APIs
to clearly state the intention of your request.
Wanna add an author?
Host to the /authors endpoint and send the data
that you want to update in the body of your request
and these fields will be defined for you somewhere.
Wanna remove an author?
Send a delete to that specific author resource.
Updates typically support the patch verb,
and you'll see we're able to represent our CRUD needs,
the create, request, update and delete using HTTP methods.
Pretty cool, right?
What this means is that you can interact with
any application out there that's exposed their REST API.
You could grab data, remotely automate tasks,
and even smoosh together different services.
This is called a mashup, and here's the thing,
there are a ton of APIs available, just about anything
that you can imagine, and if they're truly RESTful,
they'll all feel pretty much the same.
You can build anything, there are services that allow you
to do incredible things with just a few simple API calls.
So now the question becomes what are you gonna build?
So why don't you take a quick break and when you come back,
let's explore a RESTful API together.
We'll take a peek at how to use the music streaming service,
Spotify and their excellent RESTful API.
(upbeat music)
You have some newfound understanding
of how web-based APIs work.
I walked you quickly through some
imaginary REST APIs and now I'm thinking
it's time that we get a little more specific.
Why don't we go exploring together?
Some of these REST APIs allow you to give it a try
from the comfort of your very own web browser.
One of my personal favorites that allows
for exploration is the streaming music service, Spotify.
So here I am at spotify.com.
Now, a quick word of warning, it is going to look different.
I guarantee you this site is gonna look different
because they're always making things better for us,
for us Spotify users, us lovers of music,
and as well us developers.
So I want you to try this.
When you go to a site, any site that you want to,
you'll see here that this is really nice,
Spotify's letting us kind of test out what
it feels like in the web, on the webpage.
But if I scroll down to the bottom,
that's typically where they hide nerdy stuff.
Now, I knew this was here, but if I didn't,
I would look around for something like this,
this Developers, and I'm going to click into here.
Oh, I cannot tell you how good it feels as
a developer to end up in a place where it feels
like you're being taken care of, right?
This is so nice.
The Spotify for Developers, it looks like it
was thoughtful and I can tell you that it is very nice.
It's a very nice tool.
You can scroll down, you can see all sorts of stuff.
They give you some examples
of what you might want to do, right?
What could you build with this?
So you could find out like different things,
like you could use the API to see the beats
and the sections and the bars.
I think that there's a way to figure out
if a song is danceable or not.
So maybe I could import dancing. We'll see.
And if I scroll back up to the top here,
you'll see that there's these different headings.
We've got this Discover, I think this is great
if you're going to explore things, we have this Docs,
you'll see that there's all types of different, actually,
APIs and SDKs, software development kits, which are APIs,
which allow you to do different things.
You'll see that there's ones for iOS,
there's ones for Android.
What we're gonna be focusing on is the one for the web APIs.
So if I click into that,
we'll get a nice overview of what this is.
I'm gonna blow this up just a little bit.
So you'll see based on some simple REST,
representational state transfer principles,
that's what we're doing, that's what we're going through.
We're going through our scavenger hunt.
We're going through that.
There's a Spotify web API that returns JSON,
JavaScript Object Notation about
music, artist, albums, and tracks, right?
So that's awesome. That's great.
So we can see all sorts of different stuff.
It opens up at this api.spotify.com is
the specific URL and then it has a set
of endpoints with each unique path, right?
Each of those URLs that we were looking at before,
there is a way to get specific data.
So you could do this thing where you authorize.
So, I could be a user,
you could see what I recently listened to.
I could have a playlist, I could add to it.
But in order to do that, you need
to have authorization to be able to do that.
You don't wanna be able to just add
to anybody's playlist, right?
So there is some authorization that's available
and we are not gonna do anything that requires that here.
So if you keep on looking down here,
you'll see that there's, it's based on
those REST principles and you'll see that
it does get to retrieve resources, right?
And there's the post to create resources, and again,
resources here are songs and artists and tracks.
Maybe the resource you're creating here is one
of those playlists that we talked about.
You can change using a put
and a delete will delete the resource.
So awesome, right? So it's using REST like we saw.
You'll see that Spotify uses this URI style.
So it's a Spotify track and then it
has this unique ID, this Spotify ID.
This will make more sense as we dig into it
a little bit more and then you'll see that there's always
this Spotify URL that allows you to see what it was.
And it will show you this as well.
As we said, the responses come back
and they are a JSON object and we're gonna be looking
here at a tool in the future called Curl,
which you can use from your command line.
That's what this dollar sign here is talking about.
I'll show you all this, but basically it's saying
that there's a lot of artists that come back.
You don't want all the artists in it, it will
come back paginated and of course, you can use caching.
We'll take a look at that here,
you can see if the thing has changed or not.
So we'll take a look at that and basically the response
that comes back, if everything's good,
it's gonna say a 200 or these, these 200 level accounts.
And then there's bad requests.
So if you ask wrong or maybe it wasn't found,
that's the 404 or unauthorized, if we try to do one
of those playlists, we get an unauthorized
and there's also if you're a developer,
you never wanna see this one, it's the 500.
It means that you made an error on the server side.
We won't be doing any of those 'cause
we're just gonna be hitting somebody else's code.
Cool, all right, so Spotify offers the ability to,
you can look at this and you can build your own
curl request, but one of the really nice things
that I love about their API, and you'll find
this happens often, if we scroll back up here
to the top, sorry if I'm making you sick there,
we're gonna jump in here to what's known as the console.
Over on the left are the things that we can do
with their API and one of the things I wanna
do first is I want to search and you can see
it's making a get to the search endpoint.
So I'm gonna go into there and what's really nice
is it's set it up so that we can kind
of build the query that we're looking for.
So now we need to decide what we're gonna do.
I think probably the first person that I would
ever search for, the first artist I would search for.
And you can see that here it's showing
the different values of what that is.
I would probably search for Beyonce.
I mean, right, wouldn't you?
And I could choose, is that a track or an artist?
So the type there is important, and if I type artist,
you'll see that what's happening is you'll see
the curl here is changing, it's changing the,
it says the cue is the Beyonce.
So that's one of the parameters that
we're passing into the API and artist is here.
And you can see there's different things
that we could do here.
What's really important is that it
needs to have this OAuth token.
Now what's nice is we can build one of those
even if we don't have a Spotify account.
So I'm gonna do that.
I'm gonna click this Get Token,
I am not currently logged in and I actually don't
have a Spotify account yet in this user.
I do have a Spotify account. I love Spotify.
And this is asking, basically what you would do
is you would create a specific OAuth token.
If you were ever to use this API outside
of where we're doing this right now,
you would create one of these tokens so that you could
log in later and you say what it is that you want.
So here's the playlist, right?
We don't, we're not gonna do any of the playlist stuff,
we're just gonna do a search.
Maybe we'll look up some of Beyonce's tunes, but,
so I'm gonna go and I'm gonna click this Request Token.
So now it's asking me to either log in
with my Spotify account, which I can,
or I can create a new one, and let's walk through that
just in case you don't have a Spotify account already.
Let's create a new one.
And this will be at my developer account here.
So I'm gonna put in my email.
So my email, I don't know if you know about this,
but if you use Gmail you can do a little plus sign
and it will still send to the left side of your Gmail.
So I'm gonna do at Spotify sign up and that way
any emails I get from here, it will have that.
And it's kind of a way to let you have multiple accounts.
And I think, so again, my Gmail is craigsdennis@gmail.com,
but I'm gonna do SpotifySignup@gmail.com
and it will go to my Gmail.
It'll just kind of ignore this, Gmail does that for you.
It's pretty nice, right?
So I'm gonna put in the same password as my luggage,
which is 1, 2, 3, 4, 5, 6, just kidding.
And what should we call you?
Oh, you know what?
I love it when you call me Big Poppa,
that will appear on my profile, I am ready for it.
And let's see, it is my birthday on November 19th, 19.
Let's see, what do I, I think,
wouldn't it be nice if I was born in 1999?
What is your gender? Oh, this is nice.
How nice. This is like super inclusive.
I love that this is so inclusive.
It's like somebody at Spotify went and talked
to their people and listened to what people wanted
and they included people who identify as non-binary
or other, or prefer not to say.
So like wherever you sit in that I love and hats off to you
Spotify for being inclusive, 'cause inclusivity matters.
Share my registration data.
Sure, why not? I am not a robot.
I don't think I am. Oh, oh no.
The images with bicycles. That one's got a bike.
That one's got a bike. That one has a bike.
Oh, what is that garbage there?
Is that, this is a bike lane. This is a bike.
Okay, here we go, please. Okay, I am not a robot.
Thank you. All right, so I'm gonna click Sign Up.
Oh, I am male. I am male.
So here we go. That's how I identify.
All right, I'm gonna save that log in
and I am going to log in with it.
So here we go. All right.
So it's asking me if I have access to this developer console
and it'll see my logged in as Big Poppa, here we go.
And now you'll notice here that
there is this token here, right?
This is this OAuth token that got created
for me by Spotify and it's putting it into
this console for me so it can share it.
Now you never wanna let your token out.
If you get a hold of, somebody gets a hold
of your token, they can do stuff on your behalf.
And I realize that this is on a screen being recorded
and I'm going to get rid of this token here in a bit.
So you will not be able to have it.
So I've got this token, it's filled out here.
I'm gonna click Try It
and we're gonna do a search for Beyonce.
Let's see what happens.
So you will see that we got some information back.
So we got a list of artists,
'cause that's the type that I searched for.
And this is the search that we did.
We'll see some external URLs
and we think that this is probably her.
I'm gonna see, I'm gonna grab this really quick,
but you can kind of see how things are laid out here, right?
So this is my Spotify URL for that.
Let's see if this is her, this looks like her.
Yeah, yeah, definitely. So, great.
"Crazy in Love," "Cuff It." I haven't heard that one yet.
Awesome. So this is Beyonce, great.
So we got her, we found her, we used this search.
It has, shows how many followers she has.
Wow. 32 million.
Good job. And it tells the the genre.
So we can do all sorts of really cool stuff with this.
And it's here, you'll see that I'm getting a URL,
I'm getting a link that will open up
her specific artist page, right?
So, here is her ID.
So if I take her ID, I'm gonna grab this ID
and I'm gonna come over here to Artists and I can
come in here and I can show all of the artist's albums.
So you'll see it'll be artists/ID/albums.
Let's see all of the albums that she did.
But let's look at that in JSON format.
So the artist that I want, I'm just gonna pipe that ID in
there from before, right, now it still has my bearer token.
The console has done that for me and I can go ahead
and do Try It and we should see that now,
it's gonna return back albums, right?
So we've made a get request to the albums and I can see all
of the albums that she's had here and we can see
where they're available in different markets as well.
Looks like she's available in lots
of markets all over the world.
Who runs the world? All right, so here we go.
We are going to look, let's see, these are albums.
That's the artist there. That's the type of the artist.
Let's get one of the albums here,
let's make sure that we got it.
So you see that it's got different things like I could
get access to maybe the image of what this album is.
So let's do that, I'm gonna post that here,
open that up and let's see, there she is
on her horse, her magical horse there.
So that's pretty cool, right? This is awesome.
I can get access to all sorts of information
about an artist I can get by their ID.
I can go and find all sorts of different things
that I wanted to do, right?
So, if I knew an album too, once I have the ID,
I could get the ID, I could get the tracks,
all sorts of amazing things.
And then there's these playlists, right?
So you can add a playlist, if I wanted to,
I could post to create a playlist.
Now of course I need to authenticate to do that
and that's a little bit of what we're doing here,
but I could add, put together a playlist
for everybody to do using this API.
And again, when we are looking at the artists,
when when we're looking at this get artist,
it's showing this curl command
and we're gonna take a look at that here in a bit.
But I just want to take a little bit of time.
I want you to go and take a look, go back
to this Discover section and kind
of get inspired by what's available, right?
So you can look at, there's these different things
where you can, I was right, you can check
and see if there's danceability of a song.
You can go and find a track and is it danceable
and you know that Beyonce's is, the songs are gonna
come back very high in the danceability thing there.
But lots of powerful things and there's lots
of cool examples that you can do that people
have already gone and built.
And you can take a look at how they built it
and how they use the API recommendations.
This is awesome, really this is like super wide open
to you and you can do all of this
because of the APIs that they provide.
Pretty cool, right?
You can get access to all of Spotify's music library
and you can build tools that extend their platform.
I highly recommend that you go and take a look
at what other people have built using these APIs.
It's super impressive.
Did you notice how fast those results came back?
How could it do that search so quick?
Oh wait a second, does it even need to?
Those results don't change all the time.
It's not like Beyonce is dropping a new song every minute,
though I wish she would.
Those results are definitely cacheable, ding, ding ding.
Cacheability is also available on the client
and we'll get to that here in a bit.
But this is a great example of a layered system.
I'm almost positive that Spotify has
set up things so that it returns the same JSON
from its own server side caching mechanism.
It's not rendered that on every single request.
As a user of the API, I don't know how
it's doing that and frankly, I don't care.
I want it to be available
and fast when I hit that specific URL.
Beyonce definitely appreciates
how fast the results will return.
She might even take that API dev to Red Lobster.
Let's go ahead and take a quick break, during this break,
I'd love for you to move around and think about
what you might be able to build with that Spotify API.
For instance, while I was using this API,
I had this idea pop up.
When I'm at a music festival, I often have a hard time
deciding which band I should go see, there's so many.
Now I would love to have an app suggest
to me which of the bands that are
currently playing I would most likely enjoy.
You know, based on my listening habits,
I'm pretty sure that you could build that just
with the Spotify API and using a list
of events of the bands that are performing.
And if you do end up building that,
share it with me 'cause I need it.
What other interests do you have
that provide an API that you can use?
Check the notes for more ideas.
Now, after you come back from your break,
let's dive a little deeper and make use
of an API using our own tools, see you soon.
No really, take a quick break.
Seriously, it'll do you good. No binging.
(upbeat music)
While I realized that exploring
an API on a website is informative,
it might not feel like you're actually using that API.
I think the next logical step in exploring
is actually making that request yourself
from your computer, not from a website.
Now as I pointed out in the Spotify docs,
there were examples that were using a command called Curl.
Curl is an open source tool that is used to send data back
and forth and it runs locally on your computer
and it's awesome for poking around at APIs.
What do you say we explore it a bit?
Okay, so we are back here on our Spotify page
and we see that there is a command here that says Curl.
Curl is a command that we can run from our terminal.
Now if both of those terms are new to you,
command and terminal, this is a great time to head over
to the notes and learn about what the terminal is.
So my operating system is a Mac
and it has an application on it called Terminal.
So I'm gonna open that up, Terminal, and in fact,
I'm gonna make this a little bit bigger.
Let's go ahead, let's let it take up the whole screen,
we're gonna be in here for a little bit.
So I'm gonna type Clear and clear my window.
All right, so this terminal
allows me to perform actions on my computer.
Like I can make it do text to speech.
I can do Say and I can say, "Hi mom."
- Hi mom. - And it says, "Hi mom."
And the Windows operating system has a terminal as well.
And while it's a little bit different,
they're actually pretty quite similar.
But check the notes for more on this.
So what happens is I write the command
and then I pass in values for the parameter.
In this case, it's "Hi mom."
Now, note that I used quotes there because I wanna
pass one value, even though there is a space,
if there was a space, that's kinda how you separate things.
But with the quotes it's saying
this is one value that I'm passing in.
We developers spend a lot of time in our terminals,
so if this is new to you, I want you to remember
that it was new to everyone at one time.
So please take your time and don't let this
get you too frustrated, you've got this.
So I already have Curl installed.
I'm gonna just go ahead and make sure that I do
by typing the word "curl," and if I see this,
I could see that I could type curl
and then the command "--help."
Now if I didn't have this installed,
if this came back with like, I don't know
what you're talking about when you type curl,
the word curl, that's okay.
So we're gonna head over to the notes
and you'll get sorted there.
But the starting point, where I want you to go is curl.se.
So you'll come to this page,
you'll see this as a command line tool
in library for transferring data since 1998.
And that logo sure feels like that, doesn't it?
It does all sorts of things.
What we're really concerned about is this HTTP, right?
We're going to use this from
the command line to get access to a URL.
So if you don't have curl on your command line,
go ahead and pause me and check out the notes for
the best way to get this installed on your computer.
There's a lot of different ways to do it.
If you come here under Download,
you'll see that there's different operating systems.
So you've got your Linux and you've got your Mac OS.
And if you come down here, you've got your Windows.
So there's different ways of getting things installed.
So go ahead, take your time, get that installed.
And when you come back, I want you to try this.
I want you to do this thing with me.
So I'm gonna go back to my terminal
and I'm gonna make sure that it works.
The way that I always like to test is I like
to go to icanhazdadjoke.com.
How do you get a baby alien to sleep? You rocket.
Oh, so good. Anyway, that's how you do that.
That'll let you go and it's gonna hit that website
and it's gonna give you back a joke when you're ready.
So now that we've got our curl up and running,
we've got our successful dad joke,
let's go ahead and run that command from Spotify.
So I'm gonna come back over to my Spotify thing
where I was running the search.
I'm gonna make sure that it's still working
actually before I get started, 'cause these tokens
can expire and depending how long your break was,
it might have expired, but mine is still working.
So I got this back here and I should
be able to get this from my command line.
And what I'm gonna do is I'm going to copy this,
I'm gonna highlight this.
This is a development practice known
by US developers known as copy pasta.
So I'm going to copy this and I am going
to go back to my terminal and I'm going
to paste and this long list of things here,
what this is doing is just saying, hey,
I wanna make a get request to the api.spotify.com/v1/search.
Remember, that's where we were at.
We were searching and we're passing the query string
of Beyonce and the type of artist
and we're telling it, "Hey, I would like
to get back JSON and I am sending you JSON,"
even though we're not actually sending JSON.
The default has that there and this is an important one.
This is sending you a header, right?
The "-H" is a header and authorization.
And this is that long token that we had there.
Now if all is working, when I press Enter,
I should get here locally on my machine that same list
of data that we did and I did, check that out.
So what I'm gonna do is I'm gonna come back
up here and I am going to grab, wow,
there's a lot of information here, right?
When this comes back, I'm gonna grab her ID.
And so I remember that when we were looking at it,
it was down in here, this ID, is this the right one?
Is that the first one?
So we got items, no, this is her ID here, this is her ID.
It starts with a six, VW.
So I'm gonna copy that and I'm gonna use that.
I'm gonna go back and remember I'm gonna go
and I'm gonna look, I wanna see what albums she has.
So I'm gonna come into here and I'm gonna remember,
I'm gonna paste this URL here, and I'm gonna run this,
make sure that that's still working.
I'd like you to go ahead and pause me.
I think I'd like to see can you
get her albums locally on your machine?
Can you go ahead and make sure that
you can do the same Get Artist,
see all of Beyonce's albums on your local machine?
So here's what we'll do, you give it a try
and if you, once you get it printing locally
on your machine, you can let me know and then
unpause me and I'll show you how I did it, okay?
Are you ready? You got this.
Pause me. (upbeat music)
Cool. So how'd you do?
So here's probably one way that you could
have done it is just copied that, right?
Copy this whole thing and pasted it.
But what I wanna show you is that we have access to,
we know what these URLs are, right?
So we know that there's this v1 artist and in fact,
we can see that there is a get of /v1/artist.
And then you can actually pass their ID
in the URL there, right?
That's what this ID is here. So, let's just go back.
I'm gonna show you a little trick too.
So, I've got her ID copied still, so I could have just,
just ran that query and copied the curl and pasted it here.
And if you did that, you did it right.
I just wanna show you a little bit more
of what else, how else we could explore.
So I'm gonna press the up arrow
and you'll see my cursor's moving here.
I'm gonna press the Option key on Mac.
I think you're gonna press the Alt key on Windows.
You can jump around like this and I'm going to get rid
of what's here, and I'm gonna say v1 artists, right?
So that's the collection of artists.
And I have that specific ID, right?
So there's the ID and I'm gonna press Enter here
and you'll see that I got back Beyonce, which is awesome.
That's what I did and then remember, the artists have,
I'm gonna go to the front of the line,
it's Control+A on Mac, and I'm gonna switch this
so artists have, this is specific artists, so Beyonce,
and we wanna see Beyonce's slash albums,
which is what the console would've generated for you.
But I just wanted to show you that you can kind
of navigate and move through and you'll see that there's
a bunch of different information that comes through here.
It's kind of hard to see, right?
So, kind of hard to parse.
And this is something optionally that you can do.
So I press the up arrow and I'm going to press,
pipe that into a tool called JQ and you'll see
that it comes back very nice and pretty, right?
So this is something that is very nice
to have and I piped that.
That's this, the symbol above the Enter key is
this pipe symbol and it allows you to take whatever happened
in this first part of the command and take its results
and put it in this part of the command, which is super cool.
And again, this is in the notes.
You don't need to, you don't need to worry about this,
but I know that I can query things.
So I can say that of the items,
I'm only interested in the name,
the .name from each one of the items.
And this is a little query language
that's attached to this JQ program.
You don't need to do this, this is just something,
if you're doing it from the command line,
it's something that we do that we could kind of hack around
with a little bit and you'll see I can get all
the lists of her albums and that's pretty cool, right?
That's powerful, and if you are gonna just start
poking around and moving around with the URLs,
one thing I wanted to make sure that you're cautious
of is if we come back and we take a look at
the console page here, they do a nice trick for you.
If we're back here in search, like one of the things
that I wanna make sure I know that Beyonce has this song
that's like something like, who runs the world, right?
Who runs the world? Girls.
But I don't remember where, what album it's on.
So I'm gonna search for a type track.
Now, note that my query has spaces here.
Now what's happened here is it's done this URL encoding.
So like if you were trying to mess around and work
with the search, you'd wanna make sure that whatever
you did, you passed that with those plus signs there.
So I'm gonna run that curl command
and we'll see who runs the world, girls.
And I am going to, I see there's this open link here.
So I'm gonna go ahead, I'm gonna open that.
Let's take a look and make sure this is the right track.
I'm pretty sure it is, it is not the right track.
What is this? Femme phan?
Maybe it's some other, some others.
So let's take a look. Well, that's interesting.
So, oh, I just went to the bottom
instead of going to the top.
So I should have went to the very top of these results
or I should have pulled, probably pulled the name out.
Rock-a-bye, baby. There's a Rock-a-bye baby version of this.
Whoop, okay, so when the color stops,
hope you're not getting sick of all this scrolling.
So Beyonce is the artist that we want.
So this is the one, we want to hear this song.
Let's see if I got the right one this time.
This is just something, you know,
there's a lot of data coming back here.
It's kind of hard to do, to use on the command line.
Sometimes I'm a little cutsy like you just saw.
Here we go. Let's see what happens.
Let's make sure this is it.
There it is. There's Beyonce.
Not the song I was looking for.
Oh yeah, it is, "Who Runs the World? Girls."
And now I can listen to that.
So it's like, who runs the world? Girls.
It's kinda like who runs the terminal? Curl.
Boy, that was a big setup just to make that joke.
I hope that you enjoyed it. You can has dad joke.
Curl is an awesome way to explore APIs and you'll find
that often, just like we saw with Spotify,
APIs will use Curl in their docs to show you how they work.
Spotify is just one API and there are
so many companies and services out there.
In fact, some APIs are the product.
Like that's what the company's selling to us developers.
So now that we've been building up those muscles,
why don't we take a swing at another API?
Let's use a different API, this time let's use Twilio.
Twilio is a company that loves APIs.
In fact, that's what the company started as
more than a decade ago, just a set of APIs.
It started as abstracting away communication channels.
And by that I mean any way you'd ever
possibly want to communicate with your users.
Wanna send a text message? There's an API call for that.
Wanna make a phone call? There's an API.
Send an email, a WhatsApp message,
literally whichever way you want to communicate,
there's an API for all of them.
Developers embed these APIs into their apps
and I'm pretty sure that you've probably
used it without even knowing you were.
Now, nowadays there's a whole suite of products
that Twilio offers that they saw their customers needing.
But under the covers it's still
these APIs that are powering everything.
I need to be a little upfront here with you.
I love Twilio, I mean, I love it so much,
I even applied for a job with them and I got it.
But even if they didn't hire me,
I know that I'd still show off how powerful their APIs are.
I mean, with a single command, you can send a text message.
I remember when I first did that, I was like, whoa.
Like that level of abstraction is amazing.
I mean if you've never thought about it before,
what even is a text message?
I mean, I know I have an app on my phone and I can
use it to communicate with literally anyone else.
But how does that work?
Like there's all those different
cell carriers and standards and phone numbers.
I mean, I have no idea.
And with Twilio, I don't need to know.
That's the beauty of this API.
It just works here, let me show you.
All right, so here we are at twilio.com and again,
word of warning, this is going to look
completely different from my right now to your right now.
So please make sure that you check the notes for anything
that might have changed and is specifically about
this flow that we're about ready to do 'cause
we're going to sign up for a trial account.
So I'm gonna click the Sign Up button here
and this is going to look completely different.
I know because our team is constantly making this better.
But the one thing that I wanna make sure that you know
right now is that you see that there is no
credit card required and it is a free Twilio account.
So I'm gonna put my information in here
and I'm gonna use the same trick that I used before
where I do plus and I'm gonna say, "APIcourseV2@gmail.com."
So, the plus sign there again, the little Gmail trick,
we're gonna go ahead, I'm gonna put in my password again.
1, 2, 3, 4, 5, 6, my luggage.
How many times can I do that joke?
And then I gonna do this accept,
must be 16 or more characters.
Okay, there's some password requirements over there.
I think that's 16. All right, here we go.
Starting my free trial.
I'm ready. Let's do this.
I'm definitely gonna save that long password
'cause I need that for later.
All right, so I have now should have been sent an email.
So let me make sure that I got that email and I did,
I am going to click the link to verify it and here we go.
I'm gonna verify my phone number to verify
I'm a human 'cause all humans have phones.
So we're gonna do (432)-527-4274.
Please don't call too late.
I'm gonna try that. I'm gonna click Verify.
All right, so I got my verification code.
Hey that came very quickly.
Well that's wonderful, my verification code is 017869.
Hopefully your code's different than that.
Wouldn't they be funny if they were all the same?
All right, so here we go.
So which product are we here to use? We're gonna use SMS.
We're gonna use the SMS messaging product
and what do we plan to build?
Let's see the, of course,
these questions are gonna be different.
What are we gonna do?
We're gonna build, what? Build something else.
We're gonna build something else,
how do we wanna build with Twilio, with code, right?
So we are gonna end up writing code if we want to
with minimal code on top of code samples or no code at all.
So, definitely don't need code to build over here
but I'm gonna say with code 'cause we're developers here.
So what is your preferred coding language?
We're gonna do JavaScript even if it's not,
we're gonna be doing JavaScript for the rest of this course
and I do want to host my code on Twilio, which is nice.
There's a nice serverless option.
These options, like I said are totally going to change.
Just try to answer them as truthfully as you can.
So, and my building country is United States, which is true.
And I'm gonna get started. I'm gonna go ahead and walk this.
I guess let's go ahead and so we'll I can explore products.
There's way more products that
are available than you can see here.
This is what got built based on the menu
of what they thought, but if I ever want to,
I can come to this Explore Products and get more,
we probably won't need to do that
'cause we filled out what we're gonna do.
And this is where you can get access to your logs.
I'll show that off here in a bit.
If there's any problems you'll be able to find them there.
Manage your account keys
and build trust with your customers.
Yeah, we can build all sorts of different things in here.
We probably won't need to do too much here
'cause most of the keys that we have are available
to us right from this page that we're on actually.
So we'll keep going and I can see the billing and again,
you're in a trial account and I think
you get something like $15 to try with.
So we'll be sending a text messages using that trial.
Dollar dollar bills, y'all. So, here we go.
And then you can switch between products.
So you can have multiple accounts, right?
So if you wanted to come and like explore a little bit,
do your own personal thing,
maybe do something for work later, up to you.
There's a 1550 is what we got. All right.
So, and then I'll click Next and then I can,
anytime I want to, I can look at this tour again to help
navigate around, which is really nice, beautiful console.
So we could definitely go ahead and the first thing
that we wanna do is we want to get a Twilio phone number.
So I'm gonna do that.
I'm gonna click this, Get Twilio Phone Number.
And I am so glad that this happened to me.
It says that this account has been restricted
from provisioning new phone numbers,
which means that thinks that I might be spam.
And you know why it is, it's because I put
that Gmail account with a little plus thing there.
So if this happens to you, I don't want you
to feel offended or anything like that.
I want you to check the notes
for the latest ways to get around this.
And also I'm going to go right now and I'm going
to email compliancereview@twilio.com
and I should get that up and running.
If this happens to you, go ahead, press Pause,
check the notes here for what to do.
And I'm going to actually do that.
I'm gonna press Pause here.
I'm gonna get my account able to get a phone number
and I will see you back here in one second.
All right, so I got all sorted out.
So I sent my email to the compliance folks
and I was able to say that I was doing
a personal account and I'm learning how to use
these APIs and they were able to let me get in here.
So what I'm gonna do now is when I click this
Get a Twilio Phone Number, we're not going to see
that message 'cause I'm coming in and I've got
a new phone number and it automatically
gave me this one, this one here, this 561.
It went away really quick, but it's down here.
This is my Twilio phone number, this (561) 816-5433.
Now this number is mine, which is awesome.
I can use this message, I can send messages,
I can receive messages,
I can do all sorts of things with this.
I can make calls, I can receive calls.
This Twilio phone number is mine to play with.
I think we should do just that.
We should send a text message.
Now before we get started,
I wanna make sure that you know this stuff,
you are on a trial account.
So there are some limitations.
You can only send messages
and make calls to verified phone numbers.
So when we set things up, we've verified,
like I'm able to send a text to myself
because I verified my phone number.
I mean, and it will say,
"Sent from my Twilio account," beforehand.
And there's more things about your trial account too
if you wanted it to get into that.
So, check the notes because this very well could
have changed, but check the notes for more on this.
So let's do it. Let's send a short message service, an SMS.
So I'm gonna click here on this Docs and Support
and I'm gonna click this, it's gonna open up,
I'm gonna go these developer docs, these developer docs
are some of the best that are available out there.
I want you to see these.
So we're gonna come in here under SMS
'cause that's what we're doing, right?
Short message service, and under here, under these,
these sides on the docs here, you can collapse these.
So I'm gonna come in here under API reference.
I'm gonna go to this message resource.
Now this is an API, right?
So we're in the API reference.
So the API that we want to do is we wanna be able
to send from, and we wanna say from the phone number
that we just got and we wanna send
a message to my phone number, right?
So we're gonna do that and you'll see that
over here on the right, there are things
that you can do that show you what you can do.
So with this message we can do all these different things.
We'll start with creative message,
we'll create a message first.
So we're gonna go into this, create a message
and there's these different languages here.
So we have no JS and you can see the style
that it looks like, we'll take a look at
something like this here real shortly.
This is a helper library that wraps around the API.
So you don't need to do the sorts of things
that we're about ready to have to do.
You can do things a little bit more cleanly.
But what we're gonna look at is Curl, right?
So here's our friend, Curl.
We're gonna jump over here to Curl and the docs have this
really nice thing where I'm able to actually copy this.
So I'm gonna click to copy this
and I'm gonna pop over to my terminal.
Now again, I'm running on a Mac.
So there's a couple of things here that this is assuming
that it's going to be okay because I am running on a Mac.
It's going to assume that these slashes,
see these slashes at the end here, these on a Mac terminal,
these show that we are in the new,
we're making a new line available here.
And also you'll see that there's
these account SIDs and these Auth tokens.
This dollar sign Twilio Auth token, dollar sign,
Twilio Account SID, in my terminal,
on my Mac, that is what's known as an
environment variable and they've been set.
Now I don't have those set currently
because I just built this account.
So what I'm gonna do is kind of bad practice actually,
I'm gonna actually put my
real information here for us to use.
So, back here on our console,
on our Twilio console you'll see that I have this thing here
called this account SID or String Identifier.
So this is my ID,
my unique ID for the account that I just created.
So I'm gonna go ahead and I'm gonna click this Copy button.
So I copied that, I'm gonna come back here
and I'm gonna move my mouse and I'm gonna
remove right here where it says account SID.
So that's string identifier. I'm going to paste this.
So that is the way that Twilio is identifying who I am.
I'm gonna go backwards through the rest of these.
And so if you're watching here on Windows,
let's just go ahead, let's get rid of these,
these new lines here.
So I'm gonna just put them all on one line,
we can handle that, we'll do that.
So I'm gonna get rid of that line and that way
it will work completely on both Windows
and Mac for what we're trying to do.
So I still have my account SID here,
so I'm gonna keep on backing up here
and I'm going to get rid of that line, there we go.
And you'll see one more time here,
there's the Twilio account SID,
that string identifier for my account was still present.
So I'm gonna paste that there.
So now the API call that I'm doing is to
that specific account, it's due to this messages
and you'll note that it's doing, now it's doing a post.
So because we're creating something new, right?
So we're using post to create a message
and you'll see here that it says from equals
and the number actually there for the from, we want that
to be the Twilio number that we just got, right?
So that new one that we just got,
we're gonna say that is sent from, let me go back,
I'm gonna copy my Twilio phone number, right?
So I've got my 561 number there
and I'm gonna paste that here.
So you know from and this plus 561, that's a US number.
So it's got the, it's called a 10 digit long code, 10 DLC.
So it's (561)-816-5433 is the number that I just bought.
And we're gonna say by default it said, "Hi there,"
the body of the message, right?
So we're, instead of that, whatever we're gonna do
and note that this is all in quotes
and we're gonna say, "Ahoy world."
I don't know if you know this or not, but the first word,
the way that we used to answer the phone was we'd say
the phone would ring and you'd be like, "Ahoy?"
And the other side would go, "Ahoy-hoy."
So we like to do that here a lot,
at Twilio we say "Ahoy world."
It's kinda like the hello world of this.
So the body of the message, the text that we're going
to get is gonna say "Ahoy world" and then we're gonna
send it, I'm gonna send it to my phone number.
So this is one of those numbers that you verified
right when you created your account,
you verified that you had a number.
So my phone number is (432)-527-4274.
Don't text too late at night please, and then
the final thing that we have here is this Auth token.
Now this is a very private key, right?
We don't ever want anybody to have access.
This is like that Spotify token that we had, right?
We don't ever want anybody to have it.
I am going to, I trust you and I'm gonna go ahead
and I'm gonna paste that here so that we have it.
So that is something that you never wanna
let out 'cause now you could seeing this
on my screen, you could use this, right?
And you probably don't want anybody doing that.
So you never wanna actually give that number out.
You wanna hide that and we'll talk
about how to do that more here in the future.
But right now I just wanted to show you,
we're gonna use Curl, we are going to create,
we're gonna send a post to this URL here
and you'll notice that ends in JSON.
So that's what we're gonna get back.
That's the way that it works.
This is to my account, my account SID,
you see it starts with this AC
and we're gonna post using this data URL encode.
So the way that the Twilio API works is it takes
some parameters, it takes from body and to
and the way that you pass those in curl is
with this --data URL encode and you pass
what it is that you want to have go across here.
So we're gonna, if all of this works,
I should get a message back.
I should get a text message on
my actual phone that says "Ahoy world."
Let's do it,
and it looks like I'm missing a quote someplace.
So where is the quote that I'm missing?
I don't see a missing quote.
I can go press the up arrow, maybe it will show up.
Yeah. Oh, right.
You know how many times I do that?
The ahoy world, the exclamation point
is something that you need to typically escape.
So I'm just gonna say "Ahoy world"
with a single quote there.
So, if you're having that problem,
sorry, that's what that was.
So I pressed the up arrow to get my information back there
and now if all is going well and I press Send,
we should get a text, here we go.
And so I got this message back, I got this
and sure enough, here it is, I got it.
And you can see here it says at the front of the body
that it says it says, "Sent from your Twilio trial account,"
which is fine, we're in a trial account,
that's totally fine, but I did and boom, I got it on
my actual phone, I got that message, which is awesome.
We now have the ability to send a
text message from the command line, right?
That's pretty cool.
Let's take this a step farther actually.
So we know that that API, when I did a post,
I was able to get information back.
I'm gonna go ahead now and I'm gonna do just bring that back
and I'm gonna get rid of all of these parameters here.
I'm gonna get rid of the from, the body, and the to
and I'm gonna change, I'm gonna make that,
I'm gonna do a get and let me show you what that looks like.
So I'm gonna get rid of all of those parameters,
I'm gonna keep that same URL and it still has my
account SID there, right in there and instead of a post,
what we're gonna do is we're gonna do a get,
what we should do is we should get back
all of the messages that are there.
So I'm gonna hit this kinda like a message log, right?
So these are all the messages that have been sent,
which is awesome, here it is,
sent from your Twilio trial account.
So, and actually I can make this look
a little bit prettier like we saw with JQ again,
totally optional in the notes if you want it.
Yeah, there we go.
So you can see that I've got some information
that's sent back here and you can see that the message
that was sent in was sent outbound to my phone number
to me from that number, which is awesome, right?
And like I said, curl's a little bit challenging
because it's gonna be a little bit different
if you're in Microsoft Windows or if you're in Mac.
I tried to make that as comfortable as possible
but I you saw that I did run into a couple of things
like I couldn't use the exclamation point
and things like that and it's a little clumsy.
So stay tuned 'cause there are more tools coming your way.
Awesome job getting that running from your local machine.
It feels pretty good doesn't it?
What a cool skill you have now, you can send a text message
from your machine with just a single command.
Now believe me, this is just the tip
of the iceberg of what you can do.
There are so many more Twilio APIs available
and now I know that you can explore them with Curl.
For now though, let's take things another step deeper.
Now I don't know about you but I'm a little clumsy
when it comes to long commands on the terminal
like we just did with Curl.
When exploring APIs, I really like to lean on visual tools.
If you don't mind, I'd love to show you some, that is,
of course, right after you take a quick break.
(upbeat music)
There are a few tools that I rely upon when it's time for me
to explore new APIs and I thought I'd share 'em with you.
I used these to dive in and get
my hands dirty and organize my learnings.
One of the most famous tools and one that you've
probably already heard of is called "Postman."
Do you get it? Postman.
In addition to being a pretty good pun,
it's also a powerhouse of features.
Postman started as a Chrome extension that let you
make API calls and what it's turned into is
a powerful enterprise ready tool with all sorts of bells
and whistles and even shareable collections
for the most popular APIs that are out there.
It's jam-packed full of features
and in my humble opinion, you probably don't need
all that just yet in your learning journey.
There are entire courses on how to properly wield its power
and I've linked to a few of those in the notes.
There are other tools popping up all the time
that allow you to explore APIs and store your notes.
I like to think of these apps as
the built in the vein of postman.
There's a whole genre of video games
that gets dubbed Metroidvania,
because they're similar to "Metroid" and "Castlevania."
Now these apps are Postmanvania, Postman-esque.
Let me show you an example.
I just bought this lamp here, it's rad,
it's from Govee and it's got this app
that I can use to control it on my phone.
I can change the colors, right,
and I can even make it dance to my beatbox.
(Craig beatboxing)
It also has an API.
These are the docs here and as I explore these docs,
I'm gonna save them in one of these postman esque tools
that I've been using lately called REST Fox.
Like here I can see all of the devices
that are associated with my Govee account.
And I set that up using this URL here and you'll see
that it is a get request and I pressed Send
and it sent the request and I got back
the Govee devices that are associated with my account.
This API requires a header and it's called Govee API key.
And you'll see these double mustaches here that I'm using,
this side little mustache thing.
This is getting things from my environment,
it's a special key that I don't want anybody to see.
I'll show you more of those here in a bit.
Now notice how this is a get,
so I can send the request and you'll see that here's
my response and I can also get the current state.
So I have a different call here.
I can get the current state end note that if I click Send
here we can see that this is the color of my light currently
and I can also turn it on or turn it off and I can
turn it on and I can also change the color, right?
So I'm gonna turn this to red.
You don't have to turn the red light on, Roxanne.
Pretty cool, right?
And I have this here for me whenever I want
to tweak things, when I want to come back later
and when I want to use it in my code.
Did you just have a light bulb moment?
I quite literally just did.
Let's see if we can't build one
of these for the Twilio APIs.
So if you'd like to get REST Fox installed,
please check the notes.
This is an open source tool
and it's getting better every day.
Now if you love it too,
I highly suggest that you star it.
If you click up here, you could go and you can
actually check out the GitHub repo and you could
see cute little Fox here and you'll see over here
how it's tagged that, oh it's tagged REST client.
I guess maybe that's a little bit better
than my Metroidvania analogy.
Maybe REST client's a good thing.
API client, that's also a good thing.
So there's lots of these tools that are available.
But let's start using this one.
So I've got REST Fox running here and you'll see
I've got the Govee folder, which I can collapse.
And typically what I do is group these
by the API that I'm exploring.
So let's keep poking around a little bit with Twilio.
So what I'll do is I'll make a new folder called Twilio.
So I'm gonna right click over here
and make a new folder called Twilio.
And now in here we'll create a new request.
So I right clicked New Request
and I'd love to explore more of the SMS API.
So let's see, oh you know what?
This new number that we just bought,
this Twilio number that we bought,
not only can it send messages, but it can also receive them.
So we could send it a message. So let's do that.
So let's go ahead and send a text to your number.
Now if you forgot your number, it's over in your console.
So if you come to your console and you scroll down
a little bit here, you'll see My Twilio Number.
So go ahead and send a text to your number.
Now my number, I'm gonna send a number.
It's (561)-816-5433. I'm gonna send it, "Yo."
And there we go, it went and I ended up getting
a default message back and we could change this,
we could make, we could program this to do whatever,
but I'll show that off later, but for now,
I just want you to know that we created a new message
by sending to this API and we can actually get it.
And you can also find your phone number
later here under this phone numbers.
If you go to Phone Numbers, Manage Active Numbers,
you can see your phone number and remember outside
of trial mode you could buy multiple numbers.
So that's an important thing to know here.
So, all right, so we are going
to look at the incoming messages.
So we should do that,
we should call the request what it's going to be.
We'll call this incoming message log.
That sounds good. And we'll click Create.
Okay, so let's head over to the docs
and see how we might do this with,
so back in the docs here on the message resource,
remember this is where we did the create a message.
I'm gonna see if there's something else down in here.
Ah, list all messages. That's a good one.
But we don't want all of the messages,
we actually want to filter it a little bit.
So look here, we can do this by matching filter criteria.
That's great.
So we're in here, I'm under the curl tab, right?
I could be in these different ones,
but this is the example that we want here.
I'm gonna grab this URL, you're gonna see all of this.
So that's how they're filtering it.
There's some stuff here at the end.
I'm gonna grab all of this and I'm gonna copy it.
I'm gonna come back over and I am going
to paste it here in this Enter Request URL.
I'm gonna paste the whole thing there.
And it was a get request and the URL had some stuff
in this query string that came afterwards.
And a query string is, you know,
defined by this question mark.
So we don't want this date sent, let's get rid of this.
And what's next? We don't want from, right, we don't.
We wanna look at all the numbers
that were was sent to, right?
So this is the to here, is what we want.
Now this 2B here just happens to be a percent
2B URL encode and happens to be a plus.
And I know that and I just happen to know
that without having to Google it but that's a little rough
to know like how do I make a plus sign in here?
Wouldn't it be nice if this tool just
did all of this URL encoding for us?
Like we didn't need to make this mess at the end?
Well check this out. Here's query right here.
This is what we want. So we wanna add this query.
We don't want to have this whole stuff at the end here.
We wanna put in to,
we're gonna add one of those things there.
We'll put in a to.
So that's gonna append that query string for me.
And I'm gonna put in, to my phone number,
which again my Twilio phone number,
which is over here in my logs right here.
Actually I can, I'm gonna grab this number.
This is my Twilio number.
I'm gonna put it in the format that
is wanted there in my REST Fox, here we go.
So I'm gonna fix that and we're gonna get rid of this.
Just wanna make sure that's 1561, there we go.
Got rid of all those. All right.
Now also remember that this was accounts
and then it had account SID, right?
So that's the ID for my account.
And then it's under messages.
There's a collection called Messages
underneath this account SID.
But what we want here is we want,
that was using an environment variable from Curl,
but this is also known as a path variable, right?
A path parameter, so here,
down here we have this path parameters.
So the way that these work actually
to replace them is if we go like this,
if I get rid of this and I put a colon,
what's gonna happen is we can, anything that's down here
in this path parameter will replace what's here.
So let's do that. Let's add this item.
And again, that is Twilio account SID.
So that's the name of the variable that's up here, right?
It's colon, Twilio account SID, that was just what's there.
Let's keep it 'cause that's what it is.
And then we're gonna put our account SID there.
So that also is on my console.
So I'm gonna flip back to my console.
And on the main page here, if you come in,
you can come down and you can grab your account SID.
So now I have my account SID,
and I'm gonna paste that there.
Awesome, and I think we're ready.
I'm gonna press Send an, oh, we got an error.
We got a 401 unauthorized, no credentials provided.
Oh that's right. Of course, we want to log in.
We don't want anyone just being able to see these messages.
So we wanna make sure that we
have some authentication happening.
So I'm gonna head over to the Auth tab.
You'll see that if I slide this back and forth,
I don't know how much screen real estate you have,
but you can slide these kind of around as need be.
So I can, I'm gonna pull this and you'll see
that when I do my little menu here collapses.
So I'm gonna choose Auth from here,
but that is also available in the header here, right?
So like it's just a different view of it.
All right, so in the Auth I'm gonna put in
a basic Auth and I'm gonna put in a username,
which is my username and then I'm also going to put in
my password, which is my Auth token over here.
And again, this is very private
and you would never ever want to do this.
You now know my Auth token. I don't want you to have that
because you could do exactly anything
you wanted to to my account now that we're there,
and one of the things that you could do is see
the messages that were sent in and look,
there's my "Yo," my "Yo" message has made it in, awesome.
So also I wanted to show you the request that happened.
Let's take a look at that.
You can see that what actually happened was it was
a get that was sent with the account SID, right?
And so that was the account SID that we used from,
if we look back at the query parameters here,
the account SID showed up there and then it automatically
did a two URL encoded and if you look it put
the question mark in for us and it did that percent 2B
for us automatically URL encoded, very nice, right?
And we can see the headers that were sent
and we can also take a look.
Let's look again at this preview.
So we see that we had,
the response came back and this is JSON, right?
So we have a key called messages.
And this messages has this, it starts out,
there can be multiple messages here, right?
So this is the start of what's known as
a list or an array and then there's an object.
And the start of the object is always done with one
of these mustaches, and we call those mustaches
'cause if you kind of turn your head sideways,
it looks like a mustache.
So all between the opening mustache and the closing mustache
is the information about the messages, right?
So here's the, and there's the closing bit of my list.
There's only one. This is the closing mustache there.
And so part of the message that we can see,
we can see who it was to,
that's my Twilio phone number here.
And we could see the from, there's my phone number there,
please don't call too late, but that's my phone number.
And I did say, I said "Yo" to it, which is awesome.
So now we have this. Now this step is completely optional.
This is something that I like to do is I like to take notes.
All right, so there's this incoming message log.
I like to keep notes and I put those
in the description over here.
And so let's see this returns all incoming.
Well, let's be a little bit more,
all messages sent to this phone number, right?
And one of the things that I like
to do is I like to put on the docs here.
So if I ever come back and I want
to find how was it that I learned how to do that,
I can go and look and put a link there.
So I'm gonna go flip back over to the docs real quick.
And this is read a message with matching a filter.
I'm gonna copy this link, I'm gonna grab this
or click this here so that then it changed the URL up here.
I'm gonna copy this and this is
what I'm gonna store in my notes.
Again, this is totally optional and this is just notes
for you for later 'cause you could end up having
a whole bunch of these and you might not
remember how you found how to do that.
If there's something specific or strange
about the API that she wanted to know,
I keep the notes in there and it's a just
a handy learning tool and again, totally optional.
So we've got our first entry all set
and I think we should add another one.
Why don't we create the send message one, right?
So the one that we did before
we sent messages, let's do that.
So I'm just gonna duplicate this 'cause it's got
a bunch of the information that I already need in it.
So I'm gonna duplicate it and what I'm going to do here is
I'm gonna duplicate and I'm gonna say "Send message," right?
So when we sent a message before,
remember it wasn't a get, it's a post.
So I'm gonna change,
I'm gonna change that from a get to a post.
So now we've got this send message
that's a post and the query string.
I don't want to send a query string anymore, right?
So remember wait, before when we did this,
we sent it to using the data URL encode.
In fact let's go, let's pop back over there
and look at what it was that we did.
So in the documentation over here
we have this create a message resource.
So, on the message resource page here, we can look
and we can go to this create a message resource
and there's some information about it.
So you send this http post and you can see that again
we were, remember before we were doing this data
URL encode and we were doing this URL encoded.
So it's this form encoding is what's happening here.
And oh look, well a couple things
actually there's a nice warning here.
This says if you wanna send messages
while in trial mode you must verify your two numbers.
So if you wanted to send to somebody else,
you could put them in this verified caller IDs
if you wanted to, you could send to somebody else as well.
Just there. That's how you do that.
If you wanted to test or show somebody
what you're building, that's how you do that.
After of course, after you're not in trial mode,
you could submit to whoever you would like to.
So when we are creating a message,
we must include the to, right?
We need that and we need the from.
So we'll do either and we'll do body and there's
this media URL which means I can send a picture.
Let's do that. Let's make it so that it sends a picture.
Now when I am in REST Fox over here,
I don't want to use the query anymore,
I'm gonna keep the path 'cause the path is the same, right?
So we're gonna do, we're gonna do a post to that same URL,