P H T T E VAN C OOKE @emcooke C O -F OUNDER & CTO twilio CLOUD - - PowerPoint PPT Presentation

p
SMART_READER_LITE
LIVE PREVIEW

P H T T E VAN C OOKE @emcooke C O -F OUNDER & CTO twilio CLOUD - - PowerPoint PPT Presentation

B UILDING A G REAT W EB API N OVEMBER 17, 2011, QC ON P H T T E VAN C OOKE @emcooke C O -F OUNDER & CTO twilio CLOUD COMMUNICATIONS One simple goal for building great Web APIs... 5 Minutes It should take no more than 5 minutes for a


slide-1
SLIDE 1

twilio

CLOUD COMMUNICATIONS

BUILDING A GREAT WEB API

NOVEMBER 17, 2011, QCON EVAN COOKE @emcooke CO-FOUNDER & CTO

HTT

P

slide-2
SLIDE 2

One simple goal for building great Web APIs...

slide-3
SLIDE 3

5

Minutes

slide-4
SLIDE 4

It should take no more than 5 minutes for a developer to perform a useful task using your API for the first time

slide-5
SLIDE 5

What is a Web API?

  • HTTP/HTTPS endpoint
  • JSON/XML
  • Hosted API/Web service
  • Example: Amazon S3

Definition for today:

slide-6
SLIDE 6
slide-7
SLIDE 7

Twilio

Developer End User Carriers

Inbound Calls Outbound Calls Mobile/Browser VoIP

Voice SMS Phone Numbers

Send To/From Phone Numbers Short Codes Dynamically Buy Phone Numbers

Web service APIs to automate Voice and SMS phone communications

slide-8
SLIDE 8
slide-9
SLIDE 9

Why 5 Minutes?

  • Clear value proposition
  • Fast signup
  • Simple API
  • Efficient quickstarts
  • Concise accessible docs
  • Easy authentication (basic vs. oauth)
  • Debuggable
slide-10
SLIDE 10

API Adoption Funnel

Hear about API Try it Try it Build it Production Use Buy it Traction Good APIs Promote Adoption (tell friends)

slide-11
SLIDE 11

API Adoption Funnel

Hear about API 5-Minute Goal Traction Try it Try it Build it Buy it Production Use

slide-12
SLIDE 12

5-minute APIs API Design

slide-13
SLIDE 13

5-minute APIs API Design

C

  • u

n t e r E x a m p l e

slide-14
SLIDE 14
  • 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 API

Media Analysis

slide-15
SLIDE 15
  • 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

???

slide-16
SLIDE 16

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 Problem Can we safely retry the request on a transient failure?

slide-17
SLIDE 17

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 retry

2

Problem Exposed to transient failures if the processing takes minutes/hours?

slide-18
SLIDE 18

Media processing API

POST http://api.vendor.com/API/Process? key=2hkh&mode=bob&filter=yeah&token=TK123&c bUrl=http%3A%2F%2Fmyserver.com%2Fresponse Body is 100MB of binary data

Add webhook url to asynchronously respond

3

Problem Request is huge and fragile when data is included with control cmd?

slide-19
SLIDE 19

Media processing API

POST http://api.vendor.com/API/Process Body key=2hkh mode=bob filter=yeah token=TK123 cbUrl=http://myserver.com/response mediaUrl=http://s3.com/media.mov

Async fetch media & move POST params to body

4

slide-20
SLIDE 20

Media processing API

POST http://api.vendor.com/v1/Media Body key=2hkh mode=bob filter=yeah token=TK123 cbUrl=http://myserver.com/response mediaUrl=http://s3.com/media.mov Response URI http://api.vendor.com/v1/Media/MD123

Version API and make URL more RESTful

5

slide-21
SLIDE 21

5-minute APIs API Design

slide-22
SLIDE 22

Idempotency

  • Idempotence is the property of certain
  • perations 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’}

slide-23
SLIDE 23
slide-24
SLIDE 24

Self-documentation

  • Sweat what and how you name. Naming is

is the best documentation.

  • Example:

GET /Users/ID123 GET /Users/ID123/Friends GET /Users/ID123/Photos

slide-25
SLIDE 25
slide-26
SLIDE 26

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

slide-27
SLIDE 27
slide-28
SLIDE 28

Versioning

  • Do it. Remember you will be stuck

supporting old API versions (indefinitely?)

  • Examples

GET /api/v1/blag GET /api/20101206/blag GET /api/1/blag

slide-29
SLIDE 29
slide-30
SLIDE 30

Statefulness

  • When possible, offload the work of keeping

state/history because it’s hard!

  • Examples

POST /Calls GET /Calls/CA123 GET /Calls/CA123/Recordings/RE123

slide-31
SLIDE 31
slide-32
SLIDE 32

Predictability

  • The API should do what users expect
  • Examples

<Play>music.mp3</Play> <Play>music.aiff</Play>

slide-33
SLIDE 33
slide-34
SLIDE 34

Responsiveness

  • When a response is available immediately,

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

  • Examples:

POST /Object/Transforms Body cbUrl=http://b.com/response

slide-35
SLIDE 35
slide-36
SLIDE 36
  • Idempotency
  • Self-documentation
  • RESTfulness
  • Versioning
  • Statefulness
  • Predictability
  • Responsiveness

Key Ideas

slide-37
SLIDE 37

5-minute APIs “Good”

slide-38
SLIDE 38

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 audience

How to Design a Good API and Why it Matters Joshua Bloch, Google

slide-39
SLIDE 39

Human-centric

slide-40
SLIDE 40
slide-41
SLIDE 41

Saleforce auth Campaigns from Adword API

slide-42
SLIDE 42

Dynamic phone number provisioning via Twilio API

slide-43
SLIDE 43

Saleforce CRM data Google Maps API Saleforce app APIs Twilio Client API

slide-44
SLIDE 44
slide-45
SLIDE 45

Simplicity

slide-46
SLIDE 46

twilio

CLOUD COMMUNICATIONS

DEPLOYING YOUR WEB API

HTT

P

slide-47
SLIDE 47

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

slide-48
SLIDE 48

1000x

Website Content

CMS

100x

Website Code

PHP/Ruby etc.

10x

REST API

Python/Java etc.

1x

Big DB Schema

SQL

Log Scale

Deployment Frequency(Risk)

4 buckets

slide-49
SLIDE 49

Website Content

One Click

Website Code

One Click CI Tests

REST API

One Click CI Tests Human Sign-off

Big DB Schema

Human Assisted Click CI Tests Human Sign-off

Deployment Processes

slide-50
SLIDE 50

Cluster automation via boxconfig

slide-51
SLIDE 51

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
slide-52
SLIDE 52

Cluster automation via boxconfig

Boxconfig

Vanilla Linux Host (cloud/colo)

Provision

Base (AMI) SVN/git

role role role Start Roles

S3

Fetch

slide-53
SLIDE 53

twilio

Evan Cooke @emcooke