# Obtaining connection metadata in Auth0 rule

**URL:** <https://community.auth0.com/t/obtaining-connection-metadata-in-auth0-rule/9622>\
**Category:** Get Help\
**Tags:** connection-metadata, connection, rule\
**Created:** [March 1, 2018, 9:51am UTC](https://community.auth0.com/t/obtaining-connection-metadata-in-auth0-rule/9622 "2018-03-01T09:51:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob.wang](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@jacob.wang](https://community.auth0.com/u/jacob.wang)\
**Post date:** [March 1, 2018, 9:51am UTC](https://community.auth0.com/t/obtaining-connection-metadata-in-auth0-rule/9622/1 "2018-03-01T09:51:21Z")

</div>

I went through the docs as well as checked the available methods on the `auth0` management object available in rules - there doesn’t seem to be a way to access the connection metadata in the rule.

What I’m trying to do:  
We have multiple Salesforce Community connections (and our customers can create their own). In the JWT we need a claim stating which community this user logged in to (as the same Salesforce user can belong to multiple communities).  
The simplest solution I’m trying to implement now is to add the community ID to the custom connection’s metadata, and in the rules we will fetch it from the connection metadat put it in the JWT claims.

Is there a way to access connection metadata in rules?

---

<div class="post-metadata">

**Author:** ![richard.dowinton](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/richard.dowinton/32/16150_2.png) [@richard.dowinton](https://community.auth0.com/u/richard.dowinton)\
**Post date:** [March 8, 2018, 5:36pm UTC](https://community.auth0.com/t/obtaining-connection-metadata-in-auth0-rule/9622/2 "2018-03-08T17:36:33Z")

</div>

You can [read connection metadata](https://auth0.com/docs/api/management/v2#!/Connections/get_connections) via the management API.

While it’s possible to use the management API with the access token provided by `auth0.access_token` as described [here](https://auth0.com/docs/rules/current/management-api), it lacks the scope (`read:connections`) you need to read the connection metadata.

For this reason, you will need to [request an access token](https://auth0.com/docs/api/management/v2/tokens#automate-the-process) via the `client_credentials` grant in the rule. You should create a specific non-interactive client for this and grant it only the `read:connections` scope.

```auto
...
  var request = require('request@2.56.0');
  request.post({
    url: 'https://your-tenant.auth0.com/oauth/token',
    headers: {
      'Content-Type': 'application/json'
    },
    json: {
      'client_id': '...',
      'client_secret': '...',
      'audience': 'https://your-tenant.auth0.com/api/v2/',
      'grant_type': 'client_credentials',
      'scope': 'read:connections'
    }
  },
  function(err, response, body) {
    if (err) {
      return callback(err);
    }
    if (response.statusCode !== 200) {
      return callback(new Error(body));
    }
    
    var accessToken = body.accessToken;
...

```

You can then retrieve the connection metadata with the access token:

```auto
...
    request({
      url: 'https//your-tenant.auth0.com/api/v2/connections',
      headers: {
        Authorization: 'Bearer ' + accessToken
      },
      qs: {
        'name': context.connection
      }
    }
...

```

---

<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:** [August 28, 2019, 2:38pm UTC](https://community.auth0.com/t/obtaining-connection-metadata-in-auth0-rule/9622/5 "2019-08-28T14:38:59Z")

</div>

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