Caching (Token Caching)

What is token caching?

Token caching is the practice of storing an API token after you mint it so your application can reuse it for later requests.

Bandwidth OAuth 2.0 access tokens expire after one hour. Cache each token until it expires instead of requesting a new token before every API call.

How token caching works

  1. Send a POST request to the Bandwidth token endpoint with your Client ID, your Client Secret, and grant_type=client_credentials.
  2. Read the access_token and expires_in values in the response. An expires_in value of 3600 means the token stays valid for one hour.
  3. Store the token and its expiration time in memory or in a shared cache, such as Redis.
  4. Send the token as a Bearer token in the Authorization header of each API request.
  5. Request a new token 5 to 10 seconds before the cached token expires, then replace the cached copy.

Mint and store tokens on a server in a secure environment. Never place your Client Secret or a token in client-side code.

How Bandwidth is involved with token caching

Bandwidth issues OAuth 2.0 access tokens as JSON Web Tokens (JWTs) through the client credentials flow, the only grant type the token endpoint supports. Bandwidth recommends that you reuse a token for its full lifetime.

Several workers or services can share one token without conflict. Bandwidth also allows more than one valid token to exist for the same Client ID at the same time, with no limit on concurrent tokens.

If you use a current Bandwidth SDK, you can skip the cache code. Provide your Client ID and Client Secret, and the SDK retrieves and refreshes tokens for you.

Bandwidth is retiring legacy API User (Basic Authentication) credentials on December 2, 2026. After that date, OAuth 2.0 client credentials are the supported method.

What are the benefits of token caching?

  • Your application sends one token request per hour instead of one per API call, which removes a network round trip from every request.
  • Workers and services that share a cache reuse the same token, so you avoid duplicate token requests across your fleet.
  • Your Client Secret travels over the network once an hour instead of with every request.

Token caching FAQs

Related:

The information provided in this glossary definition does not, and is not intended to, constitute legal advice, nor does it necessarily represent Bandwidth's products or business practices. This page is for general informational purposes only.
# a b c d e f g h i j k l m n o p q r s t u v w x y z