api.example.com Base URL / api API version / v1 Resource name / books Resource ID / 123 Rela1onship name / authors Data format . json Parameters ? foo=1&bar=abc h]ps://api.example.com/api/v1/books/123/authors.json?foo=1&bar=abc
GET Read PUT Update DELETE Delete Collec1on: /books Create a new book List books Bulk update books Delete all books Element in collec1on: /books/123 Error Show Moby-‐Dick Update Moby-‐Dick Delete Moby-‐Dick Rela1onship: /books/123/authors Add a new author to Moby-‐Dick Get all authors of Moby-‐Dick Bulk update all rela0onships Delete the rela0onship
– 200 OK: • for GET requests, the resource was successfully retrieved. • for POST requests which perform other ac0ons than crea0ng a resource, the opera0on was successful. • for PUT requests, the resource was successfully updated. • for DELETE requests, the resource was successfully deleted. – 201 Created: • POST requests which create a resource will return this when the opera0on was successful. It will also contain a `Loca0on` header providing the URL of the created resource. – 204 No Content: • PUT requests which update a resource will return this when the opera0on was successful.
– 400 Bad Request • Your request is not well-‐formed or contains errors. The response body contains details of the error. – 401 Unauthorized • You a]empt to authen0cate with an invalid API key. – 403 Forbidden • You a]empt to retrieve content you don't have access to or to perform an ac0on you're not allowed to. – 404 Not Found • You a]empt to request a resource which doesn't exist. – 500 Internal Server Error • An unhandled error occurs on the API server for some reason. – 501 Not Implemented • The ac0on you're trying to perform is not supported.
h]ps://api.example.com/api/v1/resource/id • Body – forma]ed as defined in Content-‐Type – Elements: meta + object – Collec0ons: meta + objects • totalCount, limit, offset, previous, next
errorCode: Error code • Numbering starts at 1000 => ≠ HTTP status codes – errorType: Error type (excep0on-‐like) – errorDoc: Link to the error documenta0on – developerMessage: Verbose, plain language descrip0on of the problem for the app developer with hints about how to fix it – userMessage: Message that can be passed to the app user – userDetailedMessages: Detailed info about each error
– HTTP status code is always 200 – responseCode in body (meta) • Restricted HTTP methods – ?method=put – HTTP method is always GET – /!\ post and delete = dangerous via GET requests
how developers will use your API • Be complete and RESTful, then provide shortcuts • Take advantage of par0al response syntax • Well-‐documented, with code samples • Complement with an SDK
– design the URLs, request parameters and responses, payloads, headers, query parameters, and so on – the API design should be self-‐consistent 2. Implement the design with data stubs. – allows app developers to use your API and give you feedback even before your API is connected to internal systems 3. Mediate or integrate between the façade and the systems
h]p://www.ieg.org/rfc/rfc2616.txt • List of HTTP status codes – h]p://en.wikipedia.org/wiki/List_of_HTTP_status_codes • ISO 8601 (Data elements and interchange formats) – h]p://en.wikipedia.org/wiki/ISO_8601 • ECMA-‐262 (ECMAScript scrip0ng language) – h]p://en.wikipedia.org/wiki/ECMA-‐262