際際滷

際際滷Share a Scribd company logo
Web API Design
B Anjaneyulu Reddy
"pragmatic" REST
"pragmatic" definition:
Dealing with things sensibly and realistically in a way that is
based on practical rather than theoretical considerations.


"Outside-in" approach
What are we trying to achieve with an API?

The API's job is to make the developer as successful as
possible. The orientation for APIs is to think about design
choices from the application developer's point of view.
API ~ WiFi
APIs should be like the wifi. Any device can
connect to it and use all the functionalities.




What is the design with optimal benefit for the app
developer?
Nouns - good; verbs - bad
Thumb rule: Keep simple things simple


Simple and intuitive base URL
Affordance is a design property that communicates how
something should be used without requiring document.

There should be only 2 base URLs per resource.
Example:     /dogs     /dogs/1234
Nouns - good; verbs - bad
Keep verbs out of base URLs




This will lead to:
  Long list of URLs
  No consistent pattern

Which will make it difficult for developers to learn and use.
Nouns - good; verbs - bad
HTTP verbs for rescue
Plural & concrete nouns
 Use either plural or singular names but be consistent.
Ex: Foursquare: /checkins           GroupOn: /deals        Zappos: /product


 Concrete names are better than abstract
Ex: API that access content in various form - blogs, videos, news articles and
so on.

- Abstract names: /items or /assets
- Concrete names: /blogs, /videos, /news

Aim for concrete naming & number of resources between 12 and 24
Simplify associations
Problem
Resources always have association relationships to other
resources.
Ex: Get all the dogs who belong to owner 13.

The relationships can be more complex which leads URLs with multi level
depth.
Ex: /resource/identifier/resource/identifier/resource
In this case: /owner/13/dogs


Solution: sweep complexity behind HTTP "?"
Ex: /dogs?owner=13&color=red
Handling errors (1/5)
 Web API is a black box for developer
 error codes becomes a key tool to provide context and
  visibility to use APIs

Best practices for designing error codes:

 Use HTTP status codes
 Make messages returned in the payload as verbose as
  possible
Handling errors (2/5)
1. Use HTTP status code

 Try and map the status codes to relevant standard-
  based codes

 The are around 70 HTTP status codes. Use only those
  which are very common
Handling errors (3/5)
How many status codes should we use?
The are only 3 real outcomes:
 Everything worked - success
 The application did something wrong - client error
 The API did something wrong - server error
Handling errors (4/5)
Start using with few (say 3) codes and add as per the
requirement. But should not be more than 8.
Sample codes:
  201 - Created
  304 - Not Modified
  404 - Not Found
  403 - Forbidden
  401 - Unauthorised


It is important that the code that is returned is something
that can be consumed and acted upon by the developer.

Click here to get the complete list of HTTP error codes.
Handling errors (5/5)
2. Additional information in response message

 Use plain language to describe the error

 Link to more information page related to the error in the
  description is highly recommended
Versioning
Never release an API without at version and make the
version mandatory.
Twilio: /2010-04-01/accounts
Facebook: ?v=1.0


How to think about versioning?
 Specify the version with a 'v' prefix
 'v' tag should have the highest scope: (ex: /v1/dogs)
 Avoid dot notation in versions (ex: v1, v2)
Pagination
Support partial response by adding optional fields in a comma delimited list

Linkedin: /people:(id,first-name,last-name,industry)
Facebook: /joe.smith/friends?fields=id,name,picture
Google: ?fields=title,media:group(media:thumbnail)

Use limit and offset to make it easy for developers to paginate objects


Facebook: offset 50 and limit 25
Linkedin: start 50 and count 25
Twitter: page 3 and rpp 25 (records per page)
Non resource responses
Use verbs not nouns for all the APIs that do not return a
resource. For example:

Calculate
Translate
Convert

/convert?from=EURO&to=USD&amount=100


Separate out a section of documentation for all the non
resource returning requests.
Multiple formats
Support more than one format for returning resources.

Google data: ?alt=json
FourSquare: /venue.json (recommended)


Recommendations:
 Use JSON as default format
 Follow JavaScript conventions for naming attributes:
    Use medial capitalisation (aka CamelCase)
    Use uppercase or lowercase depending on the
      object.
Tips for search
Global search
/search?q=foo+bar (Google approach)

Scoped search
/owners/5283/dogs?q=foo+bar

Note: Parameter 'q' indicates a search query.
Subdomain for APIs
Consolidate all API requests in a single subdomain.

api.facebook.com
graph.facebook.com

api.foursquare.com

api.twitter.com
stream.twitter.com
search.twitter.com
Authentication
Recommendation: Use the latest OAuth

Facebook: OAuth 2.0
Twitter: OAuth 1.0a
PayPal: Permission service API
API facade pattern (1/4)
Credible, relevant and differentiated
API facade pattern (2/4)
Three steps approach:
 Design the ideal API
Design the URLs, request parameters and responses, payloads, headers,
query parameters, and so on. The API design should be self consistent

 Implement the design with data stubs
User temporary data without connecting to the internal system.

 Mediate (or) integrate between the facade and the
  systems
One big problem -> 3 smaller problems
API facade pattern (3/4)
Focus on app developer. API should be:

 Easy to use

 Self-consistent

 Intuitive
API facade pattern (4/4)
References
 Apigee design document: http://goo.gl/5uDdn
 Wikipedia
   API: http://en.wikipedia.org/wiki/Api
   HTTP Status codes: http://goo.gl/muq8s

More Related Content

What's hot (19)

API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
Tom Johnson
What's coming in c# 9.0
What's coming in c# 9.0What's coming in c# 9.0
What's coming in c# 9.0
Moaid Hathot
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)
Tom Johnson
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Tom Johnson
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Bhavsingh Maloth
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
Sylvain Bouchard
API Workshop: Deep dive into code samples
API Workshop: Deep dive into code samplesAPI Workshop: Deep dive into code samples
API Workshop: Deep dive into code samples
Tom Johnson
What's New in C# ? C# 6, 7.0, 7.1 and 7.2
What's New in C# ? C# 6, 7.0, 7.1 and 7.2What's New in C# ? C# 6, 7.0, 7.1 and 7.2
What's New in C# ? C# 6, 7.0, 7.1 and 7.2
David Stevenson
Am4201257261
Am4201257261Am4201257261
Am4201257261
IJERA Editor
intenranional SEO Best practices
intenranional SEO Best practices intenranional SEO Best practices
intenranional SEO Best practices
Anton Surov
STC Summit 2015: API Documentation, an Example-Based Approach
STC Summit 2015: API Documentation, an Example-Based ApproachSTC Summit 2015: API Documentation, an Example-Based Approach
STC Summit 2015: API Documentation, an Example-Based Approach
Lois Patterson
API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015
Tom Johnson
Microservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerMicroservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and Docker
Dhilipsiva DS
CIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NETCIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NET
webhostingguy
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
What is Responsive Web Design?
What is Responsive Web Design?What is Responsive Web Design?
What is Responsive Web Design?
Intellipaat
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into Java
Tom Johnson
Sexy Using Cucumber - BDD in your project
Sexy Using Cucumber - BDD in your projectSexy Using Cucumber - BDD in your project
Sexy Using Cucumber - BDD in your project
b4usolution .
Data Applied: Developer Quicklook
Data Applied: Developer QuicklookData Applied: Developer Quicklook
Data Applied: Developer Quicklook
DataminingTools Inc
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
Tom Johnson
What's coming in c# 9.0
What's coming in c# 9.0What's coming in c# 9.0
What's coming in c# 9.0
Moaid Hathot
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)
Tom Johnson
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Tom Johnson
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Bhavsingh Maloth
API Workshop: Deep dive into code samples
API Workshop: Deep dive into code samplesAPI Workshop: Deep dive into code samples
API Workshop: Deep dive into code samples
Tom Johnson
What's New in C# ? C# 6, 7.0, 7.1 and 7.2
What's New in C# ? C# 6, 7.0, 7.1 and 7.2What's New in C# ? C# 6, 7.0, 7.1 and 7.2
What's New in C# ? C# 6, 7.0, 7.1 and 7.2
David Stevenson
intenranional SEO Best practices
intenranional SEO Best practices intenranional SEO Best practices
intenranional SEO Best practices
Anton Surov
STC Summit 2015: API Documentation, an Example-Based Approach
STC Summit 2015: API Documentation, an Example-Based ApproachSTC Summit 2015: API Documentation, an Example-Based Approach
STC Summit 2015: API Documentation, an Example-Based Approach
Lois Patterson
API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015
Tom Johnson
Microservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerMicroservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and Docker
Dhilipsiva DS
CIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NETCIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NET
webhostingguy
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
What is Responsive Web Design?
What is Responsive Web Design?What is Responsive Web Design?
What is Responsive Web Design?
Intellipaat
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into Java
Tom Johnson
Sexy Using Cucumber - BDD in your project
Sexy Using Cucumber - BDD in your projectSexy Using Cucumber - BDD in your project
Sexy Using Cucumber - BDD in your project
b4usolution .
Data Applied: Developer Quicklook
Data Applied: Developer QuicklookData Applied: Developer Quicklook
Data Applied: Developer Quicklook
DataminingTools Inc

Viewers also liked (18)

Javascript: The good parts for humans (part 1)
Javascript: The good parts for humans (part 1)Javascript: The good parts for humans (part 1)
Javascript: The good parts for humans (part 1)
Anji Beeravalli
Example Acrolinx Language Analysis
Example Acrolinx Language AnalysisExample Acrolinx Language Analysis
Example Acrolinx Language Analysis
Center for Plain Language
Vocabulary - Week 1
Vocabulary - Week 1Vocabulary - Week 1
Vocabulary - Week 1
lressler
ROC History
ROC HistoryROC History
ROC History
mabielick
NaveenParameshwaraiah_June2015
NaveenParameshwaraiah_June2015NaveenParameshwaraiah_June2015
NaveenParameshwaraiah_June2015
Naveen Parameshwaraiah
Ismar 2010 Presentation
Ismar 2010 PresentationIsmar 2010 Presentation
Ismar 2010 Presentation
Julian Stadon
Juan Domingo Per坦n carta al presidente kennedy
Juan Domingo Per坦n   carta al presidente kennedyJuan Domingo Per坦n   carta al presidente kennedy
Juan Domingo Per坦n carta al presidente kennedy
Integridad Pol鱈tica
Tuberculin n scalp vein needle
Tuberculin n scalp vein needleTuberculin n scalp vein needle
Tuberculin n scalp vein needle
Pratik Kumar
Reinventing strategies for emerging markets
Reinventing strategies for emerging marketsReinventing strategies for emerging markets
Reinventing strategies for emerging markets
Bill Gunawan
Kate Ryan, "Figure in the Sky," 2015
Kate Ryan, "Figure in the Sky," 2015Kate Ryan, "Figure in the Sky," 2015
Kate Ryan, "Figure in the Sky," 2015
Gabriel Hopson
Architect Mohamed samir portfolio
Architect Mohamed samir portfolioArchitect Mohamed samir portfolio
Architect Mohamed samir portfolio
mohamed samir
Naufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvesselNaufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvessel
Naufal Hakim
Ahmed obaid
Ahmed obaidAhmed obaid
Ahmed obaid
Ahmed Obaid
鴛鉛顎壊岳姻温界庄坦稼鴛鉛顎壊岳姻温界庄坦稼
鴛鉛顎壊岳姻温界庄坦稼
Daniela Giraldo
How Dangerous Are Motorcycles, Really?
How Dangerous Are Motorcycles, Really?How Dangerous Are Motorcycles, Really?
How Dangerous Are Motorcycles, Really?
TastyPlacement
Blogs: Reading and Writing (no Arithmetic!)
Blogs: Reading and Writing (no Arithmetic!)Blogs: Reading and Writing (no Arithmetic!)
Blogs: Reading and Writing (no Arithmetic!)
MascotManor
Drugs
DrugsDrugs
Drugs
Pratik Kumar
Um readr circl
Um readr circlUm readr circl
Um readr circl
Wan Faizuddin
Javascript: The good parts for humans (part 1)
Javascript: The good parts for humans (part 1)Javascript: The good parts for humans (part 1)
Javascript: The good parts for humans (part 1)
Anji Beeravalli
Vocabulary - Week 1
Vocabulary - Week 1Vocabulary - Week 1
Vocabulary - Week 1
lressler
ROC History
ROC HistoryROC History
ROC History
mabielick
Ismar 2010 Presentation
Ismar 2010 PresentationIsmar 2010 Presentation
Ismar 2010 Presentation
Julian Stadon
Juan Domingo Per坦n carta al presidente kennedy
Juan Domingo Per坦n   carta al presidente kennedyJuan Domingo Per坦n   carta al presidente kennedy
Juan Domingo Per坦n carta al presidente kennedy
Integridad Pol鱈tica
Tuberculin n scalp vein needle
Tuberculin n scalp vein needleTuberculin n scalp vein needle
Tuberculin n scalp vein needle
Pratik Kumar
Reinventing strategies for emerging markets
Reinventing strategies for emerging marketsReinventing strategies for emerging markets
Reinventing strategies for emerging markets
Bill Gunawan
Kate Ryan, "Figure in the Sky," 2015
Kate Ryan, "Figure in the Sky," 2015Kate Ryan, "Figure in the Sky," 2015
Kate Ryan, "Figure in the Sky," 2015
Gabriel Hopson
Architect Mohamed samir portfolio
Architect Mohamed samir portfolioArchitect Mohamed samir portfolio
Architect Mohamed samir portfolio
mohamed samir
Naufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvesselNaufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvessel
Naufal Hakim
鴛鉛顎壊岳姻温界庄坦稼鴛鉛顎壊岳姻温界庄坦稼
鴛鉛顎壊岳姻温界庄坦稼
Daniela Giraldo
How Dangerous Are Motorcycles, Really?
How Dangerous Are Motorcycles, Really?How Dangerous Are Motorcycles, Really?
How Dangerous Are Motorcycles, Really?
TastyPlacement
Blogs: Reading and Writing (no Arithmetic!)
Blogs: Reading and Writing (no Arithmetic!)Blogs: Reading and Writing (no Arithmetic!)
Blogs: Reading and Writing (no Arithmetic!)
MascotManor

Similar to Web REST APIs Design Principles (20)

Restful webservices
Restful webservicesRestful webservices
Restful webservices
Lemberg Solutions
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
Tatiana Al-Chueyr
API presentation
API presentationAPI presentation
API presentation
Carlos Justiniano
flask.pptx
flask.pptxflask.pptx
flask.pptx
asif290119
Designing Good API & Its Importance
Designing Good API & Its ImportanceDesigning Good API & Its Importance
Designing Good API & Its Importance
Imran M Yousuf
Web API Design
Web API DesignWeb API Design
Web API Design
Jyotirmoy Dey
Web API Design: Crafting Interfaces that Developers Love
Web API Design:  Crafting Interfaces that Developers LoveWeb API Design:  Crafting Interfaces that Developers Love
Web API Design: Crafting Interfaces that Developers Love
Jamison K. Bell | OvenPOP 360
Navigating API Documentation: Tips from the Trenches
Navigating API Documentation: Tips from the Trenches Navigating API Documentation: Tips from the Trenches
Navigating API Documentation: Tips from the Trenches
Nordic APIs
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
Phil Wilkins
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
Patrick Savalle
Cqrs api
Cqrs apiCqrs api
Cqrs api
Brandon Mueller
The ultimate api checklist by Blendr.io
The ultimate api checklist by Blendr.ioThe ultimate api checklist by Blendr.io
The ultimate api checklist by Blendr.io
Blendr.io
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
Brian Lyttle
APIs And SDKs Breaking Into And Succeeding In A Specialty Market
APIs And SDKs  Breaking Into And Succeeding In A Specialty MarketAPIs And SDKs  Breaking Into And Succeeding In A Specialty Market
APIs And SDKs Breaking Into And Succeeding In A Specialty Market
Bill Dubie
API Design- Best Practices
API Design-   Best PracticesAPI Design-   Best Practices
API Design- Best Practices
Prakash Bhandari
A RESTful introduction
A RESTful introductionA RESTful introduction
A RESTful introduction
Daniel Toader
IRJET- Mail Server Communication:PHP
IRJET-  	  Mail Server Communication:PHPIRJET-  	  Mail Server Communication:PHP
IRJET- Mail Server Communication:PHP
IRJET Journal
Benefits of the CodeIgniter Framework
Benefits of the CodeIgniter FrameworkBenefits of the CodeIgniter Framework
Benefits of the CodeIgniter Framework
Toby Beresford
Best practices and advantages of REST APIs
Best practices and advantages of REST APIsBest practices and advantages of REST APIs
Best practices and advantages of REST APIs
Aparna Sharma
Restful api design
Restful api designRestful api design
Restful api design
Mizan Riqzia
Designing Good API & Its Importance
Designing Good API & Its ImportanceDesigning Good API & Its Importance
Designing Good API & Its Importance
Imran M Yousuf
Web API Design: Crafting Interfaces that Developers Love
Web API Design:  Crafting Interfaces that Developers LoveWeb API Design:  Crafting Interfaces that Developers Love
Web API Design: Crafting Interfaces that Developers Love
Jamison K. Bell | OvenPOP 360
Navigating API Documentation: Tips from the Trenches
Navigating API Documentation: Tips from the Trenches Navigating API Documentation: Tips from the Trenches
Navigating API Documentation: Tips from the Trenches
Nordic APIs
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
Phil Wilkins
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
Patrick Savalle
The ultimate api checklist by Blendr.io
The ultimate api checklist by Blendr.ioThe ultimate api checklist by Blendr.io
The ultimate api checklist by Blendr.io
Blendr.io
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
Brian Lyttle
APIs And SDKs Breaking Into And Succeeding In A Specialty Market
APIs And SDKs  Breaking Into And Succeeding In A Specialty MarketAPIs And SDKs  Breaking Into And Succeeding In A Specialty Market
APIs And SDKs Breaking Into And Succeeding In A Specialty Market
Bill Dubie
API Design- Best Practices
API Design-   Best PracticesAPI Design-   Best Practices
API Design- Best Practices
Prakash Bhandari
A RESTful introduction
A RESTful introductionA RESTful introduction
A RESTful introduction
Daniel Toader
IRJET- Mail Server Communication:PHP
IRJET-  	  Mail Server Communication:PHPIRJET-  	  Mail Server Communication:PHP
IRJET- Mail Server Communication:PHP
IRJET Journal
Benefits of the CodeIgniter Framework
Benefits of the CodeIgniter FrameworkBenefits of the CodeIgniter Framework
Benefits of the CodeIgniter Framework
Toby Beresford
Best practices and advantages of REST APIs
Best practices and advantages of REST APIsBest practices and advantages of REST APIs
Best practices and advantages of REST APIs
Aparna Sharma
Restful api design
Restful api designRestful api design
Restful api design
Mizan Riqzia

More from Anji Beeravalli (7)

Javascript: The good parts for humans (part 5)
Javascript: The good parts for humans (part 5)�Javascript: The good parts for humans (part 5)�
Javascript: The good parts for humans (part 5)
Anji Beeravalli
Javascript: The good parts for humans (part 6)
Javascript: The good parts for humans (part 6)Javascript: The good parts for humans (part 6)
Javascript: The good parts for humans (part 6)
Anji Beeravalli
Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 2)Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 2)
Anji Beeravalli
Javascript: The good parts for humans (part 3)
Javascript: The good parts for humans (part 3)Javascript: The good parts for humans (part 3)
Javascript: The good parts for humans (part 3)
Anji Beeravalli
Javascript: The good parts for humans (part 4)
Javascript: The good parts for humans (part 4)Javascript: The good parts for humans (part 4)
Javascript: The good parts for humans (part 4)
Anji Beeravalli
CSS for developers
CSS for developersCSS for developers
CSS for developers
Anji Beeravalli
Business Modelling
Business ModellingBusiness Modelling
Business Modelling
Anji Beeravalli
Javascript: The good parts for humans (part 5)
Javascript: The good parts for humans (part 5)�Javascript: The good parts for humans (part 5)�
Javascript: The good parts for humans (part 5)
Anji Beeravalli
Javascript: The good parts for humans (part 6)
Javascript: The good parts for humans (part 6)Javascript: The good parts for humans (part 6)
Javascript: The good parts for humans (part 6)
Anji Beeravalli
Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 2)Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 2)
Anji Beeravalli
Javascript: The good parts for humans (part 3)
Javascript: The good parts for humans (part 3)Javascript: The good parts for humans (part 3)
Javascript: The good parts for humans (part 3)
Anji Beeravalli
Javascript: The good parts for humans (part 4)
Javascript: The good parts for humans (part 4)Javascript: The good parts for humans (part 4)
Javascript: The good parts for humans (part 4)
Anji Beeravalli

Web REST APIs Design Principles

  • 1. Web API Design B Anjaneyulu Reddy
  • 2. "pragmatic" REST "pragmatic" definition: Dealing with things sensibly and realistically in a way that is based on practical rather than theoretical considerations. "Outside-in" approach What are we trying to achieve with an API? The API's job is to make the developer as successful as possible. The orientation for APIs is to think about design choices from the application developer's point of view.
  • 3. API ~ WiFi APIs should be like the wifi. Any device can connect to it and use all the functionalities. What is the design with optimal benefit for the app developer?
  • 4. Nouns - good; verbs - bad Thumb rule: Keep simple things simple Simple and intuitive base URL Affordance is a design property that communicates how something should be used without requiring document. There should be only 2 base URLs per resource. Example: /dogs /dogs/1234
  • 5. Nouns - good; verbs - bad Keep verbs out of base URLs This will lead to: Long list of URLs No consistent pattern Which will make it difficult for developers to learn and use.
  • 6. Nouns - good; verbs - bad HTTP verbs for rescue
  • 7. Plural & concrete nouns Use either plural or singular names but be consistent. Ex: Foursquare: /checkins GroupOn: /deals Zappos: /product Concrete names are better than abstract Ex: API that access content in various form - blogs, videos, news articles and so on. - Abstract names: /items or /assets - Concrete names: /blogs, /videos, /news Aim for concrete naming & number of resources between 12 and 24
  • 8. Simplify associations Problem Resources always have association relationships to other resources. Ex: Get all the dogs who belong to owner 13. The relationships can be more complex which leads URLs with multi level depth. Ex: /resource/identifier/resource/identifier/resource In this case: /owner/13/dogs Solution: sweep complexity behind HTTP "?" Ex: /dogs?owner=13&color=red
  • 9. Handling errors (1/5) Web API is a black box for developer error codes becomes a key tool to provide context and visibility to use APIs Best practices for designing error codes: Use HTTP status codes Make messages returned in the payload as verbose as possible
  • 10. Handling errors (2/5) 1. Use HTTP status code Try and map the status codes to relevant standard- based codes The are around 70 HTTP status codes. Use only those which are very common
  • 11. Handling errors (3/5) How many status codes should we use? The are only 3 real outcomes: Everything worked - success The application did something wrong - client error The API did something wrong - server error
  • 12. Handling errors (4/5) Start using with few (say 3) codes and add as per the requirement. But should not be more than 8. Sample codes: 201 - Created 304 - Not Modified 404 - Not Found 403 - Forbidden 401 - Unauthorised It is important that the code that is returned is something that can be consumed and acted upon by the developer. Click here to get the complete list of HTTP error codes.
  • 13. Handling errors (5/5) 2. Additional information in response message Use plain language to describe the error Link to more information page related to the error in the description is highly recommended
  • 14. Versioning Never release an API without at version and make the version mandatory. Twilio: /2010-04-01/accounts Facebook: ?v=1.0 How to think about versioning? Specify the version with a 'v' prefix 'v' tag should have the highest scope: (ex: /v1/dogs) Avoid dot notation in versions (ex: v1, v2)
  • 15. Pagination Support partial response by adding optional fields in a comma delimited list Linkedin: /people:(id,first-name,last-name,industry) Facebook: /joe.smith/friends?fields=id,name,picture Google: ?fields=title,media:group(media:thumbnail) Use limit and offset to make it easy for developers to paginate objects Facebook: offset 50 and limit 25 Linkedin: start 50 and count 25 Twitter: page 3 and rpp 25 (records per page)
  • 16. Non resource responses Use verbs not nouns for all the APIs that do not return a resource. For example: Calculate Translate Convert /convert?from=EURO&to=USD&amount=100 Separate out a section of documentation for all the non resource returning requests.
  • 17. Multiple formats Support more than one format for returning resources. Google data: ?alt=json FourSquare: /venue.json (recommended) Recommendations: Use JSON as default format Follow JavaScript conventions for naming attributes: Use medial capitalisation (aka CamelCase) Use uppercase or lowercase depending on the object.
  • 18. Tips for search Global search /search?q=foo+bar (Google approach) Scoped search /owners/5283/dogs?q=foo+bar Note: Parameter 'q' indicates a search query.
  • 19. Subdomain for APIs Consolidate all API requests in a single subdomain. api.facebook.com graph.facebook.com api.foursquare.com api.twitter.com stream.twitter.com search.twitter.com
  • 20. Authentication Recommendation: Use the latest OAuth Facebook: OAuth 2.0 Twitter: OAuth 1.0a PayPal: Permission service API
  • 21. API facade pattern (1/4) Credible, relevant and differentiated
  • 22. API facade pattern (2/4) Three steps approach: Design the ideal API Design the URLs, request parameters and responses, payloads, headers, query parameters, and so on. The API design should be self consistent Implement the design with data stubs User temporary data without connecting to the internal system. Mediate (or) integrate between the facade and the systems One big problem -> 3 smaller problems
  • 23. API facade pattern (3/4) Focus on app developer. API should be: Easy to use Self-consistent Intuitive
  • 25. References Apigee design document: http://goo.gl/5uDdn Wikipedia API: http://en.wikipedia.org/wiki/Api HTTP Status codes: http://goo.gl/muq8s