Slide 1

Slide 1 text

5 Features of a Good API Architecture Rob Allen 19ft.com ~ @akrabat ~ October 2016

Slide 2

Slide 2 text

Fit for Purpose Rob Allen ~ @akrabat

Slide 3

Slide 3 text

But first… Your target audience matters! Rob Allen ~ @akrabat

Slide 4

Slide 4 text

Features of a good API Malleability Correctness Client focus Documented Secure Rob Allen ~ @akrabat

Slide 5

Slide 5 text

A good API is malleable Rob Allen ~ @akrabat

Slide 6

Slide 6 text

Malleability • The representation is independent of the DB schema Rob Allen ~ @akrabat

Slide 7

Slide 7 text

It's about focus Different objectives • API is client focussed, DB schema is application focussed • Representation of data is different • Your API becomes brittle Rob Allen ~ @akrabat

Slide 8

Slide 8 text

Malleability • The representation is independent of the DB schema • Design is based on state changes Rob Allen ~ @akrabat

Slide 9

Slide 9 text

Workflows are more than CRUD • Operations are a series of steps • Think about goals and sequences of actions • Make it easy for a user to accomplish their tasks! Rob Allen ~ @akrabat

Slide 10

Slide 10 text

Malleability • The representation is independent of the DB schema • Design is based on state changes • Hypermedia aware Rob Allen ~ @akrabat

Slide 11

Slide 11 text

Hypermedia Hypermedia as the source of client flexibility! • Rename endpoints at will • Re-home endpoints on different servers (e.g. CDN resources) • API is explorable Rob Allen ~ @akrabat

Slide 12

Slide 12 text

A good API is correct Rob Allen ~ @akrabat

Slide 13

Slide 13 text

Correct • Media type format suits the design • Correct use of HTTP verbs • Understanding of Idempotency • Richardson Maturity Model 2+ Rob Allen ~ @akrabat

Slide 14

Slide 14 text

Media types matter With application/json you abdicate responsibility. The correct media type: • Tells the client how to interpret the data • Enforces structure of the payload • Informs on what the payload data means Rob Allen ~ @akrabat

Slide 15

Slide 15 text

HTTP verbs Method Used for Idempotent? GET Retrieve data Yes PUT Change data Yes DELETE Delete data Yes POST Change data No PATCH Update data No Rob Allen ~ @akrabat

Slide 16

Slide 16 text

Richardson Maturity Model source: http://martinfowler.com/articles/richardsonMaturityModel.html Rob Allen ~ @akrabat

Slide 17

Slide 17 text

A good API respects the client dev Rob Allen ~ @akrabat

Slide 18

Slide 18 text

Great error handling • Error representations are first class citizens • Honours accept header • Correct content-type • Uses correct HTTP status code • Provides application error code & human readable message • ideally in a known media type such as api-problem • Pretty print JSON for the humans! Rob Allen ~ @akrabat

Slide 19

Slide 19 text

Error messages • End user needs a short, descriptive message • Client developer needs detailed information • Client application needs an error code • The API application needs logs! Rob Allen ~ @akrabat

Slide 20

Slide 20 text

Error codes Send correct HTTP status code • 2xx for success • 4xx for failures the client can fix • 5xx for failures the client cannot fix Include an application error code • For the client application to parse Rob Allen ~ @akrabat

Slide 21

Slide 21 text

HTTP Problem (RFC 7807) HTTP/1.1 503 Service Unavailable Content-Type: application/problem+json Content-Language: en { "status": 503, "type": "https://example.com/service-unavailable", "title": "Could not authorise user due to an internal problem - try later.", "detail": "The authentication service is down for maintenance.", "instance": "https://example.com/maintenance-schedule/2016-08-30", "error_code": "AUTHSERVICE_UNAVAILABLE" } Rob Allen ~ @akrabat

Slide 22

Slide 22 text

Handling changes • Avoid major new versions • Make changes backwards-compatible • Think about forwards-compatibility Rob Allen ~ @akrabat

Slide 23

Slide 23 text

BC changes • Add resources • New verbs to existing resources • New endpoints • New format via content negotiation • Avoid change: New query arguments / new fields Rob Allen ~ @akrabat

Slide 24

Slide 24 text

A new version is a new API • Creates URL proliferation • Doesn't matter how you define it: • api.example.com/v2/user • api2.example.com/user • Use Server header for minor and patch info Rob Allen ~ @akrabat

Slide 25

Slide 25 text

Deprecation policy • Provide plenty of warning • Remove from test servers first • Communicate widely Rob Allen ~ @akrabat

Slide 26

Slide 26 text

A good API is documented Rob Allen ~ @akrabat

Slide 27

Slide 27 text

Documentation within the API • Content-type header • Link relations • Profile links • Structured data Rob Allen ~ @akrabat

Slide 28

Slide 28 text

Profile links Profile links provide the application semantics (RFC 6906) In the header: Link: Link: ;rel="profile" Content-Type: application/hal+json;profile="https://www.example.com/docs" In the body: "_links": { "profile": { "href": "https://www.example.com/docs/" } } Rob Allen ~ @akrabat

Slide 29

Slide 29 text

Structured data There's no such thing as self-explanatory! {"christian": "Rob", "patronymic" : "Allen"} {"forename": "Rob", "surname" : "Allen"} {"firstname": "Rob", "lastname" : "Allen"} Rob Allen ~ @akrabat

Slide 30

Slide 30 text

Structured data There's no such thing as self-explanatory! {"christian": "Rob", "patronymic" : "Allen"} {"forename": "Rob", "surname" : "Allen"} {"firstname": "Rob", "lastname" : "Allen"} https://schema.org/Person: {"givenName" : "Rob", "familyName" : "Allen"} Rob Allen ~ @akrabat

Slide 31

Slide 31 text

Human documentation • Sensible URLs • Tutorials • Semantic information Rob Allen ~ @akrabat

Slide 32

Slide 32 text

Sensible URLs • The actual URL does not matter to the client API • It does matter to the client developer! GET /events GET /events?filter=upcoming GET /events/455dbab6 GET /events/455dbab6/talks GET /talks/6cf948e5 GET /talks/6cf948e5/comments Rob Allen ~ @akrabat

Slide 33

Slide 33 text

The for-human documentation • Tutorials to show how to use the API • Describe all the semantic information • Examples in multiple languages Rob Allen ~ @akrabat

Slide 34

Slide 34 text

A good API is secure Rob Allen ~ @akrabat

Slide 35

Slide 35 text

Security • SSL • Authentication • Rate limited Rob Allen ~ @akrabat

Slide 36

Slide 36 text

Authentication • HTTP Basic • OAuth2 Rob Allen ~ @akrabat

Slide 37

Slide 37 text

OAuth 2 • Requires an access token • More than one way to get a token: • Via username/password • Via API server • Tokens are short life with refresh token to get a new one Rob Allen ~ @akrabat

Slide 38

Slide 38 text

Rate limits Limit by application or user token Provide information HTTP 429 Too Many Requests X-RateLimit-Limit: 5000 X-RateLimit-Remaining: 4999 X-RateLimit-Reset: 1471549573 Rob Allen ~ @akrabat

Slide 39

Slide 39 text

To sum up Rob Allen ~ @akrabat

Slide 40

Slide 40 text

Thank you! Rob Allen ~ 19ft.com ~ @akrabat