Building a Great Web API - Evan Cooke - QCON 2011

53
twilio CLOUD COMMUNICATIONS BUILDING A GREAT WEB API NOVEMBER 17, 2011, QCON EVAN COOKE @emcooke CO-FOUNDER & CTO H T T P

description

This presentation explores how fast signup, a clear value proposition, efficient quick starts, concise documentation, easy authentication and debugability are common attributes of many successful web APIs. The Twilio API is used as an example of how a focus on developer experience helps drive API adoption.

Transcript of Building a Great Web API - Evan Cooke - QCON 2011

Page 1: Building a Great Web API - Evan Cooke - QCON 2011

twilioCLOUD COMMUNICATIONS

BUILDING A

GREAT WEB APINOVEMBER 17, 2011, QCON

EVAN COOKE @emcookeCO-FOUNDER & CTO

HTTP

Page 2: Building a Great Web API - Evan Cooke - QCON 2011

One simple goalfor building great

Web APIs...

Page 3: Building a Great Web API - Evan Cooke - QCON 2011

5Minutes

Page 4: Building a Great Web API - Evan Cooke - QCON 2011

It should take no more than 5 minutes for a

developer to perform a useful task using your API for the first time

Page 5: Building a Great Web API - Evan Cooke - QCON 2011

What is a Web API?

• HTTP/HTTPS endpoint

• JSON/XML

• Hosted API/Web service

• Example: Amazon S3

Definition for today:

Page 6: Building a Great Web API - Evan Cooke - QCON 2011
Page 7: Building a Great Web API - Evan Cooke - QCON 2011

Twilio

Developer

End User

Carriers Inbound CallsOutbound Calls

Mobile/Browser VoIPVoice

SMS

PhoneNumbers

Send To/From Phone Numbers

Short Codes

Dynamically Buy Phone Numbers

Web service APIs to automate Voice and SMS phone communications

Page 8: Building a Great Web API - Evan Cooke - QCON 2011
Page 9: Building a Great Web API - Evan Cooke - QCON 2011

Why 5 Minutes?• Clear value proposition

• Fast signup

• Simple API

• Efficient quickstarts

• Concise accessible docs

• Easy authentication (basic vs. oauth)

• Debuggable

Page 10: Building a Great Web API - Evan Cooke - QCON 2011

API Adoption Funnel

Hear about API Try it

Try it

Build it

Production Use Buy it

Traction

Good APIsPromoteAdoption(tell friends)

Page 11: Building a Great Web API - Evan Cooke - QCON 2011

API Adoption Funnel

Hear about API 5-MinuteGoal

Traction

Try it

Try it

Build it

Buy itProduction Use

Page 12: Building a Great Web API - Evan Cooke - QCON 2011

5-minute APIsAPI Design

Page 13: Building a Great Web API - Evan Cooke - QCON 2011

5-minute APIsAPI Design

Counter Example

Page 14: Building a Great Web API - Evan Cooke - QCON 2011

• HTTP API to analyze large (MBs) media files

• High tps ~100,000/day

• POST data to the API

• API synchronously returns analysis of media (could be minutes/hours later)

Media processing API

You APIMedia

Analysis

Page 15: Building a Great Web API - Evan Cooke - QCON 2011

• Control and data in the same request (100 MB every request)

• Unclear error conditions. Can you resend request?

• Synchronously wait for response

• No request history/billing information

Media processing API

You API???

Page 16: Building a Great Web API - Evan Cooke - QCON 2011

Media processing API

POST http://api.vendor.com/API/Process?key=2hkh&mode=bob&filter=yeah

Body is 100MB of binary data

1 Original Request

ProblemCan we safely retry the request on

a transient failure?

Page 17: Building a Great Web API - Evan Cooke - QCON 2011

Media processing API

POST http://api.vendor.com/API/Process?key=2hkh&mode=bob&filter=yeah&token=TK123

Body is 100MB of binary data

Add a token allowing us to safely retry2

ProblemExposed to transient failures if the processing takes minutes/hours?

Page 18: Building a Great Web API - Evan Cooke - QCON 2011

Media processing API

POST http://api.vendor.com/API/Process?key=2hkh&mode=bob&filter=yeah&token=TK123&cbUrl=http%3A%2F%2Fmyserver.com%2Fresponse

Body is 100MB of binary data

Add webhook url to asynchronously respond3

ProblemRequest is huge and fragile when

data is included with control cmd?

Page 19: Building a Great Web API - Evan Cooke - QCON 2011

Media processing API

POST http://api.vendor.com/API/ProcessBody

key=2hkhmode=bobfilter=yeahtoken=TK123cbUrl=http://myserver.com/responsemediaUrl=http://s3.com/media.mov

Async fetch media & move POST params to body4

Page 20: Building a Great Web API - Evan Cooke - QCON 2011

Media processing API

POST http://api.vendor.com/v1/MediaBody

key=2hkhmode=bobfilter=yeahtoken=TK123cbUrl=http://myserver.com/responsemediaUrl=http://s3.com/media.mov

ResponseURI http://api.vendor.com/v1/Media/MD123

Version API and make URL more RESTful5

Page 21: Building a Great Web API - Evan Cooke - QCON 2011

5-minute APIsAPI Design

Page 22: Building a Great Web API - Evan Cooke - QCON 2011

Idempotency• Idempotence is the property of certain

operations that they can be applied multiple times without changing the result.

• Request failures happen. Provide users a safe way to retry requests

• Example:

POST /BankAccount/Funds

{‘value’: 1000, ‘token’: ‘TX123’}

Page 23: Building a Great Web API - Evan Cooke - QCON 2011
Page 24: Building a Great Web API - Evan Cooke - QCON 2011

Self-documentation• Sweat what and how you name. Naming is

is the best documentation.

• Example:

GET /Users/ID123GET /Users/ID123/FriendsGET /Users/ID123/Photos

Page 25: Building a Great Web API - Evan Cooke - QCON 2011
Page 26: Building a Great Web API - Evan Cooke - QCON 2011

RESTfulness• Adherence to REST object model and

verbs provides a clean way to expose business logic

• Create

• Fetch

• Modify

• Remove

POST /Users

GET /Users/ID123

PUT/POST /Users/ID123

DELETE /Users/ID123

Page 27: Building a Great Web API - Evan Cooke - QCON 2011
Page 28: Building a Great Web API - Evan Cooke - QCON 2011

Versioning• Do it. Remember you will be stuck

supporting old API versions (indefinitely?)

• Examples

GET /api/v1/blagGET /api/20101206/blag

GET /api/1/blag

Page 29: Building a Great Web API - Evan Cooke - QCON 2011
Page 30: Building a Great Web API - Evan Cooke - QCON 2011

Statefulness• When possible, offload the work of keeping

state/history because it’s hard!

• Examples

POST /CallsGET /Calls/CA123GET /Calls/CA123/Recordings/RE123

Page 31: Building a Great Web API - Evan Cooke - QCON 2011
Page 32: Building a Great Web API - Evan Cooke - QCON 2011

Predictability• The API should do what users expect

• Examples

<Play>music.mp3</Play>

<Play>music.aiff</Play>

Page 33: Building a Great Web API - Evan Cooke - QCON 2011
Page 34: Building a Great Web API - Evan Cooke - QCON 2011

Responsiveness• When a response is available immediately,

use a synchronous response. Otherwise, consider an asynchronous webhook rather then polling

• Examples:

POST /Object/TransformsBody cbUrl=http://b.com/response

Page 35: Building a Great Web API - Evan Cooke - QCON 2011
Page 36: Building a Great Web API - Evan Cooke - QCON 2011

• Idempotency

• Self-documentation

• RESTfulness

• Versioning

• Statefulness

• Predictability

• Responsiveness

Key Ideas

Page 37: Building a Great Web API - Evan Cooke - QCON 2011

5-minute APIs“Good”

Page 38: Building a Great Web API - Evan Cooke - QCON 2011

What makes a good API?

• Easy to Learn

• Easy to use (even without documentation)

• Hard to Misuse

• Easy to read and maintain code that uses it

• Sufficiently powerful to satisfy requirements

• Easy to extend

• Appropriate to audienceHow to Design a Good API and Why it MattersJoshua Bloch, Google

Page 39: Building a Great Web API - Evan Cooke - QCON 2011

Human-centric

Page 40: Building a Great Web API - Evan Cooke - QCON 2011
Page 41: Building a Great Web API - Evan Cooke - QCON 2011

Saleforceauth Campaigns from

Adword API

Page 42: Building a Great Web API - Evan Cooke - QCON 2011

Dynamic phone numberprovisioning via Twilio API

Page 43: Building a Great Web API - Evan Cooke - QCON 2011

Saleforce CRM data

Google Maps API

Saleforce app APIs

Twilio Client API

Page 44: Building a Great Web API - Evan Cooke - QCON 2011
Page 45: Building a Great Web API - Evan Cooke - QCON 2011

Simplicity

Page 46: Building a Great Web API - Evan Cooke - QCON 2011

twilioCLOUD COMMUNICATIONS

DEPLOYING YOUR WEB API

HTTP

Page 47: Building a Great Web API - Evan Cooke - QCON 2011

Infrastructure• 100’s of prod hosts in continuous operation

• 80+ service types running in prod

- Python(Twisted/GEvent), PHP, Java

• 50+ prod database servers

- MySQL, Redis

• Prod deployments several times/day across 7 engineering teams

Page 48: Building a Great Web API - Evan Cooke - QCON 2011

1000x

WebsiteContent

CMS

100x

WebsiteCode

PHP/Rubyetc.

10x

RESTAPI

Python/Javaetc.

1x

Big DBSchema

SQL

Log

Scal

e

DeploymentFrequency(Risk)

4 buckets

Page 49: Building a Great Web API - Evan Cooke - QCON 2011

WebsiteContent

One Click

WebsiteCode

One ClickCI Tests

RESTAPI

One Click

CI TestsHuman Sign-off

Big DBSchema

Human Assisted Click

CI TestsHuman Sign-off

DeploymentProcesses

Page 50: Building a Great Web API - Evan Cooke - QCON 2011

Cluster automation via boxconfig

Page 51: Building a Great Web API - Evan Cooke - QCON 2011

Cluster automation via boxconfig

• Build and deployment system - boot entire Twilio stack with one key press

• Host configuration - versioned code & config

• Host orchestration - load balancing

• Monitoring and alerting - nagios

• Multi-datacenter deployment & analytics

Page 52: Building a Great Web API - Evan Cooke - QCON 2011

Cluster automation via boxconfig

Boxconfig

Vanilla Linux Host(cloud/colo)

Provision

Base (AMI)

SVN/git

role role role Start Roles

S3Fetch

Page 53: Building a Great Web API - Evan Cooke - QCON 2011

twilioEvan Cooke@emcooke