StateSet API Design Guidelines
This document outlines the design principles, patterns, and best practices for the StateSet API. Following these guidelines ensures consistency, usability, and maintainability across all API endpoints.Core Principles
1. RESTful Design
- Use standard HTTP methods appropriately:
GET
for retrieving resourcesPOST
for creating resourcesPUT/PATCH
for updating resourcesDELETE
for removing resources
2. Resource-Oriented URLs
3. Consistent Naming Conventions
- Use lowercase with hyphens for URLs:
/v1/work-orders
- Use camelCase for JSON properties:
firstName
,createdAt
- Use snake_case for query parameters:
created_after
,sort_by
- Pluralize collection endpoints:
/orders
not/order
Request Standards
Headers
Required headers for all requests:Query Parameters
Pagination
All list endpoints must support:limit
: 20 (max: 100)offset
: 0
Filtering
Use consistent filter patterns:Sorting
Request Body
Required Fields
Clearly mark required fields in documentation:Nested Objects
Use nested objects for logical grouping:Response Standards
Success Responses
Single Resource
Collection
With Metadata
Error Responses
Standard Error Format
HTTP Status Codes
Status | Usage |
---|---|
200 OK | Successful GET, PUT, PATCH |
201 Created | Successful POST creating resource |
202 Accepted | Request accepted for async processing |
204 No Content | Successful DELETE |
400 Bad Request | Invalid request parameters |
401 Unauthorized | Missing or invalid authentication |
403 Forbidden | Valid auth but insufficient permissions |
404 Not Found | Resource doesn’t exist |
409 Conflict | Resource conflict (e.g., duplicate) |
422 Unprocessable Entity | Validation errors |
429 Too Many Requests | Rate limit exceeded |
500 Internal Server Error | Server error |
503 Service Unavailable | Temporary unavailability |
Data Types and Formats
Timestamps
Always use ISO 8601 format with timezone:Money/Currency
Store monetary values in smallest currency unit (cents):Phone Numbers
Use E.164 format:Countries and States
Use ISO standards:- Countries: ISO 3166-1 alpha-2 (US, CA, GB)
- States/Provinces: ISO 3166-2 (CA-ON, US-NY)
Versioning
URL Versioning
Primary versioning method:Header Versioning
For minor versions:Deprecation Process
- Announce deprecation with 90-day notice
- Add deprecation headers:
- Maintain deprecated version for minimum 12 months
Idempotency
Implementation
Support idempotency for all POST, PUT, PATCH requests:Webhooks
Event Naming
Use dot notation for event types:Webhook Payload
Security
Always include signature header:Performance Guidelines
Response Times
Target response times:- Simple reads: < 200ms
- Complex queries: < 500ms
- Writes: < 1000ms
Payload Size
- Limit response size to 1MB
- Use pagination for large collections
- Support field filtering:
Caching
Include cache headers:GraphQL Guidelines
Query Naming
Mutations
Error Handling
Return errors in userErrors field:Testing
Test Coverage
All endpoints must include:- Success path tests
- Error handling tests
- Edge case tests
- Performance tests
Example Test Cases
Documentation Requirements
Every endpoint must document:- Description: Clear explanation of what the endpoint does
- Authentication: Required permissions
- Parameters: All query params, headers, and body fields
- Response: Success and error response formats
- Examples: Working code examples in multiple languages
- Rate Limits: Specific limits if different from defaults
- Webhooks: Related webhook events
- See Also: Links to related endpoints
Security Best Practices
- Always use HTTPS
- Validate all inputs - Never trust client data
- Rate limit all endpoints
- Log security events - Failed auth, permission denials
- Sanitize outputs - Prevent XSS in responses
- Use secure headers:
Monitoring and Observability
Include correlation IDs in all requests:Change Management
- Backwards Compatibility: Never break existing integrations
- Additive Changes: New fields are safe to add
- Deprecation Notices: Minimum 90 days
- Migration Guides: Provide clear upgrade paths
- Changelog: Maintain detailed changelog
Contact
For API design questions or to propose changes to these guidelines:- Email: api-team@stateset.com
- Slack: #api-design
- GitHub: stateset/api-guidelines