How do I call my API from an Action?

Hey,

So I’m in the process of trying to add a user to my database using an API call from a “Post User Registration” Action. I have followed this blog post here:

and seem to have everything looking good. On top of that when I run the test it says the action was successful. The only problem is that my API is not being called at all even though I have triple checked that the URL is correct.

Here is the code I’m working with:

import axios from "axios";

exports.onExecutePostUserRegistration = async (event) => {
  const res = await axios.post(`https://${event.secrets.DOMAIN}/oauth/token`, {
    headers: {
      "content-type": "application/json",
    },
    body: {
      client_id: event.secrets.CLIENT_ID,
      client_secret: event.secrets.CLIENT_SECRET,
      audience: event.secrets.AUDIENCE,
      grant_type: "client_credentials",
    },
  });
  
  await axios.post(event.secrets.API_CREATE_USER, {
    body: {
      user: event.user,
    },
    headers: {
      Authorization: "Bearer " + res.data.access_token,
    },
  });
};

Any help would be much appreciated.

Cheers,
Josh

2 Likes

If you log the response from your API do you get any information that helps to reveal the issue?

Using the test functionality of actions could be helpful for debugging this.

How do I go about logging the response? I’m doing a console.log but it doesn’t seem to be doing anything.

const user = await axios.post(event.secrets.API_CREATE_USER, {
    body: {
      user: event.user,
    },
    headers: {
      Authorization: "Bearer " + res.data.access_token,
    },
  });
  console.log(user.data);

Cheers,
Josh

Same problem here, tried with Axios and a couple of different methods but no luck. Tried logging but console.log doesn’t return anything.

I’m trying to check if the user’s email domain is included in a trusted list, but I don’t know how to fetch it without HTTP requests.

Hope someone finds a solution, Actions are super useful!

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

@lofa

Welcome to the Auth0 Community!

You may want to create a seperate topic for your issue so we can debug your action as well, otherwise feel free to follow along with this conversation.

Works great Dan! thanks heaps for your help.

Another quick question, is there any way to get some kind of notification on the client-side when the action is completed?

For example, I would like the user to be able to log in on the client, have the action run (which adds them to the database), then have the client call information from the server after the action is complete.

Cheers,
Josh

@joshuarichards001,

If that is the flow you are working with you may want to do this in a post login action instead of a post registration action. You can do it only on the first login, which will block the login until the request is completed.

As far as how it would work, you could set a flag in the user’s metadata that indicates they have been added to the DB, and only run the action if that flag doesn’t exist.

How would that function differently from the registration flow? I just want want to call my server from my client after the action is complete. Is that not possible using the registration flow?

The post-reg action is fire and forget. It isn’t meant to work synchronously with registration/login (i.e. wait for it to complete before doing xyz). The post-login action will block login while it completes, and once completed it will return to your application with the user’s tokens.

Oh amazing, just got it set up and it’s working perfectly, thanks again!

1 Like

Thanks for post, i found lots of information here.

@joshuarichards001 Great, glad you found a solution! Let us know if you have any questions.

Thanks for help, Really appreciate.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.