# How do I call my API from an Action?

**URL:** <https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041>\
**Category:** Get Help\
**Tags:** registration, api, auth0\
**Created:** [September 3, 2021, 4:54am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041 "2021-09-03T04:54:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshuarichards001](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/joshuarichards001/32/12921_2.png) [@joshuarichards001](https://community.auth0.com/u/joshuarichards001)\
**Post date:** [September 3, 2021, 4:54am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/1 "2021-09-03T04:54:29Z")

</div>

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:

[https://community.auth0.com/t/how-do-i-call-my-api-from-a-rule/41309](https://community.auth0.com/t/how-do-i-call-my-api-from-a-rule/41309)

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 3, 2021, 4:12pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/4 "2021-09-03T16:12:39Z")

</div>

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](https://auth0.com/docs/actions/write-your-first-action#test-the-action) could be helpful for debugging this.

---

<div class="post-metadata">

**Author:** ![joshuarichards001](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/joshuarichards001/32/12921_2.png) [@joshuarichards001](https://community.auth0.com/u/joshuarichards001)\
**Post date:** [September 4, 2021, 12:59am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/5 "2021-09-04T00:59:15Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![lofa](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lofa/32/25387_2.png) [@lofa](https://community.auth0.com/u/lofa)\
**Post date:** [September 9, 2021, 3:06pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/6 "2021-09-09T15:06:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 9, 2021, 9:24pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/7 "2021-09-09T21:24:00Z")

</div>

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:

```auto
/**
* 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);
};

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 9, 2021, 9:25pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/8 "2021-09-09T21:25:08Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![joshuarichards001](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/joshuarichards001/32/12921_2.png) [@joshuarichards001](https://community.auth0.com/u/joshuarichards001)\
**Post date:** [September 10, 2021, 4:13am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/9 "2021-09-10T04:13:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 13, 2021, 3:34pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/10 "2021-09-13T15:34:52Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![joshuarichards001](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/joshuarichards001/32/12921_2.png) [@joshuarichards001](https://community.auth0.com/u/joshuarichards001)\
**Post date:** [September 14, 2021, 5:30am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/11 "2021-09-14T05:30:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 14, 2021, 3:52pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/12 "2021-09-14T15:52:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joshuarichards001](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/joshuarichards001/32/12921_2.png) [@joshuarichards001](https://community.auth0.com/u/joshuarichards001)\
**Post date:** [September 15, 2021, 6:10am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/13 "2021-09-15T06:10:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sheilabrad.93](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sheilabrad.93/32/13031_2.png) [@sheilabrad.93](https://community.auth0.com/u/sheilabrad.93)\
**Post date:** [September 15, 2021, 12:30pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/14 "2021-09-15T12:30:50Z")

</div>

> [@joshuarichards001](#):
>
> 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

Thanks for post, i found lots of information here.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 15, 2021, 4:03pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/15 "2021-09-15T16:03:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sheilabrad.93](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sheilabrad.93/32/13031_2.png) [@sheilabrad.93](https://community.auth0.com/u/sheilabrad.93)\
**Post date:** [September 16, 2021, 3:52am UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/16 "2021-09-16T03:52:52Z")

</div>

> [@joshuarichards001](#):
>
> 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?  
> [Myloweslife](https://www.myloweslife.biz/)

Thanks for help, Really appreciate.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 13, 2021, 12:16pm UTC](https://community.auth0.com/t/how-do-i-call-my-api-from-an-action/69041/18 "2021-10-13T12:16:41Z")

</div>

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