Skip to main content
Access tokens are short-lived credentials that provide authenticated access to Sawmills services. This guide explains how to exchange your API key for an access token and use it for authentication.

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:
  1. Use your API key to request an access token
  2. Use the access token to authenticate with other Sawmills services
  3. 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 the ApiKey scheme:

Request Body

The request body should be empty:

Response

Response Fields:
  • access_token: JWT token for authenticating with other services
  • token_type: Always “Bearer” for Sawmills tokens
  • expires_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 the Bearer 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 Key
401 Unauthorized - Revoked API Key
401 Unauthorized - Invalid Format
401 Unauthorized - Missing Authorization
401 Unauthorized - Invalid Scheme
500 Internal Server Error - Token Issuance Failed

Handling 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
“Authorization header must use ApiKey scheme”
  • Use ApiKey (not Bearer) when exchanging API keys for tokens
  • Format: Authorization: ApiKey YOUR_API_KEY
“Invalid API key credentials”
  • Verify your API key is correct and active
  • Check that the API key hasn’t been revoked
  • Ensure the API key is correctly formatted
“Failed to mint access token”
  • This is a server-side error
  • Check Sawmills service status
  • Contact support if the issue persists

Debugging Tips

  1. Verify API Key: Ensure your API key is correctly formatted
  2. Check Token Expiry: Verify the token hasn’t expired before using it
  3. Test with cURL: Use cURL to test token exchange independently
  4. 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:
  1. Create API Keys if you haven’t already
  2. Integrate token-based authentication into your applications
  3. Set up monitoring and alerting for your API usage