How do I call my API from an Action?

Hey @joshuarichards001,

I was able to create an action that works to request a token and call an endpoint with the token, while logging the data from each request.Can you take a look and see if this will work for you? Also, can you DM me the URL (event.secrets.API_CREATE_USER) you are trying to call?

Here is the action I created:

/**
* Handler that will be called during the execution of a PostUserRegistration flow.
*
* @param {Event} event - Details about the context and user that has registered.
*/
const axios = require('axios');

exports.onExecutePostUserRegistration = async (event) => {

  const tokenOptions = {
    method: 'POST',
    url: `https://${event.secrets.DOMAIN}/oauth/token`,
    headers: { 'content-type': 'application/json' },
    data: {
      grant_type: 'client_credentials',
      client_id: event.secrets.CLIENT_ID,
      client_secret: event.secrets.CLIENT_SECRET,
      audience: `https://${event.secrets.DOMAIN}/api/v2/`
    }
  };


  const res = await axios.request(tokenOptions);
  console.log('Access Token: ', res.data.access_token);
  
  const usersOptions = {
    method: 'GET',
    url: `https://${event.secrets.DOMAIN}/api/v2/users`,
    headers: {
      'Authorization': `Bearer ${res.data.access_token}`,
      'content-type': 'application/json'
    }
  };

  const users = await axios.request(usersOptions);
  console.log('Users: ', users.data);
};
5 Likes