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

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 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:

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.

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.

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:

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:

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

Thank you.

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.

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?

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.

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:

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.

1 Like

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

1 Like