Learn How To Design REST API in 9 Minutes
Learn How To Design REST API in 9 Minutes
REST API
Design
Best Practices
The Ultimate Guide
to Building
Effective APIs
swipe
brijpandeyji
General Concepts
KISS
Anyone should be able to use your API without having to
refer to the documentation.
Example
GET /salaries, GET /employees, GET /departments, ...
swipe
brijpandeyji
CURL
You should use CURL to share examples, which can be
easily copy/paste.
Example
CURL –X POST \
-H "Accept: application/json" \
-d '{"state":"running"}' \
https://api.company.com/v1/employee/11/orders?employee_id=111
"id": "11",
"firstname" : "John",
"name" : "Brown",
"address" : {
}
swipe
brijpandeyji
You may consider the following five
subdomains
Production - https://api.website.com
Tests - https://api.sandbox.website.com
Developer Portal - https://developers.website.com
Production - https://oauth2.website.com
Tests - https://oauth2.sandbox.website.com
swipe
brijpandeyji
URLs
Nouns
You should use nouns, not verbs (vs SOAP-RPC).
GET /employees not /getAllEmployees
Plurals
You should use plural nouns not singular nouns to
manage two different types of resources :
Collection resource : /employee
Instance resource : /employees/11
swipe
brijpandeyji
Consistent Case
You may choose between snake_case or camelCase for
attributes and parameters, but you should remain
consistent.
Example
GET /salaries?id_user=11 or GET /salaries?idUser=11
POST/salaries {"id_user" : "11"} or POST/salaries {"idUser" : "11"}
Versioning
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 (Native apps need a
longer cycle).
GET /v1/salaries
swipe
brijpandeyji
Hierarchical Structure
You should leverage the hierarchical nature of the URL
to imply structure (aggregation or composition). Ex : an
order contains products.
Example
GET /salaries/3456/departments/1
CRUD-like Operations
Use HTTP verbs for CRUD operations (Create/Read/
Update/Delete).
HTTP Verb Collection : /orders Instance : /orders/{id}
GET Read a list orders. 200 OK. Read the detail of a single order.
200 OK.
swipe
brijpandeyji
POST is used to create an instance of a collection. The
ID isn’t provided, and the new resource location is
returned in the "Location" Header.
POST /salaries {"state" : "processing", "id_user" : "11"}
201 Created
Location: https://api.website.com/salaries/3456
201 Created
200 Ok
200 Ok
swipe
brijpandeyji
GET is used to Read a collection.
GET /salaries
200 Ok
[ {"id":"3456", "state":"credited"}
{"id":"9874", "state":"processing"}]
200 Ok
{"id":"3456", "state":"credited"}
swipe
brijpandeyji
Query Strings
Search
You may use the "Google way" to perform a global
search on multiple resources.
Example
GET /search?q=processing+credited
Filters
You should use '?' to filter resources
GET /salaries?state=credited&id_user=11
swipe
brijpandeyji
Pagination
You may use a range query parameter. Pagination is
mandatory : a default pagination has to be defined, for
example : range=0-25
/orders?range=18-25
Content-Range: 18-25/971
Accept-Range: order 10
https://api.website.com/v1/salaries?range=28-53; rel="prev",
https://api.website.com/v1/salaries?range=57-69; rel="next",
https://api.website.com/v1/salaries?range=71-813; rel="last"
swipe
brijpandeyji
Partial Responses
You should use partial responses so developers can
select information needed and optimize bandwidth
(essential for mobile development).
GET /employees/11?fields=firstname,name,address(street)
200 OK
{ "id" : "11",
"firstname" : "John",
"name" : "Brown",
Sort
Use ?sort=attribute1, attributeN to sort resources. By
default resources are sorted in ascending order. Use ?
desc=attribute1, atributeN to sort resources in
descending order.
GET /companies?sort=rating,reviews,name;desc=rate,reviews
swipe
brijpandeyji
URL reserved words : first, last, count
Use /first to get the 1st element
GET /employees/first
200 OK
{"id":"3456", "state":"credited"}
200 OK
{"id":"6789", "state":"processing"}
200 OK
{"2"}
swipe
brijpandeyji
I18N
Use ISO 8601 standard for Date/Time/Timestamp :
1978-05-10T06:06:06+00:00 or 1978-05-10 Add support
for different languages.
Accept-Language: fr-ca, fr-fr not ?language=fr
swipe
brijpandeyji
Cross-origin Requests
Use CORS standard to support REST API requests from
browsers (js SPA…). But if you plane to support Internet
Explorer 7/8 or 9, you shall consider specifics endpoints
to add a Jsonp support.
All requests will be sent with a GET method!
Content negotiation cannot be handled with Accept Header
in Jsonp.
Payload cannot be used to send data.
POST /salaries and /salaries.jsonp?method=POST&callback=foo
swipe
brijpandeyji
HATEOAS
Your API should propose Hypermedia links in order to be
completely discoverable. But keep in mind that a
majority of users wont probably use those hyperlinks
(for now), and will read the API documentation and
copy/paste call examples.
So, each call to the API should return in the Link Header
every possible state of the application from the current
state, plus self.
GET /employees/11
< 200 Ok
swipe
brijpandeyji
"Non Resources" Scenarios
In a few use cases we have to consider operations or
services rather than resources. You may use a POST
request with a verb at the end of the URI.
POST /cellphone/91/send
POST /convert?from=USD&to=INR&amount=1200
swipe
brijpandeyji
Follow Me On
LinkedIn
https://www.linkedin.com/in/brijpandeyji/