2024-09-04
Design robust, scalable RESTful APIs with proper resource modeling, versioning, and security practices.
RESTful APIs are the backbone of modern web applications. This guide covers REST principles, resource design, HTTP methods, authentication, versioning, and best practices for building APIs that scale.
Client-Server
Separation of concerns
Stateless
Each request contains all information
Cacheable
Responses must define cacheability
Uniform Interface
Consistent resource identification
GET - Retrieve resource
POST - Create resource
PUT - Update entire resource
PATCH - Partial update
DELETE - Remove resource
OPTIONS - Get allowed methods
Well-designed RESTful endpoints:
# Collection resources
GET /api/users # List users
POST /api/users # Create user
# Individual resources
GET /api/users/123 # Get specific user
PUT /api/users/123 # Update user
PATCH /api/users/123 # Partial update
DELETE /api/users/123 # Delete user
# Nested resources
GET /api/users/123/posts # User's posts
POST /api/users/123/posts # Create post for user
# Filtering and pagination
GET /api/users?status=active&page=2&limit=20
GET /api/posts?sort=-created_at&filter[status]=published
{
"data": {
"id": "123",
"type": "user",
"attributes": {
"name": "John Doe",
"email": "john@example.com",
"createdAt": "2024-01-01T00:00:00Z"
},
"relationships": {
"posts": {
"links": {
"self": "/api/users/123/posts"
}
}
}
},
"meta": {
"timestamp": "2024-09-04T10:00:00Z"
}
}
{
"errors": [
{
"status": "400",
"code": "VALIDATION_ERROR",
"title": "Invalid Input",
"detail": "Email address is not valid",
"source": {
"pointer": "/data/attributes/email"
}
}
]
}
# Request with Bearer token
GET /api/users/profile
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
# Token refresh endpoint
POST /api/auth/refresh
Content-Type: application/json
{
"refreshToken": "..."
}
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000
Content-Security-Policy: default-src 'self'
URL Path Versioning
/api/v1/users
/api/v2/users
Header Versioning
Accept: application/vnd.api+json;version=2
Query Parameter
/api/users?version=2
Use Proper Status Codes
200 OK, 201 Created, 204 No Content, 400 Bad Request, 401 Unauthorized, 404 Not Found, 500 Server Error
Implement Rate Limiting
Protect your API from abuse with rate limiting headers: X-RateLimit-Limit, X-RateLimit-Remaining
HATEOAS
Include links in responses to guide clients through available actions and related resources
Published on 2024-09-04 • Category: Backend
← Back to BlogFree online developer tools and utilities for encoding, formatting, generating, and analyzing data. No registration required - all tools work directly in your browser.
Built for developers, by developers. Privacy-focused and open source.
Free online tools for Base64 encoding, JSON formatting, URL encoding, hash generation, UUID creation, QR codes, JWT decoding, timestamp conversion, regex testing, and more.
© 2024 NarvikHub. All rights reserved.