# How do I cache a m2m token in a Auth0 Action

**URL:** <https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664>\
**Category:** Get Help\
**Tags:** node, actions\
**Created:** [July 26, 2022, 2:37pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664 "2022-07-26T14:37:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgleason](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jgleason/32/25904_2.png) [@jgleason](https://community.auth0.com/u/jgleason)\
**Post date:** [July 26, 2022, 2:37pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664/1 "2022-07-26T14:37:46Z")

</div>

I am using a node action. It needs to call another service I have that caches some claim information…

```javascript
const axios = require('axios').default;

const getToken = async (secret, audience, scope)=>{
  const tokenResult = await axios.post('https://.../oauth/token', {
    "grant_type": "...",
    "client_id" : "...",
    "client_secret": secret,
    audience,
    "scope":"service:identity"
  });
  return tokenResult.data["access_token"]
}

const getCorp = async (email, domain, secret)=>{
  const token = await getToken(secret, "...", "...")
  const corpResult = await axios.get(`${domain}/corporate?email=${email}`, {
    headers: {
      "Authorization": "Bearer "+token
    }
  });
  console.log("What we get back is ", corpResult.data);
  return corpResult.data;
}

exports.onExecutePostLogin = async (event, api) => {
  console.log("Running...");
  if (event.authorization && event.user.email != null){
    ...
    corp = await getCorp(event.user.email, event.secrets.API_DOMAIN, event.secrets.CLIENT_SECRET);
    ...
  }
};

```

This works but since the action can’t cache the value it requests a new M2M token every time. Is there a way to cache the token so that I am only getting a new one when I need to (expiry, etc)?

---

<div class="post-metadata">

**Author:** ![jgleason](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jgleason/32/25904_2.png) [@jgleason](https://community.auth0.com/u/jgleason)\
**Post date:** [July 26, 2022, 2:41pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664/2 "2022-07-26T14:41:21Z")

</div>

Based on this answer ([Caching resources in Actions](https://community.auth0.com/t/caching-resources-in-actions/57540)) and this documentation ([Cache Expensive Resources in Rules](https://auth0.com/docs/customize/rules/cache-resources)) it looks like I need to use Rules instead?

---

<div class="post-metadata">

**Author:** ![gparascandolo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gparascandolo/32/38788_2.png) [@gparascandolo](https://community.auth0.com/u/gparascandolo)\
**Post date:** [July 29, 2022, 1:49pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664/3 "2022-07-29T13:49:45Z")

</div>

@jgleason You are correct, there is currently no way to cache tokens in Actions, you will need to use Rules for now to do what you are looking for.

This is a common ask from the community. There is a [feedback post](https://community.auth0.com/t/add-rate-limiting-and-cache-for-m2m-token-authentication-endpoints/75367) (although not limited to token caching in just Auth0 Actions) that seems to fit your needs. You can vote on the post and let Auth0 know it is something you are looking for as well and hopefully they can implement a solution for you soon.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 29, 2022, 3:23pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664/4 "2022-07-29T15:23:40Z")

</div>

Thanks for jumping in @gparascandolo! @jgleason Please do upvote the feedback request, like @gparascandolo this is a common ask and the more engagement we can get on the request the better 🤞

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 13, 2022, 3:24pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664/5 "2022-08-13T15:24:31Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 28, 2022, 2:22pm UTC](https://community.auth0.com/t/how-do-i-cache-a-m2m-token-in-a-auth0-action/87664/6 "2022-11-28T14:22:29Z")

</div>

Update: We recently released [Actions Caching Is Now Available](https://auth0.com/blog/actions-caching-is-now-available/) which should solve for this.
