Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How (not) to build APIs

How (not) to build APIs

Stop shooting your feet by making same old mistakes in API development. Contains fails and my advices how to avoid them.

Denis Yagofarov

September 20, 2014
Tweet

More Decks by Denis Yagofarov

Other Decks in Programming

Transcript

  1. Building app from scratch … client wants to have a

    dog adoption app … and gives us as much time as we need Credits: https://flic.kr/p/8rw6vC
  2. How do I do it now? • Design Driven Development

    • API Blueprints • Behavior and Test Driven Development • Documentation is auto-generated • … and built from tests
  3. What’s wrong? • Add a “cool” gem • Test only

    required behavior • … and frontend team can be smart in using ALL the gem’s features This is not wrong
  4. How do I do it now? • Always start with

    ‘v1’ • Never delete or change anything in production versions • Maintain “v_current_” and “v_previouos_” • While developing “v_new_” • … and have tests for both
  5. What’s wrong? • Custom algorithm to verify messages • Clients

    need to implement it on their own • The secret key can’t be revoked from single client
  6. How do I do it now? • HTTPs • oAuth

    2.0, Resource Owner Password Credentials Grant • Sending token as a header • Rails MessageVerifier for sharing “one-time” or “time-limited” links • … or just simple HTTP Authentication
  7. What was wrong? • Different fields in index and show

    • “Magic numbers” in JSON • empty (could be ‘date not in ISO8601 format’)
  8. How do I do it now? • All “Magic numbers”

    in databases are mapped to user-readable names • Use Serializers instead of templates • Use ‘partial request’ to specify the set of fields I want to get • Time is always in UTC and ISO8601 format
  9. What was wrong? • No default limit • Parser of

    parameters needs an array to find all those IDs • I used to have a count for all the results. Sometimes there were millions of results
  10. How do I do it now? • Default limit •

    If limit loo big - return 4xx error • Put filtering under a common name (q[]= or search[]=) • Have a toggle parameters for HTTP clients with quirks
  11. So… • Have API documentation (auto-generated) • Do only things

    required from you • Use versioning • Don’t re-invent authentication flows • Let JSONs be verbose and consistent • Limit data, returned to client
  12. Fortumo • Most of APIs used by us internally •

    Cloud on top of several other clouds • Many internal products, never seen by customers