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

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