RESTful API design principles and best practices
Applies RESTful API design principles when you're designing or reviewing API endpoints. Guides proper HTTP methods, status codes, resource naming, pagination, and error responses according to industry standards.
/plugin marketplace add pluginagentmarketplace/custom-plugin-api-design/plugin install custom-plugin-api-design@pluginagentmarketplace-api-designThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlassets/schema.jsonreferences/GUIDE.mdreferences/PATTERNS.mdscripts/validate.pyDesign RESTful APIs following industry best practices.
| Method | Action | Idempotent | Safe | Request Body | Response Body |
|---|---|---|---|---|---|
| GET | Read | Yes | Yes | No | Yes |
| POST | Create | No | No | Yes | Yes |
| PUT | Replace | Yes | No | Yes | Yes |
| PATCH | Update | No | No | Yes | Yes |
| DELETE | Delete | Yes | No | No | No |
| HEAD | Metadata | Yes | Yes | No | No |
| OPTIONS | Capabilities | Yes | Yes | No | Yes |
200 OK → GET, PUT, PATCH success
201 Created → POST success (include Location header)
202 Accepted → Async operation started
204 No Content → DELETE success
400 Bad Request → Validation failed
401 Unauthorized → Authentication required
403 Forbidden → Permission denied
404 Not Found → Resource doesn't exist
409 Conflict → State conflict (duplicate, etc.)
422 Unprocessable → Semantic errors
429 Too Many → Rate limit exceeded
500 Internal → Unexpected error
502 Bad Gateway → Upstream failed
503 Unavailable → Temporarily down
504 Timeout → Upstream timeout
# Collection operations
GET /api/v1/users → List (paginated)
POST /api/v1/users → Create
# Instance operations
GET /api/v1/users/{id} → Read
PUT /api/v1/users/{id} → Replace
PATCH /api/v1/users/{id} → Update
DELETE /api/v1/users/{id} → Delete
# Nested resources (max 2 levels)
GET /api/v1/users/{id}/orders → User's orders
POST /api/v1/users/{id}/orders → Create user's order
# Sub-resource instance
GET /api/v1/users/{id}/orders/{orderId}
# Actions (when CRUD doesn't fit)
POST /api/v1/orders/{id}/cancel
POST /api/v1/users/{id}/verify
POST /api/v1/reports/{id}/generate
{
"data": {
"id": "123",
"type": "user",
"attributes": {
"name": "John Doe",
"email": "john@example.com"
}
},
"meta": {
"requestId": "abc-123",
"timestamp": "2024-12-30T10:00:00Z",
"version": "1.0.0"
}
}
{
"type": "https://api.example.com/errors/validation",
"title": "Validation Failed",
"status": 400,
"detail": "One or more fields have validation errors",
"instance": "/api/v1/users",
"errors": [
{ "field": "email", "message": "Invalid email format" },
{ "field": "password", "message": "Minimum 12 characters" }
]
}
GET /api/v1/users?page=2&limit=20
Response:
{
"data": [...],
"pagination": {
"page": 2,
"limit": 20,
"total": 150,
"totalPages": 8,
"hasNext": true,
"hasPrev": true
}
}
GET /api/v1/users?cursor=eyJpZCI6MTAwfQ&limit=20
Response:
{
"data": [...],
"pagination": {
"nextCursor": "eyJpZCI6MTIwfQ",
"prevCursor": "eyJpZCI6ODB9",
"hasNext": true,
"hasPrev": true
}
}
# Filtering
GET /api/v1/users?status=active&role=admin
GET /api/v1/users?created_at[gte]=2024-01-01
GET /api/v1/users?search=john
# Sorting
GET /api/v1/users?sort=created_at:desc
GET /api/v1/users?sort=-created_at,name # - prefix for desc
# Field selection
GET /api/v1/users?fields=id,name,email
# Embedding related resources
GET /api/v1/users?include=orders,profile
# Response headers
Cache-Control: public, max-age=3600
ETag: "abc123"
Last-Modified: Wed, 21 Oct 2024 07:28:00 GMT
Vary: Accept-Encoding, Authorization
# Conditional requests
If-None-Match: "abc123"
If-Modified-Since: Wed, 21 Oct 2024 07:28:00 GMT
import { describe, it, expect } from 'vitest';
import request from 'supertest';
import app from './app';
describe('REST API - Users', () => {
describe('GET /api/v1/users', () => {
it('should return paginated users', async () => {
const res = await request(app)
.get('/api/v1/users?page=1&limit=10')
.expect(200);
expect(res.body).toHaveProperty('data');
expect(res.body).toHaveProperty('pagination');
expect(res.body.pagination.page).toBe(1);
});
it('should filter by status', async () => {
const res = await request(app)
.get('/api/v1/users?status=active')
.expect(200);
res.body.data.forEach(user => {
expect(user.status).toBe('active');
});
});
});
describe('POST /api/v1/users', () => {
it('should create user and return 201', async () => {
const res = await request(app)
.post('/api/v1/users')
.send({ email: 'test@example.com', name: 'Test' })
.expect(201);
expect(res.headers.location).toMatch(/\/api\/v1\/users\/\w+/);
expect(res.body.data.id).toBeDefined();
});
it('should return 400 for invalid data', async () => {
const res = await request(app)
.post('/api/v1/users')
.send({ email: 'invalid' })
.expect(400);
expect(res.body.type).toContain('validation');
});
});
describe('GET /api/v1/users/:id', () => {
it('should return 404 for non-existent user', async () => {
await request(app)
.get('/api/v1/users/non-existent-id')
.expect(404);
});
});
});
| Issue | Cause | Solution |
|---|---|---|
| 404 for valid resource | Trailing slash mismatch | Normalize URLs |
| CORS errors | Missing headers | Configure CORS middleware |
| Slow pagination | Large offset | Use cursor pagination |
| Cache not working | Missing Vary header | Add Vary: Authorization |
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.