Skip to content

REST API Cheatsheet

REST (Representational State Transfer) is an architectural style for web services that uses HTTP to access and manipulate resources. Introduced by Roy Fielding in 2000.

PrincipleDescription
Client-ServerClear separation between client and server
StatelessEach request contains all needed information
CacheableResponses marked as cacheable or non-cacheable
Uniform InterfaceStandard methods, consistent resource URIs
Layered SystemCan include proxies, gateways, load balancers
Code on DemandServer can send executable code (optional)

A REST API is modeled as collections of individually-addressable resources, which are:

  • Nouns of the API (e.g., books, users, orders)
  • Referenced by URIs (resource names)
  • Manipulated via HTTP methods: GET, POST, PUT, PATCH, DELETE

Resources are the nouns of your API: the “things” your system manages (books, users, orders). The URL identifies what (the resource), the HTTP verb specifies what to do with it.

TypeDescriptionExample
SingletonSingle specific instance/planets/mars
CollectionGroup of same-type resources/planets
Sub-collectionCollection under a parent/planets/mars/moons

Follow these 6 steps to design a REST API:

  1. Identify Resources: Extract nouns from your domain
  2. Determine Types: Singleton, collection, or sub-collection?
  3. Define Relationships: Sub-resources vs references
  4. Design URLs: Plural nouns, lowercase, hyphens
  5. Map HTTP Methods: CRUD operations to verbs
  6. Design Responses: JSON structure with _links

  • Use plural nouns: /planets, /missions
  • Use lowercase with hyphens: /space-stations
  • No verbs in URLs: /missions, not /getMissions
  • Keep hierarchy shallow (2-3 levels max)
/api/missions # Collection
/api/missions/{id} # Singleton
/api/missions/{id}/crew # Sub-collection
/api/missions/{id}/crew/{id} # Singleton in sub-collection

HTTP methods (verbs) define what action to perform on a resource. The URL identifies the resource; the verb says what to do with it.

Key concepts:

  • Idempotent: Same request multiple times = same result (safe to retry)
  • Safe: Doesn’t modify server state (read-only)
MethodPurposeIdempotentSafeRequest Body
GETRetrieveYesYesNo
POSTCreateNoNoYes
PUTReplace entireYesNoYes
PATCHPartial updateYes*NoYes
DELETERemoveYesNoNo

*PATCH is idempotent when implemented correctly

EndpointGETPOSTPUTPATCHDELETE
/resourcesList allCreate newN/AN/AN/A
/resources/{id}Get oneN/AReplaceUpdateRemove

Common mistakes to avoid:

  • Using GET for operations that modify data
  • Using POST for everything instead of proper verbs

Status codes indicate the outcome of a request:

  • 2xx: Success
  • 4xx: Client error (your fault)
  • 5xx: Server error (our fault)
CodeMeaning
200OK: Request successful
201Created: Resource created
204No Content: Success, no body
400Bad Request: Invalid data
404Not Found: Resource doesn’t exist

For complete status codes, see HTTP Status Codes.


Include essential fields and navigation links:

{
"id": "apollo-11",
"name": "Apollo 11",
"status": "completed",
"launch_date": "1969-07-16",
"_links": {
"self": "/api/missions/apollo-11",
"crew": "/api/missions/apollo-11/crew"
}
}

Guidelines:

  • Include only essential fields
  • Add _links for navigation (HATEOAS)
  • Use pagination for collections
  • Keep consistent structure across resources

ScenarioResource Type
Single configuration/settingsSingleton
List of similar itemsCollection
Items belonging to a parentSub-collection
One-to-one relationshipSingleton sub-resource
One-to-many relationshipSub-collection

When to use sub-resources: If deleting the parent should delete the child, use sub-resources. Otherwise, use references.