2. WHO AM I
• Phil Aylesworth
• Professor since 2000
• St. Clair College in Windsor Ontario
• Teach Linux, HTML, CSS, JavaScript
• Previously a Unix Administrator and Networking
Consultant
3. WHAT IS REST
• Representational StateTransfer
• A network API (client/server)
• Not a protocol, very few hard rules
• Uses HTTP 1.1 protocol and URIs (Uniform
Resource Identifiers)
4. QUICK INTRO
• Client makes a request
http://example.com/sculptures/39!
• Server responds to request by sending data
6. YOU ALREADY DO APIS
• If you do server-side coding, you are doing APIs
/show_all_artists.php
/show_artist.php?id=23
/update_artist.php?id=23&name=Jane+Doe
/delete_artist.php?id=23
7. IT’S ALL ABOUT DATA
• Often used to query or set data in a database
• But it can also be static files
• CRUD - Create / Read / Update / Delete
• Return data can be any format:
• JSON, XML,Text, HTML, CSV, etc.
8. RESOURCE
• Your API might have many resources
Eg: sculptures, artists, donors, etc.
• For each resource we should have two URLs
• One for the collection:
/sculptures
• One for individual items in collection:
/sculptures/39
14. RESOURCE NAMES
• No verbs in URI
• Plural or singular?
• Be consistent!
• Plural reads better
15. VERSIONING
• Always use a version
/v1/sculptures
• Only change version number if something breaks
• Don’t change the version
if features are added
16. COMPOUND RESOURCES
• Where it makes sense:
/sculptures/39/artists
/artists/14/sculptures!
• No more than 2 deep
/artists/14/sculptures/donors
17. HIDE COMPLEXITY
• Use URI attributes for complexity
/sculptures?material=bronze&size=small!
/sculptures?fields=name,artist
18. PAGINATION
• To return partial sets:
/sculptures?limit=25&offset=50!
• limit and offset are easy to understand
• Should have default limit, such as 100
19. FORMATS
• Output whatever formats you or your users, need:
JSON, XML, CSV, HTML
• Fairly easy to map one to another
/sculptures.json
/sculptures/39.json
/sculptures.xml!
• JSON is a good default
20. ERRORS
• Developers learn through errors
• Use HTTP status codes
• Return message - be verbose
• Optionally, include a URL to documentation
21. HTTP STATUS CODES
• Use a small number of HTTP status codes
200 - Okay
201 - Created
400 - Bad request
401 - Unauthorized
404 - Not found
405 - Method not allowed
500 - Internal server error
22. NAMING RETURNEDVALUES
• Doesn’t really matter as long as you are consistent
• If JSON is the default use camelCase since JSON is
JavaScript
24. DOMAIN NAMES
• Recommend using:
• api.example.com for your API
• developer.example.com for your documentation
• Redirect http://api.example.com/ to
http://developer.example.com
• Use HTTPs if possible