Overview
Sawmills uses a token exchange system where you can exchange a long-lived API key for a short-lived access token. This provides better security by limiting the exposure time of credentials. Token Exchange Flow:- Use your API key to request an access token
- Use the access token to authenticate with other Sawmills services
- Refresh the token as needed (tokens expire after 30 minutes by default)
Exchanging API Key for Token
API Endpoint
Authentication
Use your API key in the Authorization header with theApiKey scheme:
Request Body
The request body should be empty:Response
access_token: JWT token for authenticating with other servicestoken_type: Always “Bearer” for Sawmills tokensexpires_in: Token expiration time in seconds (default: 1800 = 30 minutes)
Example: Exchanging API Key for Token with cURL
Example: Exchanging API Key for Token with JavaScript
Using Access Tokens
Authentication Header
Use the access token in the Authorization header with theBearer scheme:
Example: Using Token with cURL
Example: Using Token with JavaScript
Token Management
Token Expiration
- Default TTL: 30 minutes (1800 seconds)
- Maximum TTL: 24 hours (configurable by organization)
- Refresh Required: Tokens cannot be refreshed; you must exchange a new token
Automatic Token Refresh
Implement automatic token refresh in your applications:Error Handling
Common Error Responses
401 Unauthorized - Invalid API KeyHandling Token Errors
Security Considerations
Token Storage
- Store access tokens in memory only (not persistent storage)
- Never log or expose access tokens
- Implement secure token refresh mechanisms
API Key Security
- Keep your API key secure and never expose it in client-side code
- Use environment variables or secure key management systems
- Rotate API keys regularly
Network Security
- Always use HTTPS for token exchange and API calls
- Validate SSL certificates
- Consider using certificate pinning for additional security
Troubleshooting
Common Issues
“Authorization header required”- Ensure you’re including the Authorization header in your request
- Check that the header is properly formatted
- Use
ApiKey(notBearer) when exchanging API keys for tokens - Format:
Authorization: ApiKey YOUR_API_KEY
- Verify your API key is correct and active
- Check that the API key hasn’t been revoked
- Ensure the API key is correctly formatted
- This is a server-side error
- Check Sawmills service status
- Contact support if the issue persists
Debugging Tips
- Verify API Key: Ensure your API key is correctly formatted
- Check Token Expiry: Verify the token hasn’t expired before using it
- Test with cURL: Use cURL to test token exchange independently
- Review Logs: Check application logs for detailed error information
Best Practices
Application Design
- Implement automatic token refresh
- Handle token expiration gracefully
- Use connection pooling for better performance
- Implement retry logic with exponential backoff
Error Handling
- Always check response status codes
- Implement proper error handling for all failure cases
- Log errors for debugging (without exposing sensitive data)
- Provide meaningful error messages to users
Performance
- Cache tokens in memory to avoid unnecessary exchanges
- Use appropriate timeouts for API calls
- Implement circuit breakers for resilience
- Monitor token exchange success rates
Next Steps
Now that you understand how to obtain and use tokens, you can:- Create API Keys if you haven’t already
- Integrate token-based authentication into your applications
- Explore the API Reference for available endpoints
- Set up monitoring and alerting for your API usage