Skip to content

Instantly share code, notes, and snippets.

@mohashari
Created March 14, 2026 15:12
Show Gist options
  • Select an option

  • Save mohashari/bc69361c5aff9beb588ae4d5c0956ad0 to your computer and use it in GitHub Desktop.

Select an option

Save mohashari/bc69361c5aff9beb588ae4d5c0956ad0 to your computer and use it in GitHub Desktop.
Code snippets — Rest Api Design Principles
{
"data": [...],
"meta": {
"total": 1543,
"page": 2,
"per_page": 20,
"next_cursor": "eyJpZCI6MTIwfQ"
}
}
/users # collection
/users/42 # single resource
/users/42/posts # nested resource
200 OK — Success, returning data
201 Created — Resource created (POST)
204 No Content — Success, nothing to return (DELETE)
400 Bad Request — Client sent invalid data
401 Unauthorized — Authentication required
403 Forbidden — Authenticated but not authorized
404 Not Found — Resource doesn't exist
409 Conflict — State conflict (duplicate, etc.)
422 Unprocessable Entity — Validation failed
429 Too Many Requests — Rate limited
500 Internal Server Error — Your bug
/api/v1/users
/api/v2/users
Accept: application/vnd.myapp.v2+json
# Offset pagination
GET /users?page=2&per_page=20
# Cursor pagination (better for large datasets)
GET /users?cursor=eyJpZCI6MTAwfQ&limit=20
GET /users?status=active&role=admin&sort=created_at&order=desc
Content-Type: application/json
Accept: application/json
POST /payments
Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000
{
"amount": 9900,
"currency": "USD"
}
{
"error": {
"code": "VALIDATION_FAILED",
"message": "Request validation failed",
"details": [
{
"field": "email",
"message": "Must be a valid email address"
}
]
}
}
# Bad
GET /getUsers
POST /createUser
DELETE /deleteUser/42
# Good
GET /users
POST /users
DELETE /users/42
openapi: 3.1.0
info:
title: My API
version: 1.0.0
paths:
/users:
get:
summary: List all users
parameters:
- name: page
in: query
schema:
type: integer
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/UserList'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment