# Can I get the access token as a string in a Rule or Action?

**URL:** <https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978>\
**Category:** JWT.io\
**Tags:** action, rule, auth0\
**Created:** [August 18, 2022, 3:20pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978 "2022-08-18T15:20:54Z")\
**Posts on this page:** 10\
**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:** [August 18, 2022, 3:20pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/1 "2022-08-18T15:20:54Z")

</div>

I have the following rule…

```auto
async function (user, context, callback) {
  const axios = require('axios').default;
  const corp = await getCorp(user.email);
  context.accessToken[`https://.../api/corp`] =corp;
  callback(null, user, context);
  async function getCorp(email){
    const token = `Bearer ${context.accessToken}`;
    console.log(`The token is ${token}`);
    const corpResult = await axios.get(`${configuration.domain}/corporate?email=${email}`, {
      headers: {
        "Authorization": token
      }
    });
    return corpResult.data;
  }
}

```

But the token isn’t a string it is an object. I know this is because things can still be added but I need a way to use it to call the DB to get additional user info for claims. Is there a way to do this without leaving it open?

---

<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:** [August 18, 2022, 3:24pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/2 "2022-08-18T15:24:44Z")

</div>

Does this lead me to believe this isn’t possible?

> [@Auth0 Rules: access to access token from inside Rule code](https://community.auth0.com/t/auth0-rules-access-to-access-token-from-inside-rule-code/24609/7):
>
> Zaid, I got it working! An Auth0 rule that acts on first time sign in user. It makes an API call to get a machine to machine token. It uses the token to call my API so it can register the new user in our database. I found this example that was a big help in getting the Rule to work. [https://github.com/auth0/rules/blob/master/src/rules/parse.js](https://github.com/auth0/rules/blob/master/src/rules/parse.js) Ignore the context.access\_token. You need to create your own. Make sure you use the client\_id, client\_secret and audience of your machine to machine d…

---

<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:** [August 18, 2022, 3:25pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/3 "2022-08-18T15:25:40Z")

</div>

I had the m2m account working but because I couldn’t cache it used up my access tokens really quick. Also I would prefer to just delegate the token.

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [August 19, 2022, 1:06pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/4 "2022-08-19T13:06:51Z")

</div>

Hi @jgleason

The context.accessToken is the access token you are returning to the user. When you are in the middle of the rules, the access token is not yet complete (the user is not fully authorized). Consider: the next rule in the chain could fail the login, so the partial access token is not valid. But you are trying to use it anyway, So, you need your own M2M access token instead.

You can cache M2M tokens in the rule, using the rule config.

John

---

<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:** [September 1, 2022, 1:40pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/5 "2022-09-01T13:40:46Z")

</div>

@john.gateley Can you provide an example of how to cache the token in the rule? I tried to use global but it still got reset everytime.

---

<div class="post-metadata">

**Author:** ![lucas.gonzalez](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lucas.gonzalez/32/33884_2.png) [@lucas.gonzalez](https://community.auth0.com/u/lucas.gonzalez)\
**Post date:** [September 6, 2022, 7:26pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/6 "2022-09-06T19:26:14Z")

</div>

How can I request an M2M access token in an action? Do you have any examples?  
Thanks in advance!  
@john.gateley

---

<div class="post-metadata">

**Author:** ![sandT](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@sandT](https://community.auth0.com/u/sandT)\
**Post date:** [February 15, 2023, 11:59pm UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/7 "2023-02-15T23:59:01Z")

</div>

I have a similar issue. I have a managed api token that I want to call in my rule without hardcoding it. any idea how I can make it happen?

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [January 8, 2024, 8:42am UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/9 "2024-01-08T08:42:57Z")

</div>

Hey there!

As this topic is related to Rules - Hooks - Actions and Rules & Hooks are being deprecated soon I’m excited to let you know about our next [Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043) on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

[Learn more here!](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043)

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [May 15, 2024, 9:46am UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/10 "2024-05-15T09:46:33Z")

</div>

Hi there!  
We’re making an effort to plum a bit our backlog and wanted to update this topic for any future users ending up here:

Getting an access token for the registered API can be done with the registered machine-to-machine application credentials (client credentials exchange). It is well explained here: [Call Your API Using the Client Credentials Flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/client-credentials-flow/call-your-api-using-the-client-credentials-flow)

Example usage of the flow in Actions (getting / modifying access token): [Machine to Machine Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/machine-to-machine-flow).

We also have an article on caching the Management API token in Actions - but please treat it as an outline that needs to be well-tested and updated to your needs: [Caching Management API Access Tokens in Login Action](https://community.auth0.com/t/caching-management-api-access-tokens-in-login-action/95855)

Thanks,  
Marcelina

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [May 15, 2024, 9:46am UTC](https://community.auth0.com/t/can-i-get-the-access-token-as-a-string-in-a-rule-or-action/88978/11 "2024-05-15T09:46:42Z")

</div>


