SlideShare a Scribd company logo
Building RESTful APIs
Vancouver Python Day
November 16, 2013

Ganesh Swami
www.silota.com
Hi
•

Programming professionally for 10+ years
•

x86 assembly, STL, boost, python-boost, python
!

•

Built emacs-­‐wiki-­‐blog: first blogging engine for
Emacs!
SILOTA
•

Search As A Service
•

•

full stack: crawling, indexing, retrieving, tag deployment

Python shop:
•
•

ansible	
  

•

sentry	
  

•

django	
  

•
•

pelican	
  

django-­‐rest-­‐framework	
  

In beta testing: love more feedback!
APIs: What & Why
What is an API?
Application Programming Interface
!

An API is the interface implemented by an
application which allows other applications to
communicate with it.
What is an API?
communicate
What is REST?
•

REpresentational State Transfer
•

logical resources manipulated with HTTP verbs

•

modern best practice

•

wide adoption

•

contrast with SOAP
Why build an API?
•

explosion of devices connected to the internet

•

can be a company’s greatest asset

•

bizdev 2.0: internal developers, consultants,
partners, customers
Sample APIs
•

aws

•

dropbox

•

instagram

•

pinterest

•

github

•

stripe

•

salesforce

•

parse

•

…
Source: Mary Meeker’s Internet Trends 2013
APIs: How
Top 3 qualities
•

Intuitive
•

•

no surprises, easy to learn

Documented
•
•

•

simple answers to simple questions
references, tutorials & quick starts

Opinionated
•

camelCase, ids, responses, pagination, etc.
Building RESTful APIs
Resources,
Status Codes &
Errors
Resources
•

Nouns, not verbs

•

Coarse grained, not fine grained

•

example: let’s build a document datastore!
Smells like RPC
•

/getDocument	
  

•

/getAllDocuments	
  

•

/createDocument	
  

•

/updateDocument	
  

•

/deleteDocument
Smells like RPC
•

/getDocument	
  

•

/getAllDocuments	
  

•

/createDocument	
  

•

/updateDocument	
  

•

/deleteDocument

This is a bad example. !
Don’t do this!
Embrace HTTP
•

GET,	
  POST,	
  PUT,	
  PATCH,	
  DELETE	
  
!

•

Explorable with simple tools
Embrace HTTP
GET	
  /document
GET	
  /document/19
POST	
  /document

Retrieve all documents

Retrieve a specific document #19

Create a new document

PUT	
  /document/19

Update an existing document #19

DELETE	
  /document/19

Delete an existing document #19
Bipartite graph
/documents

/documents/:id

GET
error

POST
PUT

error

PATCH

error

DELETE

…
Status Codes
2xx

OK, created, all good, carry on

4xx

User error: bad API key, malformed
data, item not found, etc.

5xx

Server error
Errors
•

Errors
•

as descriptive as possible

•

developers are your customers

•

never naked 4xx/5xx HTTP errors
Errors
<xml	
  version="1.0"?>	
  
<Error>	
  
	
  	
  	
  	
  <Message>A	
  server	
  error	
  has	
  occurred</Message>	
  
	
  	
  	
  	
  <Description>Unknown	
  Error</Description>	
  
	
  	
  	
  	
  <Id>1234</Id>	
  
</Error>

Just no.
Errors
{	
  
	
  	
  "code"	
  :	
  1234,	
  
	
  	
  "message"	
  :	
  "Unsupported	
  media	
  type	
  ‘text/html’	
  in	
  request",	
  
	
  	
  "description"	
  :	
  "Requests	
  need	
  to	
  have	
  the	
  Content-­‐Type	
  HTTP	
  
header	
  set	
  to	
  ‘application/json’"	
  
}
pip-install httpie
Best practices
security
base URLs
serialization
timestamps
versioning
caching
gzip
logging
Best practices
security

https all the way

base URLs

api.companyname.com

serialization

json

timestamps

ISO 8601 & UTC

versioning

/v1/

caching

ETag & Last-Modified

gzip

always & pretty print responses

logging

if possible
Recap
•

https + gzip + json

•

draw bipartite graph of nouns and verbs

•

great documentation

•

no surprises
django-­‐rest-­‐framework
Why use a framework?
Myths
•

roll your own

•

use a ‘lightweight’ framework

•

too tied to django

•

too slow
Features
•

pagination

•

permission

•

authentication

•

serialization

•

throttling

•

data validation

•

proper HTTP response handling
Magic formula: Mixins
Views

Authentication

Permissions

Throttling

CreateAPIView

Token

Any

SimpleRate

ListAPIView

Session

Token

AnonRate

RetrieveAPIView

OAuth

Authentication

DeleteAPIView
Four step formula
1. create the model
2. write the serializer
3. write the view
4. configure the urls
References
•

How to Design a Good API and Why it Matters:
•

•

Best Practices for Designing a Pragmatic RESTful API
•

•

http://www.vinaysahni.com/best-practices-for-a-pragmaticrestful-api

REST worst practices:
•

•

http://lcsd05.cs.tamu.edu/slides/keynote.pdf

http://jacobian.org/writing/rest-worst-practices/

http://django-rest-framework.org/
Keep in touch!
Ganesh Swami!
www.silota.com
ganesh@silota.com
@gane5h

More Related Content

PPTX
Django rest framework
PDF
Django rest framework tips and tricks
PDF
API Design & Security in django
PPTX
REST Easy with Django-Rest-Framework
PDF
Introduction to Django REST Framework, an easy way to build REST framework in...
PPTX
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
PDF
Building an API with Django and Django REST Framework
PDF
Apache Sling as an OSGi-powered REST middleware
Django rest framework
Django rest framework tips and tricks
API Design & Security in django
REST Easy with Django-Rest-Framework
Introduction to Django REST Framework, an easy way to build REST framework in...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Building an API with Django and Django REST Framework
Apache Sling as an OSGi-powered REST middleware

What's hot (20)

PDF
Unlocking the Magical Powers of WP_Query
ZIP
Drupal, Android and iPhone
PPTX
In-browser storage and me
PDF
Middleware in Golang: InVision's Rye
PDF
RESTFul development with Apache sling
PPTX
Making Watir and Cucumber an efficient tool for Web UI Automation
PPT
Build Your Own CMS with Apache Sling
KEY
WordPress APIs
PPTX
PowerShell for SharePoint Admins
PDF
Euroclojure2014: Schema & Swagger - making your Clojure web APIs more awesome
PDF
TDD a REST API With Node.js and MongoDB
ODP
2017 Codemotion OWASP ZAP in CI/CD
PDF
Two scoops of Django - Deployment
PDF
JCR and Sling Quick Dive
PDF
MEAN Stack Workshop at Node Philly, 4/9/14
PPTX
Ci of js and apex using jasmine, phantom js and drone io df14
PDF
Case study: JBoss Developer Studio, an IDE for Web, Mobile and Cloud applicat...
ODP
2017 DevSecCon ZAP Scripting Workshop
PDF
SANS DFIR Prague: PowerShell & WMI
PDF
Configuration As Code - Adoption of the Job DSL Plugin at Netflix
Unlocking the Magical Powers of WP_Query
Drupal, Android and iPhone
In-browser storage and me
Middleware in Golang: InVision's Rye
RESTFul development with Apache sling
Making Watir and Cucumber an efficient tool for Web UI Automation
Build Your Own CMS with Apache Sling
WordPress APIs
PowerShell for SharePoint Admins
Euroclojure2014: Schema & Swagger - making your Clojure web APIs more awesome
TDD a REST API With Node.js and MongoDB
2017 Codemotion OWASP ZAP in CI/CD
Two scoops of Django - Deployment
JCR and Sling Quick Dive
MEAN Stack Workshop at Node Philly, 4/9/14
Ci of js and apex using jasmine, phantom js and drone io df14
Case study: JBoss Developer Studio, an IDE for Web, Mobile and Cloud applicat...
2017 DevSecCon ZAP Scripting Workshop
SANS DFIR Prague: PowerShell & WMI
Configuration As Code - Adoption of the Job DSL Plugin at Netflix
Ad

Viewers also liked (20)

PPTX
RESTful APIs: Promises & lies
PDF
RESTful API Design, Second Edition
PPT
Linux Composite Communication
PDF
12 tips on Django Best Practices
PPTX
Maak Kennis Met Finchline 110207
PPTX
Mobile api test approach
PDF
Introduction to Big Data Infrastructure
PPT
Are RESTful APIs Well-designed? Detection of their Linguistic (Anti)Patterns
PDF
All you need to know when designing RESTful APIs
PPTX
API Management Part 1 - An Introduction to Azure API Management
PDF
RESTFul API Design and Documentation - an Introduction
PDF
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
PDF
Pycon 2008: Python Command-line Tools *Nix
PDF
Securing Your API
PPTX
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
PPT
Swift Architecture and Practice, by Alex Yang
PPTX
Software Defined presentation
PDF
Microservices with Swagger, Flask and Docker
PPTX
Core Concept: Software Defined Everything
PPTX
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
RESTful APIs: Promises & lies
RESTful API Design, Second Edition
Linux Composite Communication
12 tips on Django Best Practices
Maak Kennis Met Finchline 110207
Mobile api test approach
Introduction to Big Data Infrastructure
Are RESTful APIs Well-designed? Detection of their Linguistic (Anti)Patterns
All you need to know when designing RESTful APIs
API Management Part 1 - An Introduction to Azure API Management
RESTFul API Design and Documentation - an Introduction
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
Pycon 2008: Python Command-line Tools *Nix
Securing Your API
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
Swift Architecture and Practice, by Alex Yang
Software Defined presentation
Microservices with Swagger, Flask and Docker
Core Concept: Software Defined Everything
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
Ad

Similar to Building RESTful APIs (20)

PPTX
Super simple introduction to REST-APIs (2nd version)
PDF
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
PDF
REST in pieces
PDF
[drupalday2017] - REST in pieces
PPTX
Apitesting.pptx
PDF
API Basics
PDF
zendframework2 restful
PDF
Coding 100-session-slides
PPTX
Api crash
PPTX
Api crash
PPTX
Api crash
PPTX
Api crash
PPTX
Api crash
PPTX
Api crash
PPTX
Api crash
PPTX
REST-API introduction for developers
PDF
REST APIs, Girls Who Code
PDF
REST API Recommendations
PDF
Panoramic view of web APIs
PPTX
Http and REST APIs.
Super simple introduction to REST-APIs (2nd version)
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
REST in pieces
[drupalday2017] - REST in pieces
Apitesting.pptx
API Basics
zendframework2 restful
Coding 100-session-slides
Api crash
Api crash
Api crash
Api crash
Api crash
Api crash
Api crash
REST-API introduction for developers
REST APIs, Girls Who Code
REST API Recommendations
Panoramic view of web APIs
Http and REST APIs.

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
A Presentation on Artificial Intelligence
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Network Security Unit 5.pdf for BCA BBA.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
A Presentation on Artificial Intelligence
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
The Rise and Fall of 3GPP – Time for a Sabbatical?
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding

Building RESTful APIs