# How to get org\_id as part of access token?

**URL:** <https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703>\
**Category:** Get Help\
**Tags:** rules, auth0\
**Created:** [March 11, 2022, 4:43am UTC](https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703 "2022-03-11T04:43:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![selvi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/selvi/32/33945_2.png) [@selvi](https://community.auth0.com/u/selvi)\
**Post date:** [March 11, 2022, 4:43am UTC](https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703/1 "2022-03-11T04:43:49Z")

</div>

- I am try to get get org\_id While generating using UserNamePassword authentication?
- I have added rule as below  
**context.accessToken[namespace + ‘org’] = context.organization;**
- After generating access token and I have decoded it and I couldn’t find any org\_id in json

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 11, 2022, 8:10pm UTC](https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703/3 "2022-03-11T20:10:08Z")

</div>

Hi @selvi,

Thanks for reaching out to the Auth0 Community!

I understand you want to add the `org_id` as a custom claim to your access token.

To do so, please make sure your Rule looks something like the following:

```auto
function (user, context, callback) {
  
  const namespace = 'https://example.com';
  context.accessToken[`${namespace}/org`] = context.organization.id;

  callback(null, user, context);
}

```

Once that is complete, you will get the `org_id` in the Access Token. I have tested this and can confirm that it works.

Please let me know if there’s anything else I can do to help.

Thank you.

---

<div class="post-metadata">

**Author:** ![selvi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/selvi/32/33945_2.png) [@selvi](https://community.auth0.com/u/selvi)\
**Post date:** [March 14, 2022, 5:13am UTC](https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703/4 "2022-03-14T05:13:36Z")

</div>

Tried your solution. Refer the curl command and the error Which I got while trying to get org\_id. I tried to print context **(console.log(context)😉** Refer the context response below, but I don’t find organization information under the context.

// context response

```auto
 {
tenant: 'dev-dsadsaf',
clientID: 'fsadSFA',
clientName: 'spring-with-auth0',
clientMetadata: {},
connection: 'Username-Password-Authentication',
connectionStrategy: 'auth0',
connectionID: 'con_5ivApE7L1v3Ef9UD',
connectionOptions: {},
connectionMetadata: {},
samlConfiguration: {},
jwtConfiguration: {},
protocol: 'oauth2-password',
stats: { loginsCount: 89 },
accessToken: {},
idToken: {},
locale: 'en',
request: {
userAgent: 'curl/7.58.0',
ip: '2409:4072:109:1233:5c80:2758:7910:4672',
hostname: 'devQW1.us.auth0.com',
body: {
grant_type: 'password',
username: 'USERNAME',
audience: 'https://dev-sewaewqre.us.auth0.com/api/v2/',
client_id: '743r24twrt',
scope: 'openid',
client_secret: '56Euiegongdndni',
connection: 'Username-Password-Authentication'
},
geoip: {
country_code: 'IN',
country_code3: 'IND',
country_name: 'India',
city_name: 'Chennai',
latitude: 12.8996,
longitude: 80.2209,
time_zone: 'Asia/Kolkata',
continent_code: 'AS'
}
},
authorization: { roles: ['role-a'] }
}
{
hobby: 'surfing',
preferred_contact: 'email',
favorite_color: 'blue',
tenant_name: 'dev-dsadsaf'
}
{ plan: 'full' }

```

// code  
context.accessToken[`${namespace}/org`] = context.organization.id;

```auto
Command:
 curl --request POST --url 'https://dev-uuuu.us.auth0.com/oauth/token' --header 'content-type: application/x-www-form-urlencoded' --data grant_type=password --data username=username --data password=pwd --data audience=https://dev-uus.auth0.com/api/v2/ --data 'client_id=savdhsvfdvf' --data scope=openid profile user_metadata app_metadata --data client_secret=5rerheu

Error:
{"error":"access_denied","error_description":"Cannot read property 'id' of undefined"}

```

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 14, 2022, 9:24pm UTC](https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703/5 "2022-03-14T21:24:43Z")

</div>

Hi @selvi,

Thank you for your response.

The reason why the **context** object does not contain the **organization.id** property is because the users did not authenticate through an organization.

To address this, you will need to authenticate users through an organization, and then the organization.id can be appended as a custom claim in the Access Token.

For example:

```auto
https://YOUR_DOMAIN/authorize?
    response_type=code&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://YOUR_APP/callback&
    scope=SCOPE&
    audience=API_AUDIENCE&
    state=STATE&
    organization=YOUR_ORG_ID

```

You can read our [https://auth0.com/docs/manage-users/organizations/using-tokens#authenticate-users-through-an-organization](https://auth0.com/docs/manage-users/organizations/using-tokens#authenticate-users-through-an-organization) doc to learn more.

Please do not hesitate to reach out if you have any additional questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 29, 2022, 9:25pm UTC](https://community.auth0.com/t/how-to-get-org-id-as-part-of-access-token/79703/6 "2022-03-29T21:25:29Z")

</div>

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