Topics and tips in Web API design

twitter
@walac
email
wcosta@mozilla.com
github
https://github.com/walac
Slides
https://walac.github.io/api-design-talk

Topics and tips in Web API design

High-five at the Mozilla Festival!

Wander Lairson Costa

Software Engineer, Mozilla, Taskcluster Team

twitter
@walac
email
wcosta@mozilla.com
github
https://github.com/walac
Slides
https://walac.github.io/api-design-talk
We are hiring! https://careers.mozilla.org

RESTful 101

RESTful concepts

{URI}/{RESOURCE/ENDPOINT}

  • https://example.com/items
  • https://example.com/items/10
  • https://example.com/items/sorted
  • https://example.com/items?sorted=true
  • Idempotence
  • HTTP verbs
  • Errors code

HTTP verbs: GET

A GET request is an idempotent operation to retrieve data from the resource.

Give me the data

HTTP Verbs: POST

A POST request adds new data to the resource.

Add data

HTTP Verbs: PATCH

PATCH verb requests data update on the resource. PATCH is idempotent.

Changed my mind

HTTP Verbs: PUT

PUT = PATCH || POST.

Choose wisely

HTTP Verbs: DELETE

It does what it says: remove a data from the resource.

Destroy everything

HTTP Error Codes: 2xx, Success

Success kid

HTTP Error Codes: 3xx, Redirection

You came to the wrong neighborhood, motherfucker.

HTTP Error Codes: 4xx, Client error

Client error

HTTP Error Codes: 5xx, Internal Error

Internal error

Browsers same origin policy

Same origin policy

Same origin policy http://tinyurl.com/j8yr6sq

Be aware of CORS

CORS http://tinyurl.com/z6s7pm9

Tips on CORS implementations

  • Some requests require web server to response to a preflight (OPTION) request.
  • Web server can control allowed domains, request and exposed headers.
  • If your API needs a session key, make sure the web app stores it in a [local|session]Storage.
  • Cors proxy

General Design guidelines

Powered by JSON schemas

XML
  • Request parameters (except GET)
  • Response
  • Data validation
  • Automated documentation
  • Client bindings

Choose your programming language wisely

  • Dynamic vs static type
  • Return codes vs exception handling
  • Functional vs Procedural vs Object oriented

Write great documentation

  • Reference docs
  • Tutorials
  • Code samples
  • Screencasts
  • Mailing lists
  • Open the source, Luke

Thanks

Red panda (Firefox) Photo by Yortw