Epoch Timestamps in REST API Design
Best practices for using epoch timestamps in REST APIs. When to use epoch vs ISO 8601, how to document timestamp fields, and common API patterns.
Detailed Explanation
Epoch Timestamps in API Design
APIs need a consistent, unambiguous way to represent time. The choice between epoch integers and ISO 8601 strings affects client implementation complexity, debugging ease, and data size.
Common Patterns
Epoch seconds (Stripe, Twilio):
{
"id": "evt_123",
"created": 1705312200,
"expires_at": 1705398600
}
ISO 8601 (GitHub, AWS):
{
"id": "evt_123",
"created_at": "2024-01-15T09:30:00Z",
"expires_at": "2024-01-16T09:30:00Z"
}
Milliseconds (Discord, Twitter/X):
{
"id": "evt_123",
"timestamp": 1705312200000
}
Choosing a Format
| Criterion | Epoch (int) | ISO 8601 (string) |
|---|---|---|
| Payload size | Smaller | Larger |
| Human debugging | Harder | Easier |
| Timezone clarity | Implicit UTC | Explicit with Z or offset |
| Client parsing | Trivial | Requires date parser |
| Arithmetic | Native subtraction | Needs conversion |
| Documentation | Must specify seconds/ms | Self-documenting |
Best Practices
- Be consistent: Pick one format and use it across all endpoints
- Document the unit: If using epoch, always state whether it is seconds or milliseconds
- Use field naming conventions:
created_atfor ISO 8601,createdfor epoch - Always use UTC: Never return localized timestamps from an API
- Include timezone info: If using ISO 8601, always include
Zor an offset - Support both in input: Accept epoch or ISO 8601 in query parameters for flexibility
Versioning Considerations
If you need to change timestamp formats between API versions, provide clear migration guides. Changing from epoch to ISO 8601 (or vice versa) is a breaking change that requires a new API version.
Use Case
Reference this guide when designing REST APIs that include timestamp fields. The choice of format affects every client that consumes your API, so consistency and clear documentation are essential.