Skip to content

πŸ”Œ API Design ​

Designing clean, consistent, and developer-friendly APIs.

REST conventions ​

ActionMethodEndpointResponse
ListGET/users200 + array
Get oneGET/users/:id200 or 404
CreatePOST/users201 + created object
Update (full)PUT/users/:id200 + updated object
Update (partial)PATCH/users/:id200 + updated object
DeleteDELETE/users/:id204 (no content)

Naming rules ​

  • Use nouns, not verbs: /users not /getUsers
  • Use plural: /users not /user
  • Use kebab-case: /user-profiles not /userProfiles
  • Nest for relationships: /users/:id/orders
  • Max 2 levels of nesting

Status codes ​

CodeMeaningWhen
200OKSuccessful read/update
201CreatedSuccessful creation
204No ContentSuccessful deletion
400Bad RequestValidation error
401UnauthorizedMissing/invalid auth
403ForbiddenAuth valid but no permission
404Not FoundResource doesn't exist
409ConflictDuplicate or state conflict
422UnprocessableValid syntax but semantic error
500Server ErrorUnexpected failure

Error response format ​

json
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Email is required",
    "details": [
      { "field": "email", "message": "must not be empty" }
    ]
  }
}

Pagination ​

GET /users?page=2&limit=20

{
  "data": [...],
  "meta": {
    "page": 2,
    "limit": 20,
    "total": 150,
    "totalPages": 8
  }
}

Versioning ​

  • URL path: /api/v1/users (most common)
  • Header: Accept: application/vnd.api+json;version=1
  • Only version when breaking changes are needed

Pergame Knowledge Base