Token Management
Learn how to manage access tokens effectively.
Token Lifecycle
Authorization header of API requests.Caching Tokens
Cache tokens to avoid unnecessary authentication requests and improve performance.
Implementation strategy
-
Check cache first • Before requesting a new token, check if you have a valid cached token • Verify it has not expired
-
Use a time buffer • Consider a token expired 5 minutes before actual expiration • Avoids using a token that expires mid-request
-
Store token data • Save the access token and expiration timestamp (current time +
expires_in) • Use memory, database, or secure file storage -
Request new token when needed • If no cached token exists or it is expired or near expiration, request a new one • Update the cache
Handling Token Expiration
Automatically refresh tokens when they expire to ensure uninterrupted API access.
Automatic refresh strategy
-
Make API request • Use your cached token in the
Authorizationheader -
Check response status • If 2xx, continue • If
401 Unauthorized, the token has expired -
Handle 401 • Clear the expired token from cache • Request a new token, then retry the original request
-
Retry logic • Retry the failed request once with a fresh token • If it fails again, return the error and avoid infinite loops
Proactive refresh (recommended) Check expiration before each request. If the token expires in less than 5 minutes, refresh it first. This reduces request failures and improves reliability.
Security Best Practices
1. Store credentials securely
Never hardcode credentials in your code.
- Hardcoding client ID and secret in source code.
- Committing credentials to version control.
- Storing credentials in plain text in the web root.
Use environment variables, secure configuration, or secret management.
Example: AWS Secrets Manager, HashiCorp Vault, or CLIENT_SECRET in env.
Note : Keep credentials outside the web root.
2. Use HTTPS only
Always use HTTPS for all API requests.
- HTTP is rejected
- HTTPS encrypts data in transit
- Verify SSL certificates to prevent man-in-the-middle attacks
3. Protect token storage
If storing tokens in files or databases:
0600 on Unix).4. Implement token rotation
Rotate client credentials regularly.
- Generate new credentials from the Fuuffy dashboard
- Update application configuration and test in staging
- Deploy to production, then revoke old credentials after confirming the new ones work
- Monitor for authentication failures
Rotation schedule Every 90 days for routine rotation. Immediately if credentials are compromised or after team member departures.
Token Validation
Validate tokens before using them in API requests.
Checklist
-
Token exists • Present in cache or storage, not null or empty
-
Expiration data • Stored expiration timestamp is valid
-
Time remaining • Treat as invalid if it expires in less than 5 minutes
-
Format • Non-empty string with no unexpected characters or corruption
When validation fails
- Request a new token
- Update the cache
- Retry the operation
Troubleshooting
Invalid Token Error
Invalid Token Error
application/jsoninvalid_tokenResponse sample
{
"error": {
"code": "invalid_token",
"message": "The access token is invalid or expired",
"details": "Please request a new access token"
}
}
Common causes
• Token expired (1-hour validity) • Revoked or invalidated • Malformed • From a different environment (e.g. testing vs production)
Solution
- Clear the cached token
- Request a new access token
- Update the cache
- Retry the API request
Invalid Client Error
Invalid Client Error
application/jsoninvalid_clientResponse sample
{
"error": {
"code": "invalid_client",
"message": "Client authentication failed",
"details": "Invalid client credentials provided"
}
}
Common causes
• Wrong client ID or secret • Extra whitespace • Credentials not URL-encoded • Wrong environment • Credentials revoked
Solutions
-
Verify credentials • Copy from Fuuffy dashboard, no extra spaces
-
Confirm environment • Use the correct credentials for your environment
-
URL encoding • Ensure credentials are properly URL-encoded
-
Contact support • If still failing, contact Fuuffy support • Credentials may need to be regenerated