# Not getting any response from the API I am calling from the Action

**URL:** <https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846>\
**Category:** Get Help\
**Tags:** api\
**Created:** [May 20, 2022, 9:09pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846 "2022-05-20T21:09:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![VP003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vp003/32/33660_2.png) [@VP003](https://community.auth0.com/u/VP003)\
**Post date:** [May 20, 2022, 9:09pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/1 "2022-05-20T21:09:25Z")

</div>

Hello everyone, I am trying to make a POST call to a public API and try to read the response. I have gone through the blog - [How do I call my API from an Action? - Auth0 Community](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041) that explained how to make a call to Auth0 protected API.

However, In my case, the API I am trying to call is a public API. I am not getting any response from the API, nor any error; when running the action.

I have tried calling the API via Postman as well as from c# code and it returns the response successfully each time without any error.

Following is the code snippet of my action:

_const axios = require(‘axios’);_  
_console.log(‘Execution started’);_

_var querystring = require(‘querystring’);_

_axios.post(“https://{Public-domain}/api/Aut0TestAction”,  
querystring.stringify({  
username: ‘username’,  
password: ‘password’,  
}),  
).then((response) =\> {_  
_console.log(response);_  
_}).catch((error) =\> {_  
_console.log(error);_  
_console.log(‘Execution completed’);_

It only displays following in the console log:

```auto
Execution started
Execution completed

```

Do I still need to register the public API and register a new Application for my action?

Any help would be greatly appreciated.

---

<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:** [May 20, 2022, 9:47pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/3 "2022-05-20T21:47:40Z")

</div>

Hi @VP003,

Welcome to the Auth0 Community!

I understand your Action script is not returning any response.

After looking through your code, I noticed that the data you are passing seems to be literal strings, namely “username” and “password”. In this case, I suggest that you store the data as [Action Secrets](https://auth0.com/docs/customize/actions/write-your-first-action#add-a-secret).

Besides that, I did not find anything that stands out to be an issue. However, I suggest that you check whether your API request requires more than just supplying the data, such as defining the headers.

I would strongly suggest following this skeleton for making API calls with Axios:

```auto
var axios = require("axios").default;

var options = {
  method: 'POST',
  url: 'https://YOUR_URL',
  headers: {'content-type': 'application/json'},
  data: {
    username: event.secrets.USERNAME,
    password: event.secrets.PASSWORD
  }
};

axios.request(options).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
  console.error(error);
});

```

Alternatively, you could export the Axios code using Postman and implement it that way into your Action script. To do so, click on the Code Snippet icon located on the right column of the application. Then clicking on the drop-down menu, select `NodeJs -Axios`. See below:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/f/7/f745a699adc67c08db68de0a436b5de7bced0ad4.png)

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

Thank you.

---

<div class="post-metadata">

**Author:** ![VP003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vp003/32/33660_2.png) [@VP003](https://community.auth0.com/u/VP003)\
**Post date:** [May 21, 2022, 8:08pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/4 "2022-05-21T20:08:00Z")

</div>

Thank you Rueben for quick response.

I tried with the skeleton you provided for making API calls with Axios and also tried with the code generated from Postman. However I still don’t see any response or error in the console log.

Following is my updated code snippet for your reference.

```auto
var axios = require("axios").default;

exports.onExecutePostLogin = async (event, api) => {
  
    var options = {
                  method: 'POST',
                  url: 'https://PUBLIC_API_URL',
                  headers: {'content-type': 'application/json'},
                  data: {
                        UserName: event.secrets.USERNAME,
                        Password: event.secrets.PASSWORD
                        }
                  };
    console.log("before API call");
    axios.request(options).then(function (response) {
      console.log(response.data);
    }).catch(function (error) {
      console.error(error);
    });

};

```

Am I missing something here?

---

<div class="post-metadata">

**Author:** ![VP003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vp003/32/33660_2.png) [@VP003](https://community.auth0.com/u/VP003)\
**Post date:** [May 25, 2022, 5:26pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/5 "2022-05-25T17:26:58Z")

</div>

Hello Everyone,  
I noticed that, my Action/Rule is displaying the logs only in ‘Real-time Webtask Logs’. It does not working if I attach to a flow or run with ‘Test’ button.

Anyone from this community facing the same issue calling an API from Action/Rule?

Any help in this regard will be greatly appreciated.

---

<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:** [May 25, 2022, 8:54pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/6 "2022-05-25T20:54:51Z")

</div>

Hi @VP003,

Thank you for your responses.

We had been in communication via DMs and I will be sharing the solution on this thread for consistency.

Firstly, to call an API and set the response as a custom claim in the token, please look at the following code snippet for your use case:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const axios = require('axios');
  
  const namespace = 'https://myapp-example.com';
  const url = 'https://YOUR_URL';
  const options = {
    headers: {
      'Content-Type': 'application/json'
    }
  };
  const data = {
    UserName: event.secrets.USERNAME, 
    Password: event.secrets.PASSWORD 
  };
  var response = await axios.post(url, data, options);
  console.log(response.data);
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/CUSTOM_CLAIM_NAME_HERE`, response.data);
  }
};

```

Note that we need to save the response as a variable and **await** for the Axios post result before storing it as a custom claim in the token. If we do not wait for the response, we would observe undefined custom claims or claims that do not get appended to the token.

Hoped this helps!

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

Thank you.

---

<div class="post-metadata">

**Author:** ![VP003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vp003/32/33660_2.png) [@VP003](https://community.auth0.com/u/VP003)\
**Post date:** [May 26, 2022, 1:34pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/7 "2022-05-26T13:34:56Z")

</div>

Thank you @rueben.tiow for providing the solution.

I am able to make a call to an API and set custom claim with the updated code snippet.

Thank you very much one again.

Thank you,  
Vivek Patel

---

<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 8, 2022, 4:56pm UTC](https://community.auth0.com/t/not-getting-any-response-from-the-api-i-am-calling-from-the-action/83846/8 "2022-11-08T16:56:29Z")

</div>


