REST APIs@alexjsinger

August 1st, 2016
What an API is How an API works
1 3
Where and why an
API is used
2
Goals
To understand…
#Servers
Servers
A computer on a network
Run software for specific tasks
Specialized servers for things like web pages,
emails, and databases
Networked systems need a way
to communicate with each other
Networked systems
communicate each other
Dash Button
Client-Server Model
Client-Server Model
Client Server
(laptop, phone, physical device) (…just a computer)
Challenges
Systems don’t “speak the same language”
Don’t expose server code
Change server code without breaking
communications
Ensure communication is secure
#RESTAPI
FTW
presentationalRe
rogrammingS
Transfer
A
P
I
pplication
nterface
tate
Request / response communication
HTTP
Request
https://www.twitter.com/onedirection
1. URL
2. Method
3. Header information
4. Body
Request / response communication
HTTP
Request
https://www.twitter.com/onedirection
Response
1. Data
2. Header information
3. Status Code
http://www.songzify.com/songssongssongzify.com
Request - URL (endpoint)
Unique address for a “thing” or resource on a server
Examples:
Domain Path
http://www.songzify.com/artists
Request - Method
A CRUD style verb to associate the endpoint with an action on the server
Method Description
GET Read resource data from the server
POST Create a resource on the server
PUT Update a resource on the server
DELETE Delete a resource from the server
Request - Method
A CRUD style verb to associate the endpoint with an action on the server
Method Description
GET Read resource data from the server
POST Create a resource on the server
PUT Update a resource on the server
DELETE Delete a resource from the server
Request - Method
A CRUD style verb to associate the endpoint with an action on the server
Method Description
GET Read resource data from the server
POST Create a resource on the server
PUT Update a resource on the server
DELETE Delete a resource from the server
Request - Method
A CRUD style verb to associate the endpoint with an action on the server
Method Description
GET Read resource data from the server
POST Create a resource on the server
PUT Update a resource on the server
DELETE Delete a resource from the server
Response - Data
Data returned by the server to the client after processing the request
The HTML content of a requested
web page
Examples:
JSON or XML of an API request
Response - Status Code
A numeric code to indicate the status of the request
Response - Status Code
A numeric code to indicate the status of the request
Common Codes:
Code Description
1xx Information i.e. 101 switching protocols
2xx Success i.e. 200 OK or 201 created
3xx Redirection i.e. 301 moved permanentely
4xx Client Error i.e. 401 unauthorized or 404 not found
5xx Server Error i.e. 500 internal server error
Putting it all together…
Requests use the URL, Method, Body and Headers to tell the server
what it wants to do.
GET https://api.twitter.com/1.1/statuses/user_timeline.json?
user_id=onedirection&screen_name=onedirection
Request:
Response:
Let’s take a look at the real data!
#DesignAPI
Think about features that API should expose on the server
Songzify
It should…
List all artists
List all songs by artist
List all songs
Allow an artist to be created
Allow an artist to be deleted
/artists
/songs/{artist_id}
/songs
/artists
/artists/{artist_id}
Endpoint
GET
GET
GET
POST
DELETE
Method
-
artist_id
-
artist_name
artist_id
Body
Dinner with an API
Exercise
Groups of 2
Think through a full dinner and
functionality a “Server” should
provide to satisfy the “Client”
Create a table showing the Endpoints,
Methods, and Body for each action
For each action, describe the expected
response from the server
#LearnMore
API Docs
API docs outline requirements for requests and response
It’s a good idea to become familiar with their structure since most REST
APIs follow the same format
Examples:
https://dev.twitter.com/rest/public
https://developer.spotify.com/web-api/endpoint-reference/
https://www.instagram.com/developer/endpoints/
REST APIs@alexjsinger

What an API is How an API works
1 3
Where and why an
API is used
2
Goals
To understand…
#ThankYou!

REST APIs, Girls Who Code

  • 1.
  • 2.
    What an APIis How an API works 1 3 Where and why an API is used 2 Goals To understand…
  • 3.
  • 4.
    Servers A computer ona network Run software for specific tasks Specialized servers for things like web pages, emails, and databases
  • 5.
    Networked systems needa way to communicate with each other Networked systems communicate each other
  • 9.
  • 10.
  • 11.
    Client-Server Model Client Server (laptop,phone, physical device) (…just a computer)
  • 12.
    Challenges Systems don’t “speakthe same language” Don’t expose server code Change server code without breaking communications Ensure communication is secure
  • 13.
  • 14.
  • 15.
    Request / responsecommunication HTTP Request https://www.twitter.com/onedirection 1. URL 2. Method 3. Header information 4. Body
  • 16.
    Request / responsecommunication HTTP Request https://www.twitter.com/onedirection Response 1. Data 2. Header information 3. Status Code
  • 17.
    http://www.songzify.com/songssongssongzify.com Request - URL(endpoint) Unique address for a “thing” or resource on a server Examples: Domain Path http://www.songzify.com/artists
  • 18.
    Request - Method ACRUD style verb to associate the endpoint with an action on the server Method Description GET Read resource data from the server POST Create a resource on the server PUT Update a resource on the server DELETE Delete a resource from the server
  • 19.
    Request - Method ACRUD style verb to associate the endpoint with an action on the server Method Description GET Read resource data from the server POST Create a resource on the server PUT Update a resource on the server DELETE Delete a resource from the server
  • 20.
    Request - Method ACRUD style verb to associate the endpoint with an action on the server Method Description GET Read resource data from the server POST Create a resource on the server PUT Update a resource on the server DELETE Delete a resource from the server
  • 21.
    Request - Method ACRUD style verb to associate the endpoint with an action on the server Method Description GET Read resource data from the server POST Create a resource on the server PUT Update a resource on the server DELETE Delete a resource from the server
  • 22.
    Response - Data Datareturned by the server to the client after processing the request The HTML content of a requested web page Examples: JSON or XML of an API request
  • 23.
    Response - StatusCode A numeric code to indicate the status of the request
  • 24.
    Response - StatusCode A numeric code to indicate the status of the request Common Codes: Code Description 1xx Information i.e. 101 switching protocols 2xx Success i.e. 200 OK or 201 created 3xx Redirection i.e. 301 moved permanentely 4xx Client Error i.e. 401 unauthorized or 404 not found 5xx Server Error i.e. 500 internal server error
  • 25.
    Putting it alltogether… Requests use the URL, Method, Body and Headers to tell the server what it wants to do. GET https://api.twitter.com/1.1/statuses/user_timeline.json? user_id=onedirection&screen_name=onedirection Request: Response: Let’s take a look at the real data!
  • 26.
  • 27.
    Think about featuresthat API should expose on the server Songzify It should… List all artists List all songs by artist List all songs Allow an artist to be created Allow an artist to be deleted /artists /songs/{artist_id} /songs /artists /artists/{artist_id} Endpoint GET GET GET POST DELETE Method - artist_id - artist_name artist_id Body
  • 28.
    Dinner with anAPI Exercise Groups of 2 Think through a full dinner and functionality a “Server” should provide to satisfy the “Client” Create a table showing the Endpoints, Methods, and Body for each action For each action, describe the expected response from the server
  • 29.
  • 30.
    API Docs API docsoutline requirements for requests and response It’s a good idea to become familiar with their structure since most REST APIs follow the same format Examples: https://dev.twitter.com/rest/public https://developer.spotify.com/web-api/endpoint-reference/ https://www.instagram.com/developer/endpoints/
  • 31.
  • 32.
    What an APIis How an API works 1 3 Where and why an API is used 2 Goals To understand…
  • 33.