automation // REST APILAB

OAuth Token Cache Pattern

Reuse Marketing Cloud access tokens instead of creating a new token for every API request.

apioauthtokencache
Context Pack →Open Workspace →
Trust & provenanceLAB
Evidence tierLABOfficial sources1Other sources0Last verified2026-10-05

Evidence tier describes my hands-on relationship with the content. Official sources describe platform contracts/documentation. Production observations should not automatically be treated as official limits.

EvidenceLAB
Difficultyintermediate
Verified2026-10-05
Use when

Building an external integration, n8n custom flow or service that calls SFMC repeatedly.

async function getToken() {
  if (cache.token && Date.now() < cache.expiresAt - 60_000) {
    return cache.token;
  }

  const auth = await requestNewToken();
  cache.token = auth.access_token;
  cache.expiresAt = Date.now() + (auth.expires_in * 1000);
  return cache.token;
}
Why it works

Salesforce recommends avoiding frequent token creation and specifically advises not requesting more than one access token every 20 minutes.

Operational notes

Keep client_secret on the server/secure credential store.

Do not copy this in-browser; it is service-side pseudocode.

Sources / verification