Slide 1

Slide 1 text

Designing APIs for Humans Kyle Fuller

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Focussed on Collaboration

Slide 5

Slide 5 text

# GET /message + Response 200 (text/plain) Hello World!

Slide 6

Slide 6 text

Think Before you Code

Slide 7

Slide 7 text

Your API

Slide 8

Slide 8 text

API Facade

Slide 9

Slide 9 text

"The best API designs tends to be those that are understood by everybody involved in the API- lifecycle"

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Designing our first API

Slide 12

Slide 12 text

Cluedo

Slide 13

Slide 13 text

# Cluedo

Slide 14

Slide 14 text

# Cluedo The classic game of whodunit! To solve the mystery, you must find out who committed the crime, what was the murder weapon used, and in which room the crime was committed.

Slide 15

Slide 15 text

## The Game You will be dealt some cards. You can immediately eliminate these characters, rooms and weapons from your investigation. During the game, move from room to room to make your enquiries. Once inside a room, make a "suggestion" on your turn by calling a character and a weapon into the room. ...

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

## Game

Slide 19

Slide 19 text

## Game ### Begin play

Slide 20

Slide 20 text

## Game ### Begin play A player can begin the game as soon as there are at least two players. No new players can join the game once it is started.

Slide 21

Slide 21 text

## Game [/game/{id}] ### Begin play A player can begin the game as soon as there are at least two players. No new players can join the game once it is started.

Slide 22

Slide 22 text

## Game [/game/{id}] + Parameters + id - Unique identifier for a game

Slide 23

Slide 23 text

### Begin play [PUT] A player can begin the game as soon as there are at least two players. No new players can join the game once it is started.

Slide 24

Slide 24 text

### Begin play [PUT] A player can begin the game as soon as there are at least two players. No new players can join the game once it is started. + Response 204

Slide 25

Slide 25 text

Resource: Game Action: Begin play • Example HTTP Response

Slide 26

Slide 26 text

Human Friendly

Slide 27

Slide 27 text

Machine Readable

Slide 28

Slide 28 text

$ drafter -f json cluedo.apib

Slide 29

Slide 29 text

{ "element": "transition", "meta": { "title": "Begin play" }, "content": [ { "element": "copy", "content": "A player can begin the game as soon as there are at least two players. No new players can join the game once it is started." } ] }

Slide 30

Slide 30 text

Prototyping

Slide 31

Slide 31 text

Mock-server

Slide 32

Slide 32 text

$ curl -I -X PUT http://private-ceaf6d-cluedo.apiary-mock.com/game/c9d60ae4 HTTP/1.1 204 No Content Server: Cowboy Connection: keep-alive X-Apiary-Ratelimit-Limit: 120 X-Apiary-Ratelimit-Remaining: 116 Access-Control-Allow-Origin: * Access-Control-Allow-Methods: OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT Access-Control-Max-Age: 10 X-Apiary-Transaction-Id: 56d84b8058d1380b00f6593f Content-Length: 0 Date: Thu, 03 Mar 2016 14:34:40 GMT Via: 1.1 vegur

Slide 33

Slide 33 text

$ curl http://private-ceaf6d-cluedo.apiary-mock.com/game/c9d60ae4 { "players": [ { "name": "scarlet", "position": { "x": 12, "y": 6, "room": "ballroom" } } ], "status": "unstarted" }

Slide 34

Slide 34 text

Traffic Inspector

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Implementation

Slide 39

Slide 39 text

404 "Not Found"

Slide 40

Slide 40 text

How do you test your API documentation?

Slide 41

Slide 41 text

You Don't.

Slide 42

Slide 42 text

Dredd https://github.com/apiaryio/dredd

Slide 43

Slide 43 text

$ dredd cluedo.apib https://localhost:8080

Slide 44

Slide 44 text

$ dredd cluedo.apib https://localhost:8080 info: Beginning Dredd testing... pass: GET /game/c9d60ae4 duration: 10ms pass: PUT /game/c9d60ae4 duration: 8ms complete: 2 passing, 0 failing, 0 errors, 0 skipped, 2 total complete: Tests took 18ms

Slide 45

Slide 45 text

$ dredd cluedo.apib https://localhost:8080 info: Beginning Dredd testing... pass: GET /game/c9d60ae4 duration: 10ms fail: PUT /game/c9d60ae4 duration: 8ms fail: PUT /game/c9d60ae4 duration: 10ms fail: statusCode: Status code is not '204' expected: statusCode: 204 actual: statusCode: 201 body: { "status": "started" } complete: 1 passing, 1 failing, 0 errors, 0 skipped, 2 total complete: Tests took 18ms

Slide 46

Slide 46 text

Creating a Game

Slide 47

Slide 47 text

### Create a game [POST /new]

Slide 48

Slide 48 text

### Create a game [POST /new] + Response 201 (application/json)

Slide 49

Slide 49 text

### Create a game [POST /new] + Response 201 (application/json) { "person": { "name": "Scarlet" }, "game": { "id": "abc", "players": [], "status": "unstarted" } }

Slide 50

Slide 50 text

Data Structures

Slide 51

Slide 51 text

## Data Structures ### Person (object) + name: Scarlet

Slide 52

Slide 52 text

## Game [/game/{id}]

Slide 53

Slide 53 text

## Game [/game/{id}] + Attributes + id: c9d60ae4 - Unique identifier for this game

Slide 54

Slide 54 text

## Game [/game/{id}] + Attributes + id: c9d60ae4 - Unique identifier for this game + players (array[Player]) - Players currently in the game

Slide 55

Slide 55 text

## Game [/game/{id}] + Attributes + id: c9d60ae4 - Unique identifier for this game + players (array[Player]) - Players currently in the game + status (enum) - Current state of the game + Members + unstarted - Waiting for players to join + starting - Waiting for a player to start + running - Game in progress + ended - Game over

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

### Create a game [POST /new] + Response 201 (application/json) + Attributes + person (Person) + game (Game)

Slide 60

Slide 60 text

JSON { "player": { "name": "Scarlet" }, "game": { "id": "c9d60ae", "players": [ { "name": "Scarlet" } ], "status": "unstarted" } }

Slide 61

Slide 61 text

JSON Schema { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "game": { "type": "object", "properties": { ... "status": { "type": "string", "enum": [ "unstarted", "starting", "running", "ended" ], "description": "Current state of the game" } } } } }

Slide 62

Slide 62 text

Joining a Game

Slide 63

Slide 63 text

### Join a Game [POST] + Response 200 (application/json) + Attributes (Person)

Slide 64

Slide 64 text

### Join a Game [POST] + Response 200 (application/json) + Attributes (Person) + Response 418 (application/json) + Attributes + error: You cannot join a started game

Slide 65

Slide 65 text

Getting Started • API Blueprint (apiblueprint.org) • Editors • Apiary (apiary.io) • Aglio • Dredd

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Finding Existing Design Patterns in API Blueprint

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Documentation

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Integration

Slide 78

Slide 78 text

Aglio

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Contributing to API Blueprint

Slide 83

Slide 83 text

RFC Process

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Roadmap

Slide 87

Slide 87 text

Roadmap • Response based on parameter values • Affording Actions • MSON Parameters and Headers • Authentication • Multiple files (modularity) • External assets • State machine description

Slide 88

Slide 88 text

Response based on parameter values

Slide 89

Slide 89 text

Response based on parameter values ### Search for a question [GET /questions{?query,limit}] + Parameters + query: language - Search query + limit: 20 (number, optional) - Maximum number of questions returned + Response 200 (application/json) [ { "question": "Favourite programming language?" }, { "question": "API Description language preference" } ] + Request A search limited to one result + Parameters + query: language + limit: 1 + Response 200 (application/json) [ { "question": "Favourite programming language?" } ]

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

MSON Parameters and Headers

Slide 92

Slide 92 text

• Design First • Prototype • TDD

Slide 93

Slide 93 text

https://fuller.li/slides [email protected] Kyle Fuller