REST API Masterclass
Architecting Modern Backends01.Home02.What is REST?03.HTTP Deep Dive04.URI Best Practices05.JSON & Data Formats06.Environment Setup07.Your First Resource08.Advanced Controllers09.Database Strategy10.JWT Authentication11.Role-Based Auth (RBAC)12.API Versioning13.Filtering & Searching14.Pagination & Sorting15.Global Error Handling16.Rate Limiting17.CORS & Security18.Swagger & OpenAPI19.Testing with Supertest20.Webhooks & Caching21.Production Checklist
JSON & Data Formats
JSON (JavaScript Object Notation) is the de facto standard for modern REST APIs. It is lightweight, easy to read, and natively supported by almost every language.
1. Consistent Envelope Pattern
Consistency is king. Every response should follow the same pattern.
// ✅ Successful Response
{
"status": "success",
"data": {
"user": { "id": 1, "name": "Pradeep" }
}
}
// ❌ Error Response
{
"status": "error",
"message": "Invalid credentials",
"code": 401
}2. Proper Date Handling
Always use ISO 8601 format for dates in JSON. It's unambiguous and sortable as text.
"created_at": "2023-11-20T14:30:00Z"3. Snake_case vs CamelCase
While JavaScript prefers camelCase, many public APIs (and Python/PHP backends) use snake_case. Choose one and stick to it globally.
4. Handling Pagination
Don't just return a list. Return the "Metadata" about the list.
{
"data": [...],
"meta": {
"total_pages": 15,
"current_page": 1,
"limit": 50,
"total_results": 742
}
}Tip: Avoid returning
null values for missing fields if possible. Simply omitting the field or returning an empty array/string often makes client-side logic cleaner.