Apidays New York 2024 - Build a terrible API for people you hate by Jim Bennett, liblab
APIdays_official
75 views
31 slides
May 23, 2024
Slide 1 of 31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
About This Presentation
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?...
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/
Size: 13.24 MB
Language: en
Added: May 23, 2024
Slides: 31 pages
Slide Content
Build A Terrible
API For People
You Hate
Jim Bennett
Principal developer advocate
liblab
Who has worked
with someone
they hated?
Is it time for malicious
compliance?
We need an API
for a ticketing
system
But we hate the person we
need to build it for…
We could be nice,
Let’s build a terrible API!
but let’s be mean
instead…
6 steps to build a
terrible API for
people you hate
The best docs are…
Step 1
The best API docs are…screenshots embedded in
an Excel spreadsheet!
And are wrong!
Nice Jim would…
provide an OpenAPI spec!
Industry standard and can
be used to generate decent
documentation and client
SDKs.
Step 2
GET all the things!
How do we create
a new user?
We GET a new user
obviously!
HTTP request methods
GET
POST
PUT
PATCH
DELETE
POST
PUT
PATCH
DELETE
Nice Jim would…
support request methods
correctly, using POST to
create a new user.
Step 3
Naming things is hard so
let’s not bother doing it well.
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!
Nice Jim would…
support request methods
correctly, so we can have
just one endpoint:
/ticket
/ticket/{id}
Step 4
Request bodies are better
than path or query
parameters.
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
}
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”
}
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”
}
Nice Jim would…
use path parameters and
query parameters instead of
sending identifiers in a
request body
Step 5
Non-200 status codes are
bad and make client code
throw exceptions!
Always return 200!
Return 200, and put the
error in the response!
Status: 200 OK
{
“status”: 404,
“message”: “Ticket not found”
}
HTTP status codes mean
things
1xx - hold please
2xx - here you go!
3xx - go away
4xx - you mucked up
5xx - I mucked up
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!
Nice Jim would…
return the appropriate
status code for an error, and
include more details in an
error response body.
Step 6
Atomic updates? Nah!
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
Creating a user will insert
into 2 tables
User retries - duplicate in the
user table
Nice Jim would…
ensure all updates are
atomic - if the API call fails
the system is in the same
state as before the call.
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
Hi, I’m Jim
He/Him
Principal developer advocate
liblab
@jimbobbennett
linktr.ee/jimbobbennett