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

Apidays New York 2024 - Build a terrible API fo...

Apidays New York 2024 - Build a terrible API for people you hate by Jim Bennett, liblab

Build a terrible API for people you hate
Jim Bennett, Principal Developer Advocate at liblab

Apidays New York 2024: The API Economy in the AI Era (April 30 & May 1, 2024)

------

Check out our conferences at https://www.apidays.global/

Do you want to sponsor or talk at one of our conferences?
https://apidays.typeform.com/to/ILJeAaV8

Learn more on APIscene, the global media made by the community for the community:
https://www.apiscene.io

Explore the API ecosystem with the API Landscape:
https://apilandscape.apiscene.io/

apidays

May 14, 2024
Tweet

Video

More Decks by apidays

Other Decks in Technology

Transcript

  1. Build A Terrible API For People You Hate Jim Bennett

    Principal developer advocate liblab
  2. We need an API for a ticketing system But we

    hate the person we need to build it for…
  3. Step 1 The best API docs are… screenshots embedded in

    an Excel spreadsheet! And are wrong!
  4. Nice Jim would… provide an OpenAPI spec! Industry standard and

    can be used to generate decent documentation and client SDKs.
  5. We need lots of ticket endpoints /add_ticket - add a

    ticket /uticket - update a ticket /allTickets - get all tickets /tikcet - get one ticket. Was spelled wrong, so we can’t change it now!
  6. Nice Jim would… support request methods correctly, so we can

    have just one endpoint: /ticket /ticket/{id}
  7. To get a ticket, put the ID in the request

    body Make a request to /tikcet Pass the ticket ID in the body: { “ticket_id”: 1 }
  8. To update a ticket, put the ID in the request

    body Make a request to /uticket Pass the ticket ID in the body with the rest of the fields to update: { “ticket_id”: 1 “status”: “done” }
  9. To search for a ticket, put the search criteria in

    the request body Make a request to /search_tickets Pass the search criteria in the
body: { “status”: “open” }
  10. Step 5 Non-200 status codes are bad and make client

    code throw exceptions! Always return 200!
  11. Return 200, and put the error in the response! Status:

    200 OK { “status”: 404, “message”: “Ticket not found” }
  12. HTTP status codes mean things 1xx - hold please 2xx

    - here you go! 3xx - go away 4xx - you mucked up 5xx - I mucked up
  13. HTTP status codes mean things 200 - OK! 400 -

    bad request 401 - unauthorized 404 - not found 418 - I’m a teapot Status: 200 OK Don’t care! Always return 200!
  14. Nice Jim would… return the appropriate status code for an

    error, and include more details in an error response body.
  15. Creating a user will insert into 2 tables 2 tables

    - user and user_details Record inserted into user table Data is missing from the request, so an insert into the user_details table fails API returns an error
  16. Nice Jim would… ensure all updates are atomic - if

    the API call fails the system is in the same state as before the call.
  17. What if we don’t hate them? Provide an OpenAPI spec

    Use correct request methods Have good naming Use path parameters Return proper status codes Always do atomic updates