EMBRACE HTTP VERBS METHOD
USED FOR
IDEMPOTENT?
GET
Retrieve data
Yes
PUT
Change data
Yes
DELETE
Delete data
Yes
PATCH
Update data
No
POST
Change data
No
@akrabat
Slide 10
S TAT U S C O D E S M AT T E R 1xx
Informational
2xx
Success
3xx
Redirection
4xx
Client error
5xx
Server error
@akrabat
Slide 11
MEDIA TYPES • Read Content-Type header to decode incoming data • Honour the Accept header when sending data
@akrabat
Slide 12
HANDLING CHANGES • Avoid major new versions • Make changes backwards-compatible • Think about forwards-compatibility
@akrabat
Slide 13
A NEW VERSION IS A NEW API • Separate code • Domain or URL path is fine • api.example.com/v2/user • api2.example.com/user • Use Server header for minor and patch info
@akrabat
Slide 14
A GOOD API HAS
G R E AT E R R O R S
Slide 15
G R E AT E R R O R H A N D L I N G • Error representations are first class citizens • Code for computers; messages for humans • Pretty print for the humans!
@akrabat
Slide 16
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.”, “detail”: “The authentication service is down for maintenance.”, “instance”: “https://example.com/maintenance-schedule/2017-06”, “error_code”: “AUTHSERVICE_UNAVAILABLE”
@akrabat
H U M A N D O C U M E N TAT I O N • Tutorials • Reference
@akrabat
Slide 20
O P E N A P I S P E C I F I C AT I O N • Spec-First API Design • Tooling: https://openapi.tools
@akrabat
Slide 21
A GOOD API IS
SECURE
Slide 22
OAUTH 2 • Application identification • User identification • User’s trust relationship is with the API
@akrabat
Slide 23
R AT E L I M I T HTTP/1.1 429 Too Many Requests Content-Type: application/problem+json X-RateLimit-Limit: 5000 X-RateLimit-Remaining: 0 X-RateLimit-Reset: 1471549573 {
}
“status”: 429, “type”: “https://dev.example.com/rate_limits”, “title”: “API rate limited exceeded.”, “error_code”: “RATE_LIMIT_EXCEEDED”
@akrabat
Slide 24
TO SUM UP
Slide 25
THANK YOU HTTPS://JOIND.IN/EVENT/DUTCH-PHP-CONFERENCE-2019
ROB ALLEN
@akrabat