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

SkiPHP 2014 - Your API Sucks

SkiPHP 2014 - Your API Sucks

Do you hate the Facebook API? We all do as well. So stop writing your API in their footsteps.

In this talk we will walk through how to construct a RESTful API, what makes an API your users/developers will love, and why you should eat your own dog food with API Driven Development.

Ben Edmunds

January 14, 2014
Tweet

More Decks by Ben Edmunds

Other Decks in Programming

Transcript

  1. Who Am I Ben Edmunds @benedmunds http://benedmunds.com Build and lead

    development teams >10 years of experience in the industry Specialize in PHP development !
  2. Who Am I Personal: From Alabama (hence the accent) Living

    in Portland, OR ! Tech Focus: PHP (CodeIgniter, Laravel, Slim) Javascript (Backbone, Angular, Node.js, Express, Phonegap) Databases (MySQL, PostGreSQL) Ben Edmunds @benedmunds http://benedmunds.com
  3. What will we cover? RESTful API Basics ! API Design

    ! Implementing your API ! Versioning, Auth, & Rate Limiting !
  4. What is REST? Representational State Transfer ! HTTP Methods GET,

    POST, PUT, DELETE ! Expressive ! Real-World Usage
  5. What is REST? Most widely used data format is JSON

    ! Javascript Object Notation { key: ‘value’, anotherKey: ‘value2’ }
  6. What is REST? JSON usage in Javascript = familiar var

    object = { key: ‘value’, anotherKey: ‘value2’ };
  7. What is REST? Outputs {key: ‘value’} PHP echo json_encode(array( ‘key’

    => ‘value’ )); JSON encoding is supported natively in most languages
  8. What is REST? Outputs array( ‘key’ => ‘value’ ) $json_data

    = “{key: ‘value’}”; print_r(json_decode($json_data)); JSON decoding is just as simple PHP
  9. API Design Different Opinions ! PUT = create (idempotent) POST

    = create (unknown resource) ! PUT = update (resource is known) Put vs Post
  10. API Design A single Object maps to a singular URL

    api.domain.com/user/2 api.domain.com/company/3
  11. API Design Common actions across most objects GET /user/2 GET

    /company/3 ! DELETE /user/2 DELETE /company/3
  12. API Design https://site.com/api/statuses ! Maps to all of the statuses

    ! “Statuses” could be: SQL table NoSQL collection Aggregate Data
  13. API Design Creating ! If you know all of the

    data (including the ID) ! PUT https://site.com/api/status/1234
  14. API Design Creating ! If you know all of the

    data (including the ID) ! PUT https://site.com/api/status/1234 No matter how many times you send this data only one resource should ever be created Idempotent
  15. API Design Creating ! If you know all of the

    data (including the ID) ! PUT https://site.com/api/status/1234 { id: 1234, retweeted: false, active: true }
  16. Implementation Creating ! If you don’t know all of the

    data ! POST https://site.com/api/status
  17. Implementation Creating Each time you post this data a new

    resource should be created Unique ! If you don’t know all of the data ! POST https://site.com/api/status
  18. Implementation Creating ! If you don’t know all of the

    data ! POST https://site.com/api/status {user_id: 1, text: ‘Test Status’ }
  19. Implementation Creating ! If you don’t know all of the

    data ! POST https://site.com/api/status {user_id: 1, text: ‘Test Status’ } Response {succes: true, id: 123 }
  20. API Design Reading Get all of the statuses that have

    been retweeted and are active GET https://site.com/api/statuses ? retweeted=1&active=1
  21. Implementation Updating ! We know all the data ! PUT

    https://site.com/api/status/123 {id: 123, user_id: 1, text: ‘Test Status 2’ }
  22. API Design Relationships Get the user that posted a status

    GET https://site.com/api/status/1234/user
  23. Versioning How to handle versioning? Route newest API through https://api.site.com

    GET https://site.com/api/v2/statuses GET https://site.com/api/statuses =
  24. Resources https://leanpub.com/build-apis-you-wont-hate Book - Build APIs You Won’t Hate Tutorial

    - Demystifying REST https://tutsplus.com/tutorial/demystifying-rest/