API Development Skill
Design and build high-quality APIs that are secure, scalable, and easy to consume.
When to Use
Use this skill when the user wants to:
- Design a new API (RESTful, GraphQL, or gRPC)
- Implement CRUD operations and business logic
- Create API documentation (OpenAPI/Swagger)
- Implement authentication and authorization for an API
- Add request/response validation
- Implement pagination and filtering for large datasets
- Handle API versioning
API Paradigms
1. REST (Representational State Transfer)
- Concept: Resource-based, uses standard HTTP methods (GET, POST, PUT, DELETE, PATCH).
- Best Practice: Use meaningful URIs, standard HTTP status codes, and versioning (e.g.,
/api/v1/).
2. GraphQL
- Concept: Query language for APIs; clients request exactly the data they need.
- Best Practice: Use strong typing with schemas, implement query complexity limits, and use resolvers effectively.
3. gRPC
- Concept: High-performance, binary protocol using Protocol Buffers (Protobuf).
- Best Practice: Define strict service contracts, use streaming if necessary, and leverage interceptors for cross-cutting concerns.
Core Components
API Design Principles
- Clear Contract: Define request/response formats explicitly using schemas (OpenAPI, GraphQL Schema, Protobuf).
- Consistent Naming: Follow conventions (e.g., kebab-case for URIs, camelCase for JSON fields).
- Error Handling: Return descriptive error messages and appropriate HTTP status codes.
- Versioning: Always version your API to prevent breaking changes.
Security
- Authentication: Implement JWT, OAuth2, or API Keys.
- Authorization: Use RBAC (Role-Based Access Control) or ABAC (Attribute-Based Access Control).
- Validation: Always validate incoming request payloads against a strict schema.
- Rate Limiting: Protect your API from abuse and DoS attacks.
Performance
- Pagination: Use cursor-based or offset-based pagination for large collections.
- Caching: Use HTTP headers (Cache-Control) or server-side caching (Redis) to reduce load.
- Compression: Enable Gzip or Brotli to reduce payload size.
Implementation Examples
Standard Error Response Format
{
"error": {
"code": "INVALID_INPUT",
"message": "The 'email' field must be a valid email address.",
"details": [
{
"field": "email",
"issue": "format"
}
]
}
}
Common Pitfalls
- Inconsistent Error Responses: Returning different error formats for different errors, making it hard for clients to handle them.
- Lack of Versioning: Making breaking changes to an existing API without versioning, which breaks all current clients.
- Ignoring Pagination: Returning massive datasets in a single response, causing performance issues and high latency.
- Over-reliance on Client-Side Validation: Relying only on the frontend for validation; always re-validate on the server.
- Poor Error Messages: Returning generic "Internal Server Error" messages without providing enough context for debugging (while still being secure).
Deliverables
- Complete API implementation (REST, GraphQL, or gRPC)
- API documentation (OpenAPI/Swagger, GraphQL Schema, or Protobuf files)
- Input/Output validation schemas
- Error handling strategy
- Authentication and authorization implementation
- Integration tests for all endpoints
Quality Checklist
1---2name: api-development3description: Design and implement robust APIs (REST, GraphQL, gRPC). Focus on clear contracts, authentication, validation, and scalable architecture.4---56# API Development Skill78Design and build high-quality APIs that are secure, scalable, and easy to consume.910## When to Use1112Use this skill when the user wants to:13- Design a new API (RESTful, GraphQL, or gRPC)14- Implement CRUD operations and business logic15- Create API documentation (OpenAPI/Swagger)16- Implement authentication and authorization for an API17- Add request/response validation18- Implement pagination and filtering for large datasets19- Handle API versioning2021## API Paradigms2223### 1. REST (Representational State Transfer)24- **Concept**: Resource-based, uses standard HTTP methods (GET, POST, PUT, DELETE, PATCH).25- **Best Practice**: Use meaningful URIs, standard HTTP status codes, and versioning (e.g., `/api/v1/`).2627### 2. GraphQL28- **Concept**: Query language for APIs; clients request exactly the data they need.29- **Best Practice**: Use strong typing with schemas, implement query complexity limits, and use resolvers effectively.3031### 3. gRPC32- **Concept**: High-performance, binary protocol using Protocol Buffers (Protobuf).33- **Best Practice**: Define strict service contracts, use streaming if necessary, and leverage interceptors for cross-cutting concerns.3435## Core Components3637### API Design Principles38- **Clear Contract**: Define request/response formats explicitly using schemas (OpenAPI, GraphQL Schema, Protobuf).39- **Consistent Naming**: Follow conventions (e.g., kebab-case for URIs, camelCase for JSON fields).40- **Error Handling**: Return descriptive error messages and appropriate HTTP status codes.41- **Versioning**: Always version your API to prevent breaking changes.4243### Security44- **Authentication**: Implement JWT, OAuth2, or API Keys.45- **Authorization**: Use RBAC (Role-Based Access Control) or ABAC (Attribute-Based Access Control).46- **Validation**: Always validate incoming request payloads against a strict schema.47- **Rate Limiting**: Protect your API from abuse and DoS attacks.4849### Performance50- **Pagination**: Use cursor-based or offset-based pagination for large collections.51- **Caching**: Use HTTP headers (Cache-Control) or server-side caching (Redis) to reduce load.52- **Compression**: Enable Gzip or Brotli to reduce payload size.5354## Implementation Examples5556### Standard Error Response Format57```json58{59 "error": {60 "code": "INVALID_INPUT",61 "message": "The 'email' field must be a valid email address.",62 "details": [63 {64 "field": "email",65 "issue": "format"66 }67 ]68 }69}70```7172## Common Pitfalls7374- **Inconsistent Error Responses**: Returning different error formats for different errors, making it hard for clients to handle them.75- **Lack of Versioning**: Making breaking changes to an existing API without versioning, which breaks all current clients.76- **Ignoring Pagination**: Returning massive datasets in a single response, causing performance issues and high latency.77- **Over-reliance on Client-Side Validation**: Relying only on the frontend for validation; always re-validate on the server.78- **Poor Error Messages**: Returning generic "Internal Server Error" messages without providing enough context for debugging (while still being secure).7980## Deliverables8182- Complete API implementation (REST, GraphQL, or gRPC)83- API documentation (OpenAPI/Swagger, GraphQL Schema, or Protobuf files)84- Input/Output validation schemas85- Error handling strategy86- Authentication and authorization implementation87- Integration tests for all endpoints8889## Quality Checklist9091- [ ] API follows a clear and consistent design pattern.92- [ ] Request/Response formats are explicitly documented.93- [ ] Input validation is performed on all incoming data.94- [ ] Proper HTTP status codes or error responses are used.95- [ ] Authentication and authorization are correctly implemented.96- [ ] API is versioned.97- [ ] Pagination is implemented for large datasets.98- [ ] Error messages are descriptive but don't leak sensitive info.