49
Building Brilliant APIs that bloody work Monday, 4 October 2010

Building Brilliant APIs

Embed Size (px)

DESCRIPTION

Mine and Jason's talk from BarCampBrighton 5 about building brilliant APIs that bloody work. Probably won't make sense without having been there.. Any questions give myself (@ben_c) or Jason (@fry15) a shout on twitter.

Citation preview

Page 1: Building Brilliant APIs

Building Brilliant APIsthat bloody work

Monday, 4 October 2010

Page 2: Building Brilliant APIs

Jason Fry

@fry15jasonfry.co.uk

Monday, 4 October 2010

Page 3: Building Brilliant APIs

Ben Collier

@ben_cbencollier.net

@redsignredsign.co.uk

Monday, 4 October 2010

Page 4: Building Brilliant APIs

Our Inspiration?

• Working with a terrible API

Monday, 4 October 2010

Page 5: Building Brilliant APIs

Our Inspiration?<xml>

<head>

</head>

<body>

<response>Random Useless Text</response>

</body>

</xml>

Monday, 4 October 2010

Page 6: Building Brilliant APIs

Why Build APIs?

• Mobile Applications

• Rich Web Applcations

Monday, 4 October 2010

Page 7: Building Brilliant APIs

Mobile Applications

• Enables your app to talk to your service

• Don’t just open database connections

Monday, 4 October 2010

Page 8: Building Brilliant APIs

Websites

• Optimisation

• Caching

Monday, 4 October 2010

Page 9: Building Brilliant APIs

Websites

• Twitter

• http://engineering.twitter.com

• Facebook

Monday, 4 October 2010

Page 10: Building Brilliant APIs

But...

“I’m not planning on building an API yet so why should I care?”

Monday, 4 October 2010

Page 11: Building Brilliant APIs

What Makes a Brilliant API?

Monday, 4 October 2010

Page 13: Building Brilliant APIs

Building URLs

• Base URI

• e.g. api.twitter.com/v2/

• Method

• e.g. statuses/user_timeline.xml

Monday, 4 October 2010

Page 14: Building Brilliant APIs

Responses

• XML / JSON

• Clear structure

Monday, 4 October 2010

Page 15: Building Brilliant APIs

Responses<topalbums user="RJ" type="overall"> <album rank="1"> <name>Images and Words</name> <playcount>174</playcount> <mbid>f20971f2-c8ad-4d26-91ab-730f6dedafb2</mbid> <url> http://www.last.fm/music/Dream+Theater/Images+and+Words </url> <artist> <name>Dream Theater</name> <mbid>28503ab7-8bf2-4666-a7bd-2644bfc7cb1d</mbid> <url>http://www.last.fm/music/Dream+Theater</url> </artist> <image size="small">...</image> <image size="medium">...</image> <image size="large">...</image> </album></topalbums>

Monday, 4 October 2010

Page 16: Building Brilliant APIs

Error Codes

• Numbers which mean something

• Success, error

• Missing parameter x

• Parameter x must be 3 characters long

• etc etc

Monday, 4 October 2010

Page 17: Building Brilliant APIs

• SOAP

• RESTful

Types of APIs

Monday, 4 October 2010

Page 18: Building Brilliant APIs

Types of APIs

SOAP

Simple Object Access Protocol

Monday, 4 October 2010

Page 19: Building Brilliant APIs

Types of APIs

RESTful

Representable State Transfer

Monday, 4 October 2010

Page 20: Building Brilliant APIs

RESTful

Based on HTTP Request protocol

Monday, 4 October 2010

Page 21: Building Brilliant APIs

RESTful

GETPUT POSTDELETE

Based on HTTP Request protocol

Monday, 4 October 2010

Page 22: Building Brilliant APIs

RESTful

GET

PUT

POST

DELETE

Based on HTTP Request protocol

Monday, 4 October 2010

Page 23: Building Brilliant APIs

RESTful

GET POST

Based on HTTP Request protocol

Monday, 4 October 2010

Page 24: Building Brilliant APIs

Versioning

• API version in URL

• eg http://locationtextvideoapi.com/2.0/

• Allows backwackwards compatibility without sacrificing future versions

Monday, 4 October 2010

Page 25: Building Brilliant APIs

Authentication

• Two choices

• OAuth

• Basic (http / session)

Monday, 4 October 2010

Page 26: Building Brilliant APIs

Basic Authentication

Monday, 4 October 2010

Page 27: Building Brilliant APIs

Basic Authentication

PasswordUsername

Monday, 4 October 2010

Page 28: Building Brilliant APIs

Basic Authentication

PasswordUsername

5f4dcc3b5aa765d61d8327deb882cf99

Monday, 4 October 2010

Page 29: Building Brilliant APIs

Basic Authentication

PasswordUsername

5f4dcc3b5aa765d61d8327deb882cf99

Monday, 4 October 2010

Page 30: Building Brilliant APIs

OAuth

Open Standard Greater Control User Safety

Monday, 4 October 2010

Page 31: Building Brilliant APIs

Responses

Monday, 4 October 2010

Page 32: Building Brilliant APIs

Responses

XML JSON .plist

Monday, 4 October 2010

Page 33: Building Brilliant APIs

Responses

XML JSON .plist

Objects

Monday, 4 October 2010

Page 34: Building Brilliant APIs

XML

• PHP DomDocument

• PEAR Library for exporting straight from MySQL

Monday, 4 October 2010

Page 35: Building Brilliant APIs

JSON

Monday, 4 October 2010

Page 36: Building Brilliant APIs

JSON

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

Monday, 4 October 2010

Page 37: Building Brilliant APIs

JSON

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);

Monday, 4 October 2010

Page 38: Building Brilliant APIs

JSON

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);

{"a":1,"b":2,"c":3,"d":4,"e":5}

Monday, 4 October 2010

Page 39: Building Brilliant APIs

UGC

Images

Audio /Video

Vzaar.com

Encoding.com

FFMpeg

Monday, 4 October 2010

Page 40: Building Brilliant APIs

Media Links

• Link in a logical way

• Eg: http://apisrock.com/64x64/23456.jpg

Monday, 4 October 2010

Page 41: Building Brilliant APIs

Location Aware

Location based searching can be a bitch

Monday, 4 October 2010

Page 42: Building Brilliant APIs

Documentation

Monday, 4 October 2010

Page 43: Building Brilliant APIs

Documentation

• Do it

Monday, 4 October 2010

Page 44: Building Brilliant APIs

Documentation

• Do it

• Even if it’s not public facing- do it!

Monday, 4 October 2010

Page 45: Building Brilliant APIs

Hosting

• Build in PHP / MySQL

• Deployable on Amazon Web Services

Monday, 4 October 2010

Page 46: Building Brilliant APIs

Some Good Examples

• Last.fm

• Twitter

• Foursquare

Monday, 4 October 2010

Page 47: Building Brilliant APIs

Any questions?

• Good.

Monday, 4 October 2010

Page 48: Building Brilliant APIs

Thanks

Ben [email protected]

@ben_c

Jason [email protected]

@fry15

Monday, 4 October 2010

Page 49: Building Brilliant APIs

Thanks

Ben [email protected]

@ben_c

Jason [email protected]

@fry15

Bubble Image- http://www.flickr.com/photos/philippbunge/Hammock- http://www.flickr.com/photos/ironrodart/ Padlock- http://www.flickr.com/photos/sooperkuh/Binary Windows - http://www.flickr.com/photos/

9619972@N08/Boxing Lemur - http://www.flickr.com/photos/e_phots/

Earth from Space - NASAMonday, 4 October 2010