How to design RESTful APIs

This is course note for the following online course: "Designing RESTful APIs" on LinkedIn by Keith Casey

https://www.linkedin.com/learning/designing-restful-apis/welcome?u=3322

Course completed on Apr 11, 2020

Why good API design important?

Think of 3 aspects, they have to aligned:

How to we add an API?

Bolt-on StrategyFacade StrategyGreenfield Strategy
ProBrute-force approachReplacing system piece by piece / keep working system functioningAPI first or mobile first mindset. New technologies and architectures
ConLeaking internal designMultiple mindsets / hard to replicate behavior 1:1Massive upfront investment before benefit from it

How to modeling API?

3 rules

Conclusion: doc everything

Modeling process

Understand the business process

1. Identify users

2. Identify activities

Example: Order a food

  1. place an order
  2. wait for the order
  3. receive the order

3. Break into atomic functions

Above is from user perspective, complete picture is:

  1. customer search for a food and decide
  2. customer place an order with the cashier
  3. cashier passes the order to chief
  4. chief ACK and tell cashier it's ok to cook
  5. cashier calculate the bill
  6. user make payment, accepted/rejected
  7. if accepted, chief cook the food
  8. deliver the food to customer

Example 2: online book order

  1. view items
  2. add item to cart
  3. remove items
  4. checkout
  5. fullfil and ship order
  6. view order

Validate the API to avoid ambiguities

4. Create API definitions

Now we can decide the definitions of the steps in example 2:

  1. View items
    • GET: List items
    • GET: View item
  2. Add item to cart
    • POST: Create new cart
  3. Remove items
    • PUT: update existing cart
  4. Checkout
    • POST: other resources
  5. Fullfil and ship order
    • Create order is done in POST checkout
    • GET: View order
    • POST: cancel order
  6. Customer Resource
    • Customer registration: POST
    • View customer: GET

Types of relationships between resources:

5. Validate API

  1. card of scenarios
  2. use microframework

HTTP

REST APIs: 6 constraints

  1. Client-Server Architecture
  2. Stateless Architecture
  3. Cacheable
  4. Layered Systems
  5. Code on Demand
  6. Uniform Interfaces
    • ID of resources
    • Manipulate resources
    • Self-descriptive messages
    • Can discover links

API Design patterns

AuthN is who you are. AuthZ is what you can do.

3 ways to implement them

  1. API Keys: Put in header, url or body
    • Pro: Easiest way
    • Con: leak, not easy to update
  2. Implement your own AuthN/AuthZ protocol
    • Pro: Only you know
    • Con: Untested, unproven; Training cost; No supporting tool
  3. OAuth 2.0
    • Pro: Reliable and well established; Massive ecosystem; Open-source and commercial options
    • Con: Complicated; Learning curve steep

API Versioning

2 ways to versioning:

  1. via URL
  2. via Accept Header

Payload

Hypermedia

Hyper: the media is not linear, it can beginning in the middle

Advanced HTTP headers: Content negotiation and caching

Documentations

Jekyll/Slate

SDK

Make users' lives easier. Ask existing user's demand.

Final

Conference:

Book: