api design guidelines
play

API Design Guidelines Mike Kistler & Dan Hudlow IBM Cloud - PowerPoint PPT Presentation

API Design Guidelines Mike Kistler & Dan Hudlow IBM Cloud Developer Experience Why you need API Design Guidelines Consistency in your API design will benefit your users will benefit your service/library/tool developers 2


  1. API Design Guidelines Mike Kistler & Dan Hudlow IBM Cloud Developer Experience

  2. Why you need API Design Guidelines • Consistency in your API design • will benefit your users • will benefit your service/library/tool developers � 2

  3. Guiding Principles • Usefulness • Adherence to HTTP semantics • Ease of use and low barrier to entry • Defensiveness/Compatibility • Security • Longevity � 3

  4. References • Microsoft API Guidelines https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md • Google API Guidelines https://cloud.google.com/apis/design/ • API Stylebook http://apistylebook.com/design/guidelines/ � 4

  5. Design First • User stories • ERDs • and UML � 5

  6. Specification Format • OpenAPI http://spec.openapis.org/oas/v3.0.2 or https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md • Linux Foundation project (Open) • Specifically OpenAPI v3.x • published July 2017 � 6

  7. Resource-oriented API design • API consists of nouns — resources — and verbs — operations • Final static segment in API path is the resource name • Always plural • Resource instances have a unique ID • Resource has a well defined schema of its contents � 7

  8. Example Resource /authors/18345 { “id”: 18345, “first_name”: “Scott”, “last_name”: “Thompson”, “city”: “Dallas”, “region”: “Texas”, “country”: “United States”, “tags”: [“Objective-C”, “Swift”, “Ruby”], “href”: “https://api.hudlow.org/authors/18345” } � 8

  9. Resource Format • JSON • JSON is unordered • JSON names are case-sensitive • But not any JSON • Use well-defined types • Don’t mix types within an array • Beware of "null" • Create clear guidelines about what is/is not allowed � 9

  10. Naming Conventions • snake_case • camelCase • UpperCamelCase • Kebab-case � 10

  11. Naming Conventions • snake_case • Our choice for parameter and property names • camelCase • Our choice for operation ids • UpperCamelCase • Our choice for schema names • kebab-case � 11

  12. Operations Create Update C R U D Read Delete � 12

  13. Operations Create with POST Update with PATCH C R U D Read with GET Delete with DELETE � 13

  14. Operations Create and Update with POST C R U D Read with GET Delete with DELETE � 14

  15. HTTP Methods � 15

  16. GET & HEAD • Safe • Idempotent • Ignore request bodies � 16

  17. POST • Unsafe • Non-idempotent • Used for creation of subordinate resources: POST /books → /books/38573 • May also be used for modifying an existing resource: POST /books/38573 � 17

  18. PATCH • Unsafe • Non-idempotent • Modify an existing resource � 18

  19. PUT • Unsafe • Idempotent • Used for creating or replacing a resource at a known URL � 19

  20. Standard Error Model 400 Bad Request { “code”: “missing_field”, “message”: “The `first_name` field is needed to create an author.”, “target”: { “type”: “field”, “name”: “first_name” } } � 20

  21. Programmatic Information in Errors 400 Bad Request { “code”: “missing_field”, “message”: “The `first_name` field is needed to create an author.”, “target”: { “type”: “field”, “name”: “first_name” } } � 21

  22. Collections GET /authors { “authors”: [ { “id”: 18345, “first_name”: “Scott”, “last_name”: “Thompson”, “href”: “https://api.hudlow.org/authors/18345” }, { “id”: 63840, “first_name”: “David”, “last_name”: “Gelphman”, “href”: “https://api.hudlow.org/authors/63840” } ] } � 22

  23. Offset & Limit Pagination GET /authors? offset=4 & limit=2 { “total_count”: 12 “authors”: [ { “id”: 18345, “first_name”: “Scott”, “last_name”: “Thompson”, “href”: “https://api.hudlow.org/authors/18345” }, { “id”: 63840, “first_name”: “David”, “last_name”: “Gelphman”, “href”: “https://api.hudlow.org/authors/63840” } ] } � 23

  24. Token-based Pagination GET /authors { “total_count”: 12 “next”: { “token”: “d537748fe4” }, “authors”: [ { “id”: 18345, “first_name”: “Scott”, “last_name”: “Thompson”, “href”: “https://api.hudlow.org/authors/18345” }, { “id”: 63840, “first_name”: “David”, “last_name”: “Gelphman”, “href”: “https://api.hudlow.org/authors/63840” } ] } � 24

  25. Pagination • Offset and limit pagination • Stateless • Imprecise • Token-based pagination • Robust • More difficult and demanding to implement • Stateful vs stateless considerations 
 � 25

  26. Versioning � 26

  27. Compatibility • Usually compatible • Adding new fields to models • Adding new types of resources at new URLs • Usually incompatible • Removing resource types • Removing fields from resources • Adding new required fields to templates • Making previously valid field values invalid � 27

  28. Specifying Versions • Custom header API-Version: 3 • Query parameter /books?api_version=3 • Content type Accept: application/vnd.github.v3+json • Root path /v3/books � 28

  29. Go Forth • Define your principles and priorities • Create purposeful guidelines • Write them down • Start designing better web APIs � 29

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend