Category: api design

Data-First API Design Chart

A starting point for designing a simple restful API for your data-first web applications

Created in 2012 for

mdavidgreen.com

by M. David Green

POST GET PUT DELETE
collection

/items

Create a new item

/items

Return a set of items

/items?date=20120612

Update the date for a set of items

/items

Delete a set of items

item

/items/123

error

/items/123

Return item 123

/items/123?date=20120612

Update the date for item 123

/items/123

Delete item 123

versioned collection

/v1/items

Create a new item

/v1/items

Return a set of items

/v1/items?date=20120612

Update the date for a set of items

/v1/items

Delete a set of items

versioned item

/v1/items/123

error

/v1/items/123

Return item 123

/v1/items/123?date=20120612

Update the date for item 123

/v1/items/123

Delete item 123

nested collection

/items/123/parts

create a new part for item 123

/items/123/parts

Return parts for item 123

/items/123/parts?date=20120612

Update the date for parts for item 123

/items/123/parts

Delete parts for item 123

nested item

/items/123/parts/2

error

/items/123/parts/2

Return item 123 part 2

/items/123/parts/2?date=20120612

Update the date for item 123 part 2

/items/123/parts/2

Delete item 123 part 2

legacy client collection

/items?method=post

Create a new item

/items?method=get

Return a set of items

/items?method=put&date=20120612

Update the date for a set of items

/items?method=delete

Delete a set of items

legacy client item

/items/123?method=post

error

/items/123?method=get

Return item 123

/items/123?method=put&date=20120612

Update the date for item 123

/items/123?method=delete

Delete item 123

collection filtered by fields

/items

Create a new item

/items?fields=name,color,version

Return the name, color, and version values for a set of items

/items

Update a set of items

/items

Delete a set of items

collection filtered by content

/items

Create a new item

/items?color=red

Return a set of items with a color of red

/items

Update a set of items

/items

Delete a set of items

XML format collection

/items

Create a new item

/items.xml

Return a set of items in XML format

/items

Update a set of items

/items

Delete a set of items

XML format item

/items/123

error

/items/123.xml

Return item 123 in XML format

/items/123

Update item 123

/items/123

Delete item 123

paginated collection

/items

create a new item

/items?offset=0&count=10

return a set of items

/items

update a set of items

/items

delete a set of items

collection search

/items

create a new item

/items/search?q=red

return items matching "red"

/items

update a set of items

/items

delete a set of items

Data-First API Design with JSON

An easy API is one that can and (maybe) will be used. Before you start marketing your API to potential developers, you need to design it to be useful and straightforward. Design a flexible, resilient, and sensible API for your web service so applications and their respective presentations can evolve as they will on top of it.

Here are some opinions about how to strategize your API design if you don't know where to start, regardless of your server-side architecture.

Visual Reference Chart

API Design Chart
Here's a handy chart that lists the various urls to support for a basic RESTful api.

Versioning

Consider versioning before you design your first version.
Put the version first in the URL: /v1/items
Abstract the version control layer to allow the back end and the front end to evolve independently.

Minimal URL Structures

Use plural nouns for collection actions: /items
POST: create a new item in a collection
GET: retrieve a set of items
PUT: update or modify matching items
DELETE: delete all items

Use plural nouns plus unique identifiers for single item actions: /items/123
POST: error
GET: retrieve a single item
PUT: update an existing item if it exists. error otherwise.
DELETE: delete an item

Use a method parameter for clients that don't support HTTP methods:
POST: /items?method=post
GET: /items
PUT: /items/123?method=put&version=2
DELETE: /items/123?method=delete

Nested data should also use plurals and unique identifiers: /items/123/accessories/456/colors/2

Filtering

Design for the primary use cases first.
Allow refinement through parameters: /items?color=red
Provide a way to return a filtered set of data: /items?fields=name,color,size

Documentation and Testing

Document your api as you develop
Include representative urls for typical use cases
Let your API test suite dictate the scope of the documentation

Pagination

Set default pagination based on primary use cases
Override pagination using parameters for offset and count: /items?offset=0&count=10

Output Formats

Default to JSON return types.
Use a suffix to specify a return type: /items.xml or /items/123.json

Subdomains

Create a unified api.domain.com subdomain for all api calls
send visitors to a documentation page if they navigate directly to your api subdomain

Search

Use verbs for functionality such as search or computation: /search?q=red
Add the verb to a collection plural noun for limited search: /items/search?q=red

Error Handling

The error code is for machines, but the message is for humans at a time of great need, so be verbose.
Allow the developer using your API to hide error response codes as an edge case: /items?suppress-errors=true

Access Control

Issue unencrypted API keys to help keep track of how your API is being used
Lock API keys to specific credentials or IP addresses to improve accuracy and allow metered API access
Add OpenID or HTTP authentication and SSL to allow secure API access and content manipulation
Support Oauth for secure communication across and among services