Understanding
APIs
How APIs and
RESTful APIs work
behind the scenes
What is an API?
1
Application
Programming
Interface
But not a “visual” interface like what we
see in this presentation
1
It’s how one computer
can talk to another
computer.
It doesn’t matter what programming
language you’re using. JavaScript,
Python, PHP, Java, C and every other
modern language supports RESTful APIs.
More on this in a bit...
There are many types
of APIs.
But RESTful APIs are most
commonly talked about these days.
RESTful APIs are
what we’re talking
about today, too.
Think of an
API as a
waiter as a
restaurant.
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
What can I get
for you?
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
Your finest
pizza!
The waiter
relays the
message to
the kitchen.
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
Where the
chef makes
the food.
Art by freepik at https://www.freepik.com/freepik
Then the
waiter
brings your
food to you.
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
It’s that simple!
Just always keep this metaphor in mind.
Remember the restaurant.
RESTful APIs are meant
to be simple.
Let’s look at a real life
example with actual
computers.
This is a site that uses an
API to collect flight prices
from other websites.
These are
airline
services.
They hold
all the
data.
Skyscanner
will ask
each one
for flight
data.
Now you
can see all
the flight
prices from
other
websites
Computers use APIs to
talk to each other over
the internet.
What programming
languages can we
use?
Art by artmonkey at https://www.freepik.com/artmonkey
What programming
languages can we
use?
Art by artmonkey at https://www.freepik.com/artmonkey
We can use any
modern language
that you’d use for a
website.
● Python
● JavaScript
● PHP
● Java
● C
● Ruby
● Etc
What are
RESTful APIs?
REST is a type of API
Art by freepik at https://www.freepik.com/freepik
REpresentational
State
Transfer
Simply put:
● Client computer asks another
computer for data, or to take an
action
SWAPI
The Star Wars API
https://swapi.co/
JavaScript Demo
fetch('https://swapi.co/api/people/')
.then(res => res.json())
.then(response => console.log(response))
Hello, JSON
Hello, JSON
JavaScript Object Notation
Most languages have a data structure
that looks like a JavaScript Object.
One day, someone decided it should
be a standard.
Then it became the standard.
Request Methods:
● HTTP GET
● HTTP POST
● HTTP PUT
● HTTP DELETE
● HTTP PATCH
The first method:
● HTTP GET
● HTTP POST
● HTTP PUT
● HTTP DELETE
● HTTP PATCH
GET
Requests
How do the work?
GET Requests
● When you load a website.
That’s a GET request
● It’s a request to get data from
another computer
● You’re simply asking for data
and you’re not asking to
perform a task
● You’re not creating, updating or
deleting data
● Most common request type
HTTP Methods for RESTful Requests
HTTP
Method
CRUD Operation Example URL(s)
GET Read
HTTP GET http://website.com/api/users/
HTTP GET http://website.com/api/users/1/
POST
Requests
How do the work?
POST Requests
● Do not go through the standard
URL, but use a URL as the
endpoint
● Ask another computer to create
a new resource
● Returns data about the newly
created resource
HTTP Methods for RESTful Requests
HTTP
Method
CRUD Operation Example URL(s)
GET Read
HTTP GET http://website.com/api/users/
HTTP GET http://website.com/api/users/1/
POST Create HTTP POST http://website.com/api/users/
DELETE
Requests
How do the work?
DELETE Requests
● Do not go through the standard
URL, but use a URL as the
endpoint
● Ask another computer to delete
a single resource or a list of
resources
● Use with caution
HTTP Methods for RESTful Requests
HTTP
Method
CRUD Operation Example URL(s)
GET Read
HTTP GET http://website.com/api/users/
HTTP GET http://website.com/api/users/1/
POST Create HTTP POST http://website.com/api/users/
DELETE Delete HTTP DELETE http://website.com/api/user/1/
PUT/PATCH
Requests
How do the work?
PATCH Requests
● Do not go through the standard
URL, but use a URL as the
endpoint
● Ask another computer to
update a piece of a resource
● Are not fully supported by all
browsers or frameworks, so we
typically fall back on PUT
requests
● Example: Updating a users first
name
PUT Requests
● Do not go through the standard
URL, but use a URL as the
endpoint
● Ask another computer to
update an entire resource
● If the resource doesn’t exist,
the API might decide to
CREATE (CRUD) the resource
HTTP Methods for RESTful Requests
HTTP
Method
CRUD Operation Example URL(s)
GET Read
HTTP GET http://website.com/api/users/
HTTP GET http://website.com/api/users/1/
POST Create HTTP POST http://website.com/api/users/
DELETE Delete HTTP DELETE http://website.com/api/user/1/
PUT Update/Replace HTTP PUT http://website.com/api/user/1/
PATCH Partial Update/Modify HTTP PATCH http://website.com/api/user/1/
More details at https://restfulapi.net/http-methods/
Consuming
APIs
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
APIs can be written in almost
any server-side language.
Art by starline at https://www.freepik.com/starline
APIs will generally return
one of two types data
structures:
JSON or XML
Art by starline at https://www.freepik.com/starline
JSON Example
{
"key_val_example": "value",
"array_example": [
'array item 1',
'array item 2',
],
"object_example": {
"key1": "value1",
"key2": "value2"
}
}
Art by starline at https://www.freepik.com/starline
XML Example
<example>
<field>
Value
</field>
<secondField>
Value
</secondField>
<nestedExample>
<nestedField>
Value
</nestedField>
<nestedSecondField>
Value
</nestedSecondField>
</nestedExample>
</example>
APIs can be consumed in
almost any language.
Art by starline at https://www.freepik.com/starline
Browsers use JavaScript for
their API requests.
Servers use any language that
runs on that computer.
Art by starline at https://www.freepik.com/starline
Common API
Responses
What are they?
Requests and
Responses
When you request data from a
server using GET, POST, PUT,
PATCH or DELETE… that’s a
request.
When the server returns your
data… that’s a response
Requests and
Responses
Responses will always come
with an HTTP Status Code.
And these “status codes” tell you
what’s wrong (or right) without
needing to give you text back to
read.
Common HTTP
Status Codes
Healthy Responses (2--)
● 200 — OK.
Request accepted.
● 201 — Created.
POST requests often return 201s when a resource is created.
● 202 — Accepted.
When a request is accepted but its not done processing.
Maybe the task goes into a queue.
Common HTTP
Status Codes
Redirect Responses (3--)
● 301 — Moved Permanently.
When the endpoint has permanently changed. Update your
endpoint.
● 302 — Found.
The endpoint you’re accessing is temporarily moved to
somewhere else.
Common HTTP
Status Codes
Client Responses (4--)
● 400 — Bad Request.
Server cannot or will not process your request. Often this is
due to malformed API keys or an invalid payload.
● 401 — Unauthorized.
You’re not allowed here. Usually this is because you’re
missing authentication credentials (API keys)
● 403 — Forbidden.
The servers understands your request but won’t execute it.
Your API keys might not have the right permissions or your
trying to use an endpoint that you don’t have access to.
● 404 — Not Found.
There’s nothing here. Move along, move along.
● 405 — Method Not Allowed.
You’re using the wrong HTTP Method. The endpoint might
only accept GET requests and you might be POSTing to it,
for example.
Common HTTP
Status Codes Server Responses (5--)
● 500 — Internal Server Error.
The server had a problem and couldn’t process the request.
This is the only time you are out of control.
Just For Fun Find out
what a 418
response is
API Security
Art by freepik at https://www.freepik.com/freepik
API Keys
● API keys are “passwords” to
access an API. These are your
authentication credentials.
● Almost every website requires
API keys to perform some action.
● Facebook's Graph API is a good
example
graph.facebook.com/codingforeverybody
Access token
required.
Access tokens are
usually generated
with an API key.
Matching 400 status
code.
“Bad Request”.
Something is missing.
Access token
required.
Access tokens are
usually generated
with an API key.
Other services might
throw you a 403 or
405 status.
Art by Sapann Design at https://www.freepik.com/sapann-design
Summary
Think of an
API as a
waiter as a
restaurant.
Art by iconicbestiary at https://www.freepik.com/iconicbestiary
HTTP Methods for RESTful Requests
HTTP
Method
CRUD Operation Example URL(s)
GET Read
HTTP GET http://website.com/api/users/
HTTP GET http://website.com/api/users/1/
POST Create HTTP POST http://website.com/api/users/
DELETE Delete HTTP DELETE http://website.com/api/user/1/
PUT Update/Replace HTTP PUT http://website.com/api/user/1/
PATCH Partial Update/Modify HTTP PATCH http://website.com/api/user/1/
Most APIs are secured with API keys
Art by freepik at https://www.freepik.com/freepik
Free Resources
● https://restfulapi.net/http-methods/
● https://httpstatuses.com/
● https://swapi.co/
Art by rawpixel.com at https://www.freepik.com/rawpixel-com
Questions?

Understanding APIs.pptx

  • 1.
    Understanding APIs How APIs and RESTfulAPIs work behind the scenes
  • 2.
  • 3.
    1 Application Programming Interface But not a“visual” interface like what we see in this presentation
  • 4.
    1 It’s how onecomputer can talk to another computer. It doesn’t matter what programming language you’re using. JavaScript, Python, PHP, Java, C and every other modern language supports RESTful APIs. More on this in a bit...
  • 5.
    There are manytypes of APIs. But RESTful APIs are most commonly talked about these days.
  • 6.
    RESTful APIs are whatwe’re talking about today, too.
  • 7.
    Think of an APIas a waiter as a restaurant. Art by iconicbestiary at https://www.freepik.com/iconicbestiary
  • 8.
    Art by iconicbestiaryat https://www.freepik.com/iconicbestiary What can I get for you?
  • 9.
    Art by iconicbestiaryat https://www.freepik.com/iconicbestiary Your finest pizza!
  • 10.
    The waiter relays the messageto the kitchen. Art by iconicbestiary at https://www.freepik.com/iconicbestiary
  • 11.
    Where the chef makes thefood. Art by freepik at https://www.freepik.com/freepik
  • 12.
    Then the waiter brings your foodto you. Art by iconicbestiary at https://www.freepik.com/iconicbestiary
  • 13.
    It’s that simple! Justalways keep this metaphor in mind. Remember the restaurant.
  • 14.
    RESTful APIs aremeant to be simple.
  • 15.
    Let’s look ata real life example with actual computers.
  • 16.
    This is asite that uses an API to collect flight prices from other websites.
  • 17.
  • 18.
  • 19.
    Now you can seeall the flight prices from other websites
  • 20.
    Computers use APIsto talk to each other over the internet.
  • 21.
    What programming languages canwe use? Art by artmonkey at https://www.freepik.com/artmonkey
  • 22.
    What programming languages canwe use? Art by artmonkey at https://www.freepik.com/artmonkey
  • 23.
    We can useany modern language that you’d use for a website. ● Python ● JavaScript ● PHP ● Java ● C ● Ruby ● Etc
  • 24.
    What are RESTful APIs? RESTis a type of API Art by freepik at https://www.freepik.com/freepik
  • 25.
  • 26.
    Simply put: ● Clientcomputer asks another computer for data, or to take an action
  • 27.
    SWAPI The Star WarsAPI https://swapi.co/
  • 28.
  • 30.
  • 31.
  • 32.
    Most languages havea data structure that looks like a JavaScript Object. One day, someone decided it should be a standard. Then it became the standard.
  • 33.
    Request Methods: ● HTTPGET ● HTTP POST ● HTTP PUT ● HTTP DELETE ● HTTP PATCH
  • 34.
    The first method: ●HTTP GET ● HTTP POST ● HTTP PUT ● HTTP DELETE ● HTTP PATCH
  • 35.
  • 36.
    GET Requests ● Whenyou load a website. That’s a GET request ● It’s a request to get data from another computer ● You’re simply asking for data and you’re not asking to perform a task ● You’re not creating, updating or deleting data ● Most common request type
  • 37.
    HTTP Methods forRESTful Requests HTTP Method CRUD Operation Example URL(s) GET Read HTTP GET http://website.com/api/users/ HTTP GET http://website.com/api/users/1/
  • 38.
  • 39.
    POST Requests ● Donot go through the standard URL, but use a URL as the endpoint ● Ask another computer to create a new resource ● Returns data about the newly created resource
  • 40.
    HTTP Methods forRESTful Requests HTTP Method CRUD Operation Example URL(s) GET Read HTTP GET http://website.com/api/users/ HTTP GET http://website.com/api/users/1/ POST Create HTTP POST http://website.com/api/users/
  • 41.
  • 42.
    DELETE Requests ● Donot go through the standard URL, but use a URL as the endpoint ● Ask another computer to delete a single resource or a list of resources ● Use with caution
  • 43.
    HTTP Methods forRESTful Requests HTTP Method CRUD Operation Example URL(s) GET Read HTTP GET http://website.com/api/users/ HTTP GET http://website.com/api/users/1/ POST Create HTTP POST http://website.com/api/users/ DELETE Delete HTTP DELETE http://website.com/api/user/1/
  • 44.
  • 45.
    PATCH Requests ● Donot go through the standard URL, but use a URL as the endpoint ● Ask another computer to update a piece of a resource ● Are not fully supported by all browsers or frameworks, so we typically fall back on PUT requests ● Example: Updating a users first name
  • 46.
    PUT Requests ● Donot go through the standard URL, but use a URL as the endpoint ● Ask another computer to update an entire resource ● If the resource doesn’t exist, the API might decide to CREATE (CRUD) the resource
  • 47.
    HTTP Methods forRESTful Requests HTTP Method CRUD Operation Example URL(s) GET Read HTTP GET http://website.com/api/users/ HTTP GET http://website.com/api/users/1/ POST Create HTTP POST http://website.com/api/users/ DELETE Delete HTTP DELETE http://website.com/api/user/1/ PUT Update/Replace HTTP PUT http://website.com/api/user/1/ PATCH Partial Update/Modify HTTP PATCH http://website.com/api/user/1/ More details at https://restfulapi.net/http-methods/
  • 48.
    Consuming APIs Art by iconicbestiaryat https://www.freepik.com/iconicbestiary
  • 49.
    APIs can bewritten in almost any server-side language. Art by starline at https://www.freepik.com/starline
  • 50.
    APIs will generallyreturn one of two types data structures: JSON or XML Art by starline at https://www.freepik.com/starline
  • 51.
    JSON Example { "key_val_example": "value", "array_example":[ 'array item 1', 'array item 2', ], "object_example": { "key1": "value1", "key2": "value2" } } Art by starline at https://www.freepik.com/starline XML Example <example> <field> Value </field> <secondField> Value </secondField> <nestedExample> <nestedField> Value </nestedField> <nestedSecondField> Value </nestedSecondField> </nestedExample> </example>
  • 52.
    APIs can beconsumed in almost any language. Art by starline at https://www.freepik.com/starline
  • 53.
    Browsers use JavaScriptfor their API requests. Servers use any language that runs on that computer. Art by starline at https://www.freepik.com/starline
  • 54.
  • 55.
    Requests and Responses When yourequest data from a server using GET, POST, PUT, PATCH or DELETE… that’s a request. When the server returns your data… that’s a response
  • 56.
    Requests and Responses Responses willalways come with an HTTP Status Code. And these “status codes” tell you what’s wrong (or right) without needing to give you text back to read.
  • 57.
    Common HTTP Status Codes HealthyResponses (2--) ● 200 — OK. Request accepted. ● 201 — Created. POST requests often return 201s when a resource is created. ● 202 — Accepted. When a request is accepted but its not done processing. Maybe the task goes into a queue.
  • 58.
    Common HTTP Status Codes RedirectResponses (3--) ● 301 — Moved Permanently. When the endpoint has permanently changed. Update your endpoint. ● 302 — Found. The endpoint you’re accessing is temporarily moved to somewhere else.
  • 59.
    Common HTTP Status Codes ClientResponses (4--) ● 400 — Bad Request. Server cannot or will not process your request. Often this is due to malformed API keys or an invalid payload. ● 401 — Unauthorized. You’re not allowed here. Usually this is because you’re missing authentication credentials (API keys) ● 403 — Forbidden. The servers understands your request but won’t execute it. Your API keys might not have the right permissions or your trying to use an endpoint that you don’t have access to. ● 404 — Not Found. There’s nothing here. Move along, move along. ● 405 — Method Not Allowed. You’re using the wrong HTTP Method. The endpoint might only accept GET requests and you might be POSTing to it, for example.
  • 60.
    Common HTTP Status CodesServer Responses (5--) ● 500 — Internal Server Error. The server had a problem and couldn’t process the request. This is the only time you are out of control.
  • 61.
    Just For FunFind out what a 418 response is
  • 62.
    API Security Art byfreepik at https://www.freepik.com/freepik
  • 63.
    API Keys ● APIkeys are “passwords” to access an API. These are your authentication credentials. ● Almost every website requires API keys to perform some action. ● Facebook's Graph API is a good example graph.facebook.com/codingforeverybody
  • 64.
    Access token required. Access tokensare usually generated with an API key. Matching 400 status code. “Bad Request”. Something is missing.
  • 65.
    Access token required. Access tokensare usually generated with an API key. Other services might throw you a 403 or 405 status.
  • 66.
    Art by SapannDesign at https://www.freepik.com/sapann-design Summary
  • 67.
    Think of an APIas a waiter as a restaurant. Art by iconicbestiary at https://www.freepik.com/iconicbestiary
  • 69.
    HTTP Methods forRESTful Requests HTTP Method CRUD Operation Example URL(s) GET Read HTTP GET http://website.com/api/users/ HTTP GET http://website.com/api/users/1/ POST Create HTTP POST http://website.com/api/users/ DELETE Delete HTTP DELETE http://website.com/api/user/1/ PUT Update/Replace HTTP PUT http://website.com/api/user/1/ PATCH Partial Update/Modify HTTP PATCH http://website.com/api/user/1/
  • 70.
    Most APIs aresecured with API keys Art by freepik at https://www.freepik.com/freepik
  • 71.
    Free Resources ● https://restfulapi.net/http-methods/ ●https://httpstatuses.com/ ● https://swapi.co/ Art by rawpixel.com at https://www.freepik.com/rawpixel-com
  • 73.