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

PDF
Web Fonts in Email: How? What? Where?
PPTX
Domain Modeling & Full-Stack Web Development F#
ODP
Open Power Template 2 presentation
PPTX
Writing code samples for API/SDK documentation
PPTX
Publishing API documentation -- Workshop
PPTX
What's Coming in C# 9.0
PPTX
API Documentation -- Presentation to East Bay STC Chapter
ODP
Front End Publishing for WordPress
Web Fonts in Email: How? What? Where?
Domain Modeling & Full-Stack Web Development F#
Open Power Template 2 presentation
Writing code samples for API/SDK documentation
Publishing API documentation -- Workshop
What's Coming in C# 9.0
API Documentation -- Presentation to East Bay STC Chapter
Front End Publishing for WordPress

What's hot (19)

PPTX
API Workshop: Deep dive into REST APIs
PPTX
What's coming in c# 9.0
PPTX
API workshop: Introduction to APIs (TC Camp)
PPTX
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
PDF
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
PPT
Presentation log4 j
PPTX
API Workshop: Deep dive into code samples
PPTX
What's New in C# ? C# 6, 7.0, 7.1 and 7.2
PDF
Am4201257261
PDF
intenranional SEO Best practices
PPTX
STC Summit 2015: API Documentation, an Example-Based Approach
PPTX
API Documentation Workshop tcworld India 2015
PDF
Microservices with Swagger, Flask and Docker
PPT
CIS 451: Introduction to ASP.NET
PDF
Web programming UNIT II by Bhavsingh Maloth
PPTX
What is Responsive Web Design?
PPTX
API workshop: Deep dive into Java
PPTX
Sexy Using Cucumber - BDD in your project
PPTX
Data Applied: Developer Quicklook
API Workshop: Deep dive into REST APIs
What's coming in c# 9.0
API workshop: Introduction to APIs (TC Camp)
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Presentation log4 j
API Workshop: Deep dive into code samples
What's New in C# ? C# 6, 7.0, 7.1 and 7.2
Am4201257261
intenranional SEO Best practices
STC Summit 2015: API Documentation, an Example-Based Approach
API Documentation Workshop tcworld India 2015
Microservices with Swagger, Flask and Docker
CIS 451: Introduction to ASP.NET
Web programming UNIT II by Bhavsingh Maloth
What is Responsive Web Design?
API workshop: Deep dive into Java
Sexy Using Cucumber - BDD in your project
Data Applied: Developer Quicklook
Ad

Viewers also liked (18)

PPTX
Javascript: The good parts for humans (part 1)
PDF
Example Acrolinx Language Analysis
PPTX
Vocabulary - Week 1
PPTX
ROC History
PDF
NaveenParameshwaraiah_June2015
PPT
Ismar 2010 Presentation
PDF
Juan Domingo Perón carta al presidente kennedy
PPTX
Tuberculin n scalp vein needle
PPTX
Reinventing strategies for emerging markets
PDF
Kate Ryan, "Figure in the Sky," 2015
PDF
Architect Mohamed samir portfolio
PPT
Naufalcolin adeline blood_bloodvessel
PDF
Ahmed obaid
PPTX
Ilustración
PDF
How Dangerous Are Motorcycles, Really?
PPTX
Blogs: Reading and Writing (no Arithmetic!)
PPT
PPTX
Um readr circl
Javascript: The good parts for humans (part 1)
Example Acrolinx Language Analysis
Vocabulary - Week 1
ROC History
NaveenParameshwaraiah_June2015
Ismar 2010 Presentation
Juan Domingo Perón carta al presidente kennedy
Tuberculin n scalp vein needle
Reinventing strategies for emerging markets
Kate Ryan, "Figure in the Sky," 2015
Architect Mohamed samir portfolio
Naufalcolin adeline blood_bloodvessel
Ahmed obaid
Ilustración
How Dangerous Are Motorcycles, Really?
Blogs: Reading and Writing (no Arithmetic!)
Um readr circl
Ad

Similar to Web REST APIs Design Principles (20)

PPTX
Restful webservices
PDF
Crafting APIs
PDF
API presentation
PDF
Effective API Documentation Strategies (With an API Documentation Example
PPTX
flask.pptx
ODP
Designing Good API & Its Importance
PDF
Web API Design
PDF
Web API Design: Crafting Interfaces that Developers Love
PDF
Navigating API Documentation: Tips from the Trenches
PPTX
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
PPTX
REST-API introduction for developers
PDF
Cqrs api
PDF
The ultimate api checklist by Blendr.io
PPTX
Introduction to Google App Engine with Python
PPT
APIs And SDKs Breaking Into And Succeeding In A Specialty Market
PPTX
API Design- Best Practices
PDF
A RESTful introduction
PDF
IRJET- Mail Server Communication:PHP
PPT
Benefits of the CodeIgniter Framework
PDF
Best practices and advantages of REST APIs
Restful webservices
Crafting APIs
API presentation
Effective API Documentation Strategies (With an API Documentation Example
flask.pptx
Designing Good API & Its Importance
Web API Design
Web API Design: Crafting Interfaces that Developers Love
Navigating API Documentation: Tips from the Trenches
gRPC, GraphQL, REST - Which API Tech to use - API Conference Berlin oct 20
REST-API introduction for developers
Cqrs api
The ultimate api checklist by Blendr.io
Introduction to Google App Engine with Python
APIs And SDKs Breaking Into And Succeeding In A Specialty Market
API Design- Best Practices
A RESTful introduction
IRJET- Mail Server Communication:PHP
Benefits of the CodeIgniter Framework
Best practices and advantages of REST APIs

More from Anji Beeravalli (7)

PPTX
Javascript: The good parts for humans (part 5)
PPTX
Javascript: The good parts for humans (part 6)
PPTX
Javascript: The good parts for humans (part 2)
PPTX
Javascript: The good parts for humans (part 3)
PPTX
Javascript: The good parts for humans (part 4)
PDF
CSS for developers
PDF
Business Modelling
Javascript: The good parts for humans (part 5)
Javascript: The good parts for humans (part 6)
Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 3)
Javascript: The good parts for humans (part 4)
CSS for developers
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