# Having trouble calling the Management API from my Node backend using an M2M token

**URL:** <https://community.auth0.com/t/having-trouble-calling-the-management-api-from-my-node-backend-using-an-m2m-token/88754>\
**Category:** Get Help\
**Tags:** nodejs, m2m\
**Created:** [August 15, 2022, 6:35am UTC](https://community.auth0.com/t/having-trouble-calling-the-management-api-from-my-node-backend-using-an-m2m-token/88754 "2022-08-15T06:35:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [August 15, 2022, 6:35am UTC](https://community.auth0.com/t/having-trouble-calling-the-management-api-from-my-node-backend-using-an-m2m-token/88754/1 "2022-08-15T06:35:35Z")

</div>

Hi!

I’m curious what is a working way to get my SPA (front-end) + Node (back-end) setup to call the Management API using an M2M token without generating a CORS error?

Using the Auth0 [tutorials](https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-the-client-credentials-flow?_ga=2.215391060.295499598.1660404994-1763337307.1660266811&_gl=1*1t6l3ch*rollup_ga*MTc2MzMzNzMwNy4xNjYwMjY2ODEx*rollup_ga_F1G3E656YZ*MTY2MDQ4NzIyMS45LjEuMTY2MDQ4ODI5MC42MA..#sample-use-cases), I can authenticate with Auth0 and also get the M2M token without problem. However, when I try to call the Management API using the received M2M token, I get a CORS error:

![image](https://global.discourse-cdn.com/auth0/original/3X/3/6/36a78aa479f2f61ef44e7f986f16028dc0cc8ba4.png)

This occurs both when testing locally (with React-UI running on port 3000, and Node server running on port 5000) as well as after building and running it from Heroku, which is strange. I have put the origin domain into the `Allowed Web Origins` and `Allowed Origins (CORS)` boxes in the applications Auth0 settings. I also put the domain in the `Access-Control-Allow-Origin` headers.

Here is the GET route I set up in my server.js (backend) file:

> app.get(“/M2M”, async (req, res) =\> {  
> try {  
> const apiResponse = await fetch(  
> “[https://dev-7-8i89hb.us.auth0.com/api/v2/](https://dev-7-8i89hb.us.auth0.com/api/v2/)”,  
> {  
> method: “POST”,  
> headers: { “content-type”: “application/json” },  
> body: ‘{“client\_id”:“6J2cpQGzD456WzodmDHXj4Kot4y84bgI”,“client\_secret”:“XXXXXXXXXXXXXXXXXXXXXXXXXXX”,“audience”:“[https://dev-7-8i89hb.us.auth0.com/api/v2/",“grant\_type”:"client\_credentials](https://dev-7-8i89hb.us.auth0.com/api/v2/%22,%22grant_type%22:%22client_credentials)”}’,  
> }  
> );  
> const apiResponseJson = await apiResponse.json();  
> console.log(apiResponseJson);  
> res.send(“Done – check console log”);  
> } catch (err) {  
> console.log(err);  
> res.status(500).send(“Something went wrong”);  
> }  
> });

And here is my setup to call the API from the frontend after receiving the M2M token (src/app.js):

> const GetM2Mdata = () =\> {  
> const { user, isAuthenticated } = useAuth0();
> 
> const [visible, setVisible] = useState(“false”);
> 
> var request = require(“request”);
> 
> var options = {  
> method: “POST”,  
> url: “[https://dev-7-8i89hb.us.auth0.com/oauth/token](https://dev-7-8i89hb.us.auth0.com/oauth/token)”,  
> headers: { “content-type”: “application/json”,  
> “Access-Control-Allow-Origin”: “[http://localhost:3000](http://localhost:3000)”,  
> },  
> body: ‘{“client\_id”:“6J2cpQGzD456WzodmDHXj4Kot4y84bgI”,“client\_secret”:“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”,“audience”:“[https://dev-7-8i89hb.us.auth0.com/api/v2/",“grant\_type”:"client\_credentials](https://dev-7-8i89hb.us.auth0.com/api/v2/%22,%22grant_type%22:%22client_credentials)”}’,  
> };
> 
> const optionsM2M = {  
> method: “GET”,  
> url: “[https://dev-7-8i89hb.us.auth0.com/api/v2/](https://dev-7-8i89hb.us.auth0.com/api/v2/)”,  
> headers: {  
> “content-type”: “application/json”,  
> authorization: “Bearer ACCESS\_TOKEN”,  
> “Access-Control-Allow-Origin”: “[http://localhost:3000/](http://localhost:3000/)”  
> },  
> };
> 
> request(options, function (error, response, body) {  
> if (error) throw new Error(error);
> 
> ```
> console.log(body);
> 
> ```
> 
> });
> 
> const getUsersData = function () {  
> axios  
> .request(optionsM2M)  
> .then(function (response) {  
> console.log(response.data);  
> })  
> .catch(function (error) {  
> console.error(error);  
> });  
> };
> 
> return isAuthenticated ? (
> 
> ```
> <button class="buttonProfile" onClick={() => getUsersData()}>
> Get Users
> </button>
> 
> </div>
> 
> ```
> 
> ) : (  
> “”  
> );  
> };  
> export default GetM2Mdata;

Again, the M2M token is received appropriately. On clicking the button in app.js, I would expect my back-end route to be called and using the received M2M token, to receive data from the Management API.

Other things I’ve tried include writing Actions/ Rules to automate getting the M2M token or using the M2M token to call the Management API. However, setting up and using a Rule get the M2M token still results in the CORS error (even when building from Heroku), while Actions doesn’t seem to be able to handle external modules. Hence, I can’t seem to use Fetch/Axios/etc. with Actions.

Also, as a further side question- but how is it possible to have both regular a Management API as well as an M2M client installed in the same SPA? It would appear that wrapping the root with more than one Auth0 clientId would almost certainly cause problems?

Any help would be sincerely appreciated; I’ve been stuck on this issue for a few days now.

Thanks,

Rob

@ty.frith @rueben.tiow

* * *

> [tyf](https://community.auth0.com/u/tyf)[Auth0 Employee](https://community.auth0.com/g/Auth0_Employees)
> 
> [26d](https://community.auth0.com/t/is-the-only-way-to-obtain-a-management-api-token-via-copy-and-paste/86908/7)
> 
> No worries! Good to know that was helpful 😄
> 
> > [@Is the only way to obtain a Management API token via copy and paste?](https://community.auth0.com/t/is-the-only-way-to-obtain-a-management-api-token-via-copy-and-paste/86908/6):
> >
> > So basically, we have to choose between using a normal SPA or an M2M/Management API for our app, is this correct?
> 
> You will need both - The SPA app configured in Auth0 will be specific to your React App/client side. On the other hand, you will have a M2M app configured in Auth0 specific to your own API/backend. The flow would look like this:
> 
> - User logs in to your React/SPA app.
> - User goes to perform something (get all users for instance) that requires Management API use.
> - **This is really where the magic happens** - Instead of your React app communicating directly with the Management API, it will communicate with your own API/Backend instead. Your API/Backend should take the query, make the necessary call to the Management API and then pass that response back to your React/SPA app.
> 
> The key here is that the backend process (M2M/Client Credentials Exchange) allows the Client Credentials to be abstracted from the SPA (public) app and safely used against the Management API.
> 
> Again, if all you need from the Management API is to get info about the current user and/or update the current user [metadata](https://auth0.com/docs/manage-users/user-accounts/metadata#manage-metadata), this can be accomplished from the React/SPA app directly as outlined [here](https://auth0.com/docs/quickstart/spa/react/02-calling-an-api).
> 
> Hope this helps!

---

<div class="post-metadata">

**Author:** ![robliou01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robliou01/32/34126_2.png) [@robliou01](https://community.auth0.com/u/robliou01)\
**Post date:** [August 16, 2022, 3:33pm UTC](https://community.auth0.com/t/having-trouble-calling-the-management-api-from-my-node-backend-using-an-m2m-token/88754/2 "2022-08-16T15:33:33Z")

</div>

As a follow-up: I was able to figure things out using the repository given [here](https://github.com/auth0-samples/auth0-api-auth-samples), as well as by using [node-auth0](https://github.com/auth0/node-auth0/blob/master/README.md) to make the requests (I guess it’s nice not having to write out every route).

I’m still not totally successful, but I’ll update if any other questions. Thanks,

---

<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:** [December 21, 2022, 4:05pm UTC](https://community.auth0.com/t/having-trouble-calling-the-management-api-from-my-node-backend-using-an-m2m-token/88754/3 "2022-12-21T16:05:10Z")

</div>


