Overview
TTL (Time To Live) provides a usage-based access control mechanism that decrements with each access attempt. Unlike expiration dates which are time-based, TTL limits access by counting the number of times a link is used.How TTL Works
TTL implements a hop-count system where each successful access decrements the TTL counter:- Create access link with
enableTTL: trueandttl: N - Each successful access decrements the TTL value by 1
- When TTL reaches 0, the link becomes permanently inaccessible
- TTL is checked before content is served
TTL is decremented during the access check, not after download completes. If TTL reaches 0 during access, that final access is denied.
Data Model
TTL is controlled by two fields in the Access model (seemodels/access.go:17-18):
EnableTTL- Master switch that activates TTL checkingTTL- Integer counter representing remaining accesses allowed
Creating TTL-Limited Links
API Examples
Success Response
TTL: 5 and EnableTTL: true fields in the response.
TTL Validation Logic
The TTL check is performed during access validation in the middleware (seemiddleware/access_restrictions.go:113-124):
- Check if
EnableTTLis true andTTL > 0 - Decrement TTL counter by 1
- If TTL reaches 0, deny access and abort
- If TTL > 0, save new value and allow access
- If
EnableTTLis false, skip TTL check entirely
TTL Lifecycle Example
Let’s trace a TTL-3 link through its lifecycle:Third Access
User accesses link, TTL decrements to 0.Access DENIED. Error: “Access link has reached its TTL limit”
The third access attempt is denied because the TTL reaches 0 during that request. Only the first two accesses succeed.
Error Response
When TTL reaches its limit:403 Forbidden
This error is permanent - the link can never be used again unless you update the TTL value through the API.
Monitoring TTL Usage
You can check the current TTL value of an access link:TTL: 2 indicates 2 accesses remaining.
Updating TTL
You can modify the TTL value or enable/disable TTL on existing access links:TTL vs One-Time Use
Both TTL and OneTimeUse limit access, but they work differently:| Feature | TTL | One-Time Use |
|---|---|---|
| Use count | Configurable (any integer) | Always 1 |
| Requires enable flag | Yes (enableTTL) | No |
| Decrements | During access | After access |
| Edge case | Denied when reaches 0 | Allowed on first, denied on second |
| Database field | Two fields (TTL, EnableTTL) | Two fields (OneTimeUse, Used) |
Setting
ttl: 1 with enableTTL: true is similar to oneTimeUse: true, but OneTimeUse is more explicit for single-use scenarios.Combining TTL with Other Restrictions
TTL can be layered with other access controls for comprehensive security:TTL + Expiration
- TTL reaches 0, OR
- Current time exceeds expiration date
TTL + IP Restrictions
- Client IP matches restrictions, AND
- TTL > 0 (if EnableTTL is true)
TTL + One-Time Use
Use Cases
Trial Downloads
Allow users to download a file multiple times during evaluation:Team Collaboration
Share a file with a small team, limiting total accesses:API Rate Limiting
Limit programmatic access to files:Content Preview
Allow limited previews before requiring payment:Implementation Reference
The TTL check is part of the access restrictions middleware pipeline (seemiddleware/access_restrictions.go:44-50):
- Network restrictions verified first
- Usage limits (OneTimeUse, TTL) checked
- Time-based expiration validated last
Best Practices
- Set Appropriate Values - Consider use case: 1-5 for limited sharing, 10-100 for team access
- Combine with Expiration - Use both TTL and expiration date for comprehensive control
- Monitor Usage - Check TTL values periodically to understand access patterns
- Document Purpose - Use descriptive
namefield to track why TTL was set - Reset When Needed - Update TTL if legitimate users need more access
- Avoid TTL + OneTimeUse - Choose one or the other, not both
- Test Before Sharing - Verify TTL decrements correctly by testing access
Next Steps
- One-Time Links - Create single-use access links
- Creating Access Links - Learn about other access restrictions
- IP Subnet Restrictions - Control access by network location