# Client Grants: How can I grant API authorization while generating new M2M so I don't have to authorize it manually?

**URL:** <https://community.auth0.com/t/client-grants-how-can-i-grant-api-authorization-while-generating-new-m2m-so-i-dont-have-to-authorize-it-manually/115671>\
**Category:** Get Help\
**Tags:** clients, management-api\
**Created:** [September 7, 2023, 4:17pm UTC](https://community.auth0.com/t/client-grants-how-can-i-grant-api-authorization-while-generating-new-m2m-so-i-dont-have-to-authorize-it-manually/115671 "2023-09-07T16:17:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruni-WanKenobi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bruni-wankenobi/32/50643_2.png) [@Bruni-WanKenobi](https://community.auth0.com/u/Bruni-WanKenobi)\
**Post date:** [September 7, 2023, 4:17pm UTC](https://community.auth0.com/t/client-grants-how-can-i-grant-api-authorization-while-generating-new-m2m-so-i-dont-have-to-authorize-it-manually/115671/1 "2023-09-07T16:17:39Z")

</div>

Hey Community!

I hope everything is going well.

**Background:**

I have an M2M application that generates new M2Ms for API consumption. The newly created M2M needs to have my API identifier grants.

I am receiving this error:

```auto
The client is not authorized to access "API-IDENTIFIER". You need to create a \"client-grant\" associated with this API

```

- I know I can manually change it by going to the dashboard.

**Question:**

How can I grant API authorization while generating new M2M so I don’t have to authorize it manually?

```auto
export const postApiRegistration = async (registrationDetails): Promise<Auth0Connection> => {
    const token = await getAuth0ManagementToken();
    const url = `${AUTH0_BASE_URL}/api/v2/clients`;
    const DATAHUB_API_ID = 'DATAHUB_API_ID ';
    

    let data = JSON.stringify({
        name: registrationDetails.AppName,
        description: `Auth0 Generic Api registration ${registrationDetails.CustomerCode}`,
        callbacks: [],
        client_aliases: [],
        allowed_clients: [`${DATAHUB_API_ID}`],
        grant_types: ['client_credentials'],
        token_endpoint_auth_method: 'client_secret_post',
        app_type: 'non_interactive',
        is_first_party: true,
        oidc_conformant: false,
        jwt_configuration: {
            lifetime_in_seconds: 36000,
            scopes: {},
            alg: 'RS256',
            secret_encoded: false
        },
        cross_origin_authentication: false,
        sso_disabled: false,
        custom_login_page_on: true,
        native_social_login: {
            apple: {
                enabled: false
            },
            facebook: {
                enabled: false
            }
        },
        refresh_token: {
            expiration_type: 'non-expiring',
            leeway: 0,
            infinite_token_lifetime: true,
            infinite_idle_token_lifetime: true,
            token_lifetime: 31557600,
            idle_token_lifetime: 2592000,
            rotation_type: 'non-rotating'
        }
    });

    const options: AxiosRequestConfig = {
        url: url,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json',
            Authorization: `Bearer ${token.access_token}`
        },
        data: data
    };

    const connectionsResponse = await axios.request<Auth0Connection>(options);

    return connectionsResponse.data;
};

```

---

<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:** [September 7, 2023, 5:17pm UTC](https://community.auth0.com/t/client-grants-how-can-i-grant-api-authorization-while-generating-new-m2m-so-i-dont-have-to-authorize-it-manually/115671/3 "2023-09-07T17:17:36Z")

</div>

Hi @Bruni-WanKenobi,

Thanks for posting your question on the Community!

> [@Bruni-WanKenobi](#):
>
> How can I grant API authorization while generating new M2M so I don’t have to authorize it manually?

You can do so by calling the Management API’s [Create a client grant endpoint](https://auth0.com/docs/api/management/v2/client-grants/post-client-grants).

After you have created your M2M application, you can grant it API authorization by passing in the `client_id`, `audience`, and `scope` parameters in the request.

For example:

```auto
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Accept", "application/json");

var raw = JSON.stringify({
  "client_id": "YOUR_M2M_CLIENT_ID",
  "audience": "YOUR_API_IDENTIFIER",
  "scope": [
    "YOUR:SCOPES"
  ]
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://login.auth0.com/api/v2/client-grants", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

```

If you found this post helpful or interesting, please give it a like 👍 . Your interaction makes a difference. **Have a wonderful day!** 🌞

Thanks,  
Rueben

* * *

[📹 _Prefer how-to videos instead of written docs? We’ve got you covered! Check out our OktaDev YouTube channel for those helpful resources!_](https://www.youtube.com/@OktaDev/videos)

---

<div class="post-metadata">

**Author:** ![Bruni-WanKenobi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bruni-wankenobi/32/50643_2.png) [@Bruni-WanKenobi](https://community.auth0.com/u/Bruni-WanKenobi)\
**Post date:** [September 7, 2023, 6:49pm UTC](https://community.auth0.com/t/client-grants-how-can-i-grant-api-authorization-while-generating-new-m2m-so-i-dont-have-to-authorize-it-manually/115671/4 "2023-09-07T18:49:38Z")

</div>

Thank you @rueben.tiow

---

<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:** [September 21, 2023, 6:49pm UTC](https://community.auth0.com/t/client-grants-how-can-i-grant-api-authorization-while-generating-new-m2m-so-i-dont-have-to-authorize-it-manually/115671/5 "2023-09-21T18:49:50Z")

</div>

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