RESTful API Design OCTO Quick Reference Card 2.2
RESTful API Design OCTO Quick Reference Card 2.2
API
Design
–
OCTO
Quick
Reference
Card
AUDIENCE
:
API
DESIGNERS
–
API
DEVELOPERS
General
concepts
Medium
grained
resources
{ "id":"007", !
"firstname":"James",!
You
should
use
medium
grained,
not
fine
nor
coarse.
"name":"Bond",!
KISS
Resources
shouldn’t
be
nested
more
than
two
level
deep
:! "address":{!
Anyone
should
be
able
to
use
your
API
without
having
to
refer
to
the
documentaTon.
GET /users/007! "street":"H.Ferry Rd.",!
"country":{"name":"London"}!
• Use
standard,
concrete
and
shared
terms,
not
your
specific
business
terms
or
acronyms.
}!
• Never
allow
applicaTon
developers
to
do
things
more
than
one
way.
}!
• Design
your
API
for
your
clients
(ApplicaTon
developers),
not
for
your
data.
You
may
consider
the
following
five
subdomains
• Target
major
uses
cases
first,
deal
with
excepTons
later.
• ProducTon
-‐
https://api.fakecompany.com
GET /orders, GET /users, GET /products, ...! • Tests
-‐
https://api.sandbox.fakecompany.com
! • Developer
portal
-‐
https://developers.fakecompany.com!
CURL
• ProducTon
-‐
https://oauth2.fakecompany.com
You
should
use
CURL
to
share
examples,
which
can
be
easily
copy/paste.
• Tests - https://oauth2.sandbox.fakecompany.com
CURL –X POST \!
-H "Accept: application/json" \ Security
:
OAuth2
&
HTTPS
-H "Authorization: Bearer at-80003004-19a8-46a2-908e-33d4057128e7" \ ! You
should
use
OAuth2
to
manage
AuthorizaTon.
-d '{"state":"running"}' \ ! • OAuth2
matches
99%
of
requirements
and
client
typologies,
don't
reinvent
the
wheel,
you'll
fail.
https://api.fakecompany.com/v1/users/007/orders?client_id=API_KEY_003 ! You
should
use
HTTPS
for
every
API/OAuth2
requests.
!
URLs
CRUD-‐like
operaGons
:
Use
HTTP
verbs
for
CRUD
operaTons
(Create/Read/Update/Delete).
HTTP
Verb
CollecGon
:
/orders
Instance
:
/orders/{id}
Nouns
GET
Read
a
list
orders.
200
OK.
Read
the
detail
of
a
single
order.
200
OK.
You
should
use
nouns,
not
verbs
(vs
SOAP-‐RPC).
GET /orders not /getAllOrders!
POST
Create
a
new
order.
201
Created.
-‐
!
PUT
Full
Update
:
200
OK./
Create
a
specific
order
:
Plurals
-‐
201
Created.
You
should
use
plural
nouns
not
singular
nouns
to
manage
two
different
types
of
resources
:
• CollecTon
resource
:
/users
PATCH
-‐
ParTal
Update.
200
OK.
• Instance
resource
:
/users/007
DELETE
-‐
Delete
order.
204
OK.
You
should
remain
consistent.
GET /users/007 not GET /user/007! POST
is
used
to
Create
an
instance
of
a
collecTon.
The
ID
isn’t
provided,
and
the
new
resource
locaTon
is
! returned
in
the
“LocaTon”
Header.
Consistent
case
POST /orders {"state":"running", "id_user":"007"}!
201 Created !
You
may
choose
between
snake_case
or
camelCase
for
aWributes
and
parameters,
but
you
should
Location: https://api.fakecompany.com/orders/1234!
remain
consistent.
GET /orders?id_user=007 ! or
GET /orders?idUser=007!
POST/orders {"id_user":"007"} ! or POST/orders {"idUser":"007"}! But
remember
that,
if
the
ID
is
specified
by
the
client,
PUT
is
used
for
Create.
PUT /orders/1234!
201 Created !
If
you
have
to
use
more
than
one
word
in
URL,
you
should
use
spinal-‐case
(some
servers
ignore
case).
!
POST /specific-orders!
PUT
is
used
for
Update
to
perform
a
full
replacement.
PUT /orders/1234 {"state":"paid", "id_user":"007"}!
Versioning
200 Ok!
You
should
make
versioning
mandatory
in
the
URL
at
the
highest
scope
(major
versions).
You
may
support
at
most
two
versions
at
the
same
Tme
(NaTve
apps
need
a
longer
cycle).
PATCH
is
commonly
used
for
parTal
Update.
GET /v1/orders! PATCH /orders/1234 {"state":"paid"}!
! 200 Ok!
Hierarchical
structure
You
should
leverage
the
hierarchical
nature
of
the
URL
to
imply
structure
(aggregaTon
or
composiTon).
GET
is
used
to
Read
a
collecTon.
GET
is
used
to
Read
an
instance.
Ex
:
an
order
contains
products.
GET /orders ! ! GET /orders/1234!
GET /orders/1234/products/1! 200 Ok ! ! 200 Ok!
! [{"id":"1234", "state":"paid"} {"id":"1234", "state":"paid"}!
{"id":"5678", "state":"running"}]!
!
©2014
OCTO
Technology
www.octo.com
!
Page
1
RESTful
API
Design
–
OCTO
Quick
Reference
Card
AUDIENCE
:
API
DESIGNERS
–
API
DEVELOPERS
DISCLAMER
This
Reference
Card
doesn’t
claim
to
be
absolutely
accurate.
The
design
concepts
exposed
result
from
our
pervious
work
in
the
REST
area.
Please
check
out
our
blog
hWp://blog.octo.com,
and
feel
free
to
comment/challenge
this
API
cookbook.
We
are
really
looking
forward
to
sharing
with
you.
One
more
thing
We
encourage
you
to
be
pragmaTc,
for
the
benefit
of
your
customers…
Applica'on
developers.
Sources
Design
BeauTful
REST
+
JSON
APIs
>
hWp://www.slideshare.net/stormpath/rest-‐jsonapis
Web
API
Design:
Craxing
Interfaces
that
Developers
Love
>
hWps://pages.apigee.com/web-‐api-‐design-‐website-‐h-‐ebook-‐registraTon.html
HTTP
API
Design
Guide
>
hWps://github.com/interagent/hWp-‐api-‐design
RESTful
Web
APIs
>
hWp://shop.oreilly.com/product/0636920028468.do