# Access token for machine-to-machine API

**URL:** <https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [November 28, 2022, 10:40pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256 "2022-11-28T22:40:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tms](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tms/32/16540_2.png) [@tms](https://community.auth0.com/u/tms)\
**Post date:** [November 28, 2022, 10:40pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/1 "2022-11-28T22:40:50Z")

</div>

I’m confused about how to configure a “Custom API” on my tenant dashboard so that it can make calls to the “Auth0 Management API”. For example, I want my API to collect “users” from the “Auth0 Management API”.

I’ve added a special “Application” on my tenant dashboard whose only role is to provide a client\_id and client\_secret so that I can perform a POST to get an access token from Auth0. This Application has no other use for me. I store the resulting token in the local state of my backend service and use that to access the “Auth0 Management API”. It looks like I need to handle token expiration and so on, since I expect my backend to run for long periods (more than a day) and I currently only perform this access at startup.

Is this the preferred/only way to get an access token that my backend service can use to access the “Auth0 Management API”? I was hoping for a mechanism that I could enable once at configuration/setup and then leave alone.

Thanks,  
Tom S.

---

<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:** [November 29, 2022, 1:10am UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/3 "2022-11-29T01:10:41Z")

</div>

Hey there @tms!

> [@tms](#):
>
> I’m confused about how to configure a “Custom API” on my tenant dashboard so that it can make calls to the “Auth0 Management API”. For example, I want my API to collect “users” from the “Auth0 Management API”.

Assuming your backend is requesting management API tokens, you don’t need to configure another API in your dashboard. The `audience` is the management API identifier itself, so `https://YOUR_DOMAIN/api/v2/`

[https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production#get-access-tokens](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production#get-access-tokens)

---

<div class="post-metadata">

**Author:** ![tms](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tms/32/16540_2.png) [@tms](https://community.auth0.com/u/tms)\
**Post date:** [November 29, 2022, 4:12pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/4 "2022-11-29T16:12:17Z")

</div>

Here is the request I make (using an Application created for just this) that works:

```auto
const requestData = `{"client_id":"<client_id>","client_secret":"<client_secret>","audience":"https://0-0-zeetix.us.auth0.com/api/v2/","grant_type":"client_credentials"}`
const url = `https://0-0-zeetix.us.auth0.com/oauth/token`;
const tokenOptions = {
  method: 'POST',
  url: url,
  headers: { 'content-type': 'application/json' },
  data: requestData
};
response = await axios.request(tokenOptions)

```

I tried removing the `client_id` and `client_secret` parameters as per your response, as follows:

```auto
const requestData = `{"audience":"https://0-0-zeetix.us.auth0.com/api/v2/","grant_type":"client_credentials"}`
const url = `https://0-0-zeetix.us.auth0.com/oauth/token`;
const tokenOptions = {
  method: 'POST',
  url: url,
  headers: { 'content-type': 'application/json' },
  data: requestData
};
response = await axios.request(tokenOptions)

```

When I run the latter, I get an error response:

```auto
"Request failed with status code 401"

```

It may be irrelevant, but I also notice that in the “Machine to Machine Applications” tab of the “API” section of my tenant dashboard, the only entries are for “Applications” (with a client\_id and client\_secret").

I fear I misunderstand something basic here, so please tolerate my confusion.

---

<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:** [November 29, 2022, 5:54pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/5 "2022-11-29T17:54:09Z")

</div>

Thanks for following up!

As you’ve noticed, you will need client credentials (client id + secret) in order perform a client credentials exchange for an access token. I was referring to the fact that you don’t need to create another API in your dashboard - You do however need to have a m2m application created (where client ID and secret are generated) AND authorize that app in the Management API settings of your dashboard. This is done in the Machine to Machine Applications tab that you mentioned.

The following documentation outlines the concept of using an M2M app to to call an API - The only difference is that instead of calling your own API, you are using the M2M app to retrieve an access token to call the Auth0 Management API.

> **[Call Your API Using the Client Credentials Flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-the-client-credentials-flow)**
>
> Learn how to call your API from a machine-to-machine (M2M) application using the Client Credentials Flow.

---

<div class="post-metadata">

**Author:** ![tms](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tms/32/16540_2.png) [@tms](https://community.auth0.com/u/tms)\
**Post date:** [November 29, 2022, 8:02pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/6 "2022-11-29T20:02:49Z")

</div>

I don’t need to create another API in the dashboard, but it appears that I do need to create a separate Application – of type Machine-To-Machine – whose only function is to provide a client\_id and client\_secret.

I have a backend API. I already have an Application of type SPA. When I use the client\_id and client\_secret of that SPA Application, I get a 403 complaint in response to the token request.

I therefore gather that each API I create (I have two so far) requires a Machine-To-Machine Application instance, and I then need an additional SPA Application for each front-end that I use.

I’m not complaining, I’m just trying to mirror my learnings.

It would be awesome if there was a single documentation URL where these sorts of constraints were presented in a more organized way.

---

<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:** [November 29, 2022, 10:25pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/7 "2022-11-29T22:25:19Z")

</div>

Thanks for following up @tms!

> [@tms](#):
>
> I have a backend API. I already have an Application of type SPA. When I use the client\_id and client\_secret of that SPA Application, I get a 403 complaint in response to the token request.

It is not recommended that SPAs request management API tokens for reasons outlined [here](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-tokens-for-single-page-applications#limitations). The M2M approach essentially allows your backend to proxy requests on behalf of your SPA, keeping the client id and secret safe. This use case is outline here:

[https://community.auth0.com/t/how-can-i-enable-users-to-change-their-email-address-from-a-spa-or-native-app/44064](https://community.auth0.com/t/how-can-i-enable-users-to-change-their-email-address-from-a-spa-or-native-app/44064)

> [@tms](#):
>
> I therefore gather that each API I create (I have two so far) requires a Machine-To-Machine Application instance, and I then need an additional SPA Application for each front-end that I use.

You should be able to utilize a single M2M app and authorize it for multiple APIs.

> [@Best practice to set up one m2m app and multiple APIs](https://community.auth0.com/t/best-practice-to-set-up-one-m2m-app-and-multiple-apis/73135/3):
>
> Hi @domasr, Welcome to the Auth0 Community! Generally, you would have one application. The application requests tokens from the auth server and uses the tokens to request resources from the API. I don’t see the benefit of multiple M2M applications in this scenario, but I would be happy to explore further if you provide more detail.

---

<div class="post-metadata">

**Author:** ![tms](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tms/32/16540_2.png) [@tms](https://community.auth0.com/u/tms)\
**Post date:** [November 29, 2022, 10:43pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/8 "2022-11-29T22:43:11Z")

</div>

Ah, got it – a single M2M app authorized to provide a token for each API.

That makes all kinds of sense.

---

<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:** [December 13, 2022, 10:43pm UTC](https://community.auth0.com/t/access-token-for-machine-to-machine-api/95256/9 "2022-12-13T22:43:16Z")

</div>

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