SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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 APIsTom 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.0Moaid 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 MALOTHBhavsingh 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 samplesTom 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.2David 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 ApproachLois Patterson
 
API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015Tom Johnson
 
Microservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerMicroservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerDhilipsiva DS
 
CIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NETCIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NETwebhostingguy
 
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 MalothBhavsingh 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 JavaTom 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 projectb4usolution .
 
Data Applied: Developer Quicklook
Data Applied: Developer QuicklookData Applied: Developer Quicklook
Data Applied: Developer QuicklookDataminingTools Inc
 
Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019
Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019
Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019Jose Luis Bonilla Sánchez
 

Tendances (20)

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
 
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
 
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)
 
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...
 
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
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
 
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
 
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
 
Am4201257261
Am4201257261Am4201257261
Am4201257261
 
intenranional SEO Best practices
intenranional SEO Best practices intenranional SEO Best practices
intenranional SEO Best practices
 
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
 
API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015
 
Microservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerMicroservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and Docker
 
CIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NETCIS 451: Introduction to ASP.NET
CIS 451: Introduction to ASP.NET
 
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
 
What is Responsive Web Design?
What is Responsive Web Design?What is Responsive Web Design?
What is Responsive Web Design?
 
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into Java
 
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
 
Data Applied: Developer Quicklook
Data Applied: Developer QuicklookData Applied: Developer Quicklook
Data Applied: Developer Quicklook
 
Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019
Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019
Building and Implementing MT systems @ eBay – TAUS Global Content Summit 2019
 

En vedette

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 1lressler
 
Ismar 2010 Presentation
Ismar 2010 PresentationIsmar 2010 Presentation
Ismar 2010 PresentationJulian 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 kennedyIntegridad Política
 
Tuberculin n scalp vein needle
Tuberculin n scalp vein needleTuberculin n scalp vein needle
Tuberculin n scalp vein needlePratik Kumar
 
Reinventing strategies for emerging markets
Reinventing strategies for emerging marketsReinventing strategies for emerging markets
Reinventing strategies for emerging marketsBill 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," 2015Gabriel Hopson
 
Architect Mohamed samir portfolio
Architect Mohamed samir portfolioArchitect Mohamed samir portfolio
Architect Mohamed samir portfoliomohamed samir
 
Naufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvesselNaufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvesselNaufal Hakim
 
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
 

En vedette (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)
 
Example Acrolinx Language Analysis
Example Acrolinx Language AnalysisExample Acrolinx Language Analysis
Example Acrolinx Language Analysis
 
Vocabulary - Week 1
Vocabulary - Week 1Vocabulary - Week 1
Vocabulary - Week 1
 
ROC History
ROC HistoryROC History
ROC History
 
NaveenParameshwaraiah_June2015
NaveenParameshwaraiah_June2015NaveenParameshwaraiah_June2015
NaveenParameshwaraiah_June2015
 
Ismar 2010 Presentation
Ismar 2010 PresentationIsmar 2010 Presentation
Ismar 2010 Presentation
 
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
 
Tuberculin n scalp vein needle
Tuberculin n scalp vein needleTuberculin n scalp vein needle
Tuberculin n scalp vein needle
 
Reinventing strategies for emerging markets
Reinventing strategies for emerging marketsReinventing strategies for emerging markets
Reinventing strategies for emerging markets
 
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
 
Architect Mohamed samir portfolio
Architect Mohamed samir portfolioArchitect Mohamed samir portfolio
Architect Mohamed samir portfolio
 
Naufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvesselNaufalcolin adeline blood_bloodvessel
Naufalcolin adeline blood_bloodvessel
 
Ahmed obaid
Ahmed obaidAhmed obaid
Ahmed obaid
 
Ilustración
IlustraciónIlustración
Ilustración
 
How Dangerous Are Motorcycles, Really?
How Dangerous Are Motorcycles, Really?How Dangerous Are Motorcycles, Really?
How Dangerous Are Motorcycles, Really?
 
Blogs: Reading and Writing (no Arithmetic!)
Blogs: Reading and Writing (no Arithmetic!)Blogs: Reading and Writing (no Arithmetic!)
Blogs: Reading and Writing (no Arithmetic!)
 
Drugs
DrugsDrugs
Drugs
 
Um readr circl
Um readr circlUm readr circl
Um readr circl
 

Similaire à Web REST APIs Design Principles

Designing Good API & Its Importance
Designing Good API & Its ImportanceDesigning Good API & Its Importance
Designing Good API & Its ImportanceImran 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 LoveJamison 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 20Phil Wilkins
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developersPatrick 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.ioBlendr.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 PythonBrian 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 MarketBill Dubie
 
A RESTful introduction
A RESTful introductionA RESTful introduction
A RESTful introductionDaniel Toader
 
IRJET- Mail Server Communication:PHP
IRJET-  	  Mail Server Communication:PHPIRJET-  	  Mail Server Communication:PHP
IRJET- Mail Server Communication:PHPIRJET Journal
 
Benefits of the CodeIgniter Framework
Benefits of the CodeIgniter FrameworkBenefits of the CodeIgniter Framework
Benefits of the CodeIgniter FrameworkToby 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 APIsAparna Sharma
 
Restful api design
Restful api designRestful api design
Restful api designMizan Riqzia
 

Similaire à Web REST APIs Design Principles (20)

Restful webservices
Restful webservicesRestful webservices
Restful webservices
 
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
 
API presentation
API presentationAPI presentation
API presentation
 
flask.pptx
flask.pptxflask.pptx
flask.pptx
 
Designing Good API & Its Importance
Designing Good API & Its ImportanceDesigning Good API & Its Importance
Designing Good API & Its Importance
 
Web API Design
Web API DesignWeb API Design
Web API Design
 
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
 
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
 
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
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
Cqrs api
Cqrs apiCqrs api
Cqrs api
 
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
 
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
 
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
 
API Design- Best Practices
API Design-   Best PracticesAPI Design-   Best Practices
API Design- Best Practices
 
A RESTful introduction
A RESTful introductionA RESTful introduction
A RESTful introduction
 
IRJET- Mail Server Communication:PHP
IRJET-  	  Mail Server Communication:PHPIRJET-  	  Mail Server Communication:PHP
IRJET- Mail Server Communication:PHP
 
Benefits of the CodeIgniter Framework
Benefits of the CodeIgniter FrameworkBenefits of the CodeIgniter Framework
Benefits of the CodeIgniter Framework
 
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
 
Restful api design
Restful api designRestful api design
Restful api design
 

Plus de 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
 

Plus de 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)
 
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)
 
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)
 
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)
 
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)
 
CSS for developers
CSS for developersCSS for developers
CSS for developers
 
Business Modelling
Business ModellingBusiness Modelling
Business Modelling
 

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