On Github karolikl / How-to-create-a-web-api-no-one-wants-to-use
By Karoline Klever / @karolikl
at the world of Web APIs...
Start out by looking at how the world of Web APIs has evolved, and where we are at today.is increasing fast
Badly named and inconsistent URIs that violate the Principle of Least Astonishment will make your web API just as disappointing as the movie "Sharknado".
We cannot guess the URI of an endpoint based on our experience with the other endpoints.
/all_links_for_city_of/chicago/il.json
/all_links_for_city_of/chicago/il.json
/all_links_for_city_of/chicago/il.json
/all_links_for_city_of/chicago/il.json
/all_links_for_county_of/cook%20county/il.json
/primary_links_for_city_of/chicago/il.json
/primary_links_for_county_of/cook%20county/il.json
Duplicating endpoints. Unless you’re using URL templating, this will be a problem./links
/links/il
/links/il/cook%20county
/links/il/cook%20county/chicago
/links/il/cook%20county/chicago?type=primary
Possible solution, assuming you’re not building your API using hypermedia. If you are building your API using hypermedia, that’s awesome! But it’s also a complete different talk than the one I’m doing right now.Why do we need anything other than GET?
There's an old story called "The Spider of Doom" where a company developing a content management system suddenly found that their client had "magically" lost all the pages of their website. After a lot of searching around, they found that the Googlebot was able to bypass their authentication mechanisms and had therefore crawled all their "Delete page" links. This, of course, resulted in all the pages of the website being deleted.GET
PUT
POST
DELETE
GET: Yes
PUT: No
POST: No
DELETE: No
Methods we can issue the same request against as many times as we want without changing the result.
GET: Yes
PUT: Yes
POST: No
DELETE: Yes
GET /vehicles/{id}/command/climate_state
{ "inside_temp": 17.0, "outside_temp": 9.5, "driver_temp_setting": 22.6, "passenger_temp_setting": 22.6, "is_auto_conditioning_on": false, "is_front_defroster_on": null, "is_rear_defroster_on": false, "fan_status": 0 }
GET /vehicles/{id}/command/sun_roof_control?state=open
GET /vehicles/{id}/command/sun_roof_control
{ "state": "open" }
PUT /vehicles/{id}/command/sun_roof_control?state=open
{ "result": true }
Section 9 «Method definitions» in RFC 2616
https://tools.ietf.org/html/rfc2616#section-9Everyone just loves an "error in disguise".
Invalid requests return with a 200 OK status code, giving the impression that the request was successful.
Including a detailed error report or stack trace makes your Web API vulnerable to attack
GET http://api.worldbank.org/countries/a?format=json
404 Not Found
GET http://api.worldbank.org/topic?format:json
400 Bad Request
Section 10 «Status Code Definitions» in RFC 2616
https://tools.ietf.org/html/rfc2616#section-10Dictating the format will drive developers away.
Hurray!
However perfect you make your web API, you're bound to find a way that's even more perfect before long, and then you'll need to launch a new version.
Capture5 -> Complete / Cancel2
GET https://api.twitter.com/1.1/trends/place.json?id=1
GET http://api-public.netflix.com/catalog/titles/series/7002352?v=1.5
GET https://api.foursquare.com/v2/venues/40a55d80f964a52020f31ee3?oauth_token=XXX&v=YYYYMMDD
HEADER Accept: {type}/vnd.{company}.{version}+{type}
HEADER Accept: {type}/vnd.{company}+{type}; version={version}
HEADER Accept: application/vnd.github.v3+json
HEADER Accept: application/vnd.github+json; version=3
HEADER x-ms-version: 2014-02-14
HEADER api-version: 2Were deprecated in RFC 6648 as most downstream cache servers don’t care about custom headers. https://tools.ietf.org/html/rfc6648
URI design
HTTP Verbs
HTTP Status Codes
Result Formatting
Versioning
Thank you!
By Karoline Klever / @karolikl / karolikl@gmail.com