# Rule for creating new User in Mailchimp when User signs up with Auth0

**URL:** <https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380>\
**Category:** Get Help\
**Created:** [March 7, 2022, 9:51am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380 "2022-03-07T09:51:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 7, 2022, 9:51am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/1 "2022-03-07T09:51:04Z")

</div>

Hi everyone, new to here to Auth0. Looking to create a rule for a User that signs up on Auth0 to then add the user to Mailchimp.

Having trouble adding these fields below

- MC\_TOKEN: Mailchimp API Token. E.g.: ef235a44355dda3e61ea074ae0d439a2-us20
- MC\_LIST\_ID: ID of the Mailchimp list you would like to populate. E.g.: ca9eb2555e
- MC\_API\_URL: Mailchimp API URL. E.g.: [https://us20.api.mailchimp.com/3.0](https://us20.api.mailchimp.com/3.0)

Can someone help show where they go? Thanks! 🙏

Found this code:

```auto
function (user, context, callback) {

    // Short-circuit if the user signed up already
    if (context.stats.loginsCount > 1 || context.protocol === 'oauth2-refresh-token') {
        return callback(null, user, context);
      }
  
    const request = require('request');
  
    const ctx = {
        email_address: user.email,
        merge_fields : {"FNAME": user.given_name || "",
                        "LNAME": user.family_name || ""},
        status: "subscribed"
    };
 
    var auth = "Basic " + new Buffer("token:" + configuration.MC_TOKEN).toString("base64");

    request.post({
      url: configuration.MC_API_URL + "/lists/" + configuration.MC_LIST_ID + "/members",
      headers : {"Authorization" : auth},
      json: ctx
    });
  
    // Don’t wait for the Mailchimp call to finish, return right away (the request will continue on the sandbox)`
    callback(null, user, context);
}

```

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 8, 2022, 12:29am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/3 "2022-03-08T00:29:37Z")

</div>

Hi @christopherkuchta,

Welcome to the Auth0 Community!

There are a couple of things I would like to address.

First, Rules are executed Post-Login. In general, whenever a user signs up for your app, they are immediately logged in afterward. In the Rule snippet you shared, it hacks around this by checking if the `loginCount` is less than 1 and executing this Rule only once. It works, however, Rules will be deprecated sometime this year in 2022 and we encourage you to migrate to Actions.

In this case, I recommend using an Auth0 [Post-User Registration Action](https://auth0.com/docs/customize/actions/flows-and-triggers/post-user-registration-flow) to accomplish this. The [Mailchimp documentation](https://mailchimp.com/developer/marketing/api/list-members/add-member-to-list/) will also be useful for looking at their API reference to make these requests.

See below of the Action snippet:

```auto
exports.onExecutePostUserRegistration = async (event) => {
  const client = require('@mailchimp/mailchimp_marketing');

  client.setConfig({
    apiKey: event.secrets.MC_API_KEY,
    server: event.secrets.MC_SERVER_PREFIX
  });

  const run = async () => {
    const response = await client.lists.addListMember(event.secrets.MC_LIST_ID, {
      email_address: "example_user@gmail.com",
      status: "pending",
    });
    console.log(response);
  };

  run();
};

```

Once you have included this Action, please click on the **Secrets** button to include the `MC_TOKEN`, `MC_LIST_ID`, AND `MC_API_URL` values. These values can be called by using the `event.secrets` prefix, for example `event.secrets.MC_TOKEN`.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/8/0/8025583655b0d0899a344b135fa339d467efe6dc.jpeg)

And include the appropriate dependency:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/f/4/f4e3445d337be67db88aae3aa562d3534bd6bac5.jpeg)

After this is complete, you should be able to create a new user in Mailchimp whenever a new user signs up onto your Auth0 application.

Please let me know how this goes for you.

Thank you.

---

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 8, 2022, 1:09am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/5 "2022-03-08T01:09:13Z")

</div>

Hi @rueben.tiow Awesome. Thank you so much for your detailed response 🙌 I am going to work on this later this evening and will let you know if I have any problems.

---

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 8, 2022, 4:39am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/6 "2022-03-08T04:39:54Z")

</div>

Hi @rueben.tiow I just implemented everything on Auth0 and not having luck getting the Post registration user into the Mailchimp list. Any idea if I am missing something?

 ![Screen Shot 2022-03-07 at 8.37.51 PM](https://global.discourse-cdn.com/auth0/original/3X/8/f/8ff17cf2aefd6804649c349f9e25c2af256c0d21.png)  
 ![Screen Shot 2022-03-07 at 8.38.01 PM](https://global.discourse-cdn.com/auth0/original/3X/b/b/bb195e6f4a550f537155b9f8df83b3bc16142813.png)  
 ![Screen Shot 2022-03-07 at 8.38.09 PM](https://global.discourse-cdn.com/auth0/original/3X/4/9/49ed106598b696f6b7d84b663ee4a0bb3ba8b189.png)

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 8, 2022, 5:21pm UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/7 "2022-03-08T17:21:00Z")

</div>

Hi @christopherkuchta,

Thank you for your response.

I have tested this thoroughly and did not find your same observations.

Instead, after I added the member to the list, I called the [Mailchimp Marketing API List members info endpoint](https://mailchimp.com/developer/marketing/api/list-members/list-members-info/) and verified that the users are added to the specific list.

I also encountered the “user already exists” error when adding the user to the list again.

In this case, could you please verify if your new user ([example\_user@gmail.com](mailto:example_user@gmail.com)) is added to the member’s list?

Looking forward to your response.

Thanks.

---

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 8, 2022, 7:45pm UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/8 "2022-03-08T19:45:29Z")

</div>

Hi @rueben.tiow thanks for your continued help. Sorry for my lack of experience with back end dev, I think it may not be working for me because I do not have this exact code added

```auto
const client = require("mailchimp-marketing");

client.setConfig({
  apiKey: "YOUR_API_KEY",
  server: "YOUR_SERVER_PREFIX",
});

const run = async () => {
  const response = await client.lists.setListMember(
    "list_id",
    "subscriber_hash",
    { email_address: "Marcel81@gmail.com", status_if_new: "pending" }
  );
  console.log(response);
};

run();

```

Right now the code currently looks like this for the action:

```auto
exports.onExecutePostUserRegistration = async (event) => {
  const client = require('@mailchimp/mailchimp_marketing');

  client.setConfig({
    apiKey: event.secrets.MC_API_KEY,
    server: event.secrets.MC_SERVER_PREFIX
  });

  const run = async () => {
    const response = await client.lists.addListMember(event.secrets.MC_LIST_ID, {
      email_address: "example_user@gmail.com",
      status: "pending",
    });
    console.log(response);
  };

  run();
};

```

---

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 8, 2022, 7:47pm UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/9 "2022-03-08T19:47:09Z")

</div>

Also did not see [example\_user@gmail.com](mailto:example_user@gmail.com) added to the mailchimp list

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 9, 2022, 11:27pm UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/10 "2022-03-09T23:27:25Z")

</div>

Hi @christopherkuchta,

Thank you for your response.

Could you please clarify if you used the [Mailchimp Marketing API List members info endpoint](https://mailchimp.com/developer/marketing/api/list-members/list-members-info/) to check if the users are added to the specific list?

When using this endpoint, I successfully saw the member’s list populated.

Looking forward to your reply.

Thanks.

---

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 10, 2022, 7:19am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/11 "2022-03-10T07:19:02Z")

</div>

Hi @rueben.tiow sorry I am only a front-end developer trying to help out a client set this up that I normally am only front-end building in Webflow.

I think I have the initial setup all correct but I am not sure where to put this Mailchimp API? I tried adding it as a new action but did not work.

Are you able to help out the client directly or is there another contractor from Auth0 that could set this connection up with Mailchimp for them?

Thanks so much 🙏🏻

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 14, 2022, 10:07pm UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/12 "2022-03-14T22:07:34Z")

</div>

Hi @christopherkuchta,

Thank you for your response.

> [@christopherkuchta](#):
>
> I tried adding it as a new action but did not work.

Could you please elaborate further what you mean by the Action did not work? Did you get any errors?

> [@christopherkuchta](#):
>
> Are you able to help out the client directly or is there another contractor from Auth0 that could set this connection up with Mailchimp for them?

Sure! I can help the customer if they can reach out to me through email or DM on the Community Forum.

Please let me know what they decide.

Thank you.

---

<div class="post-metadata">

**Author:** ![christopherkuchta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christopherkuchta/32/35683_2.png) [@christopherkuchta](https://community.auth0.com/u/christopherkuchta)\
**Post date:** [March 15, 2022, 2:43am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/13 "2022-03-15T02:43:33Z")

</div>

I’m not seeing the new contact in Mailchimp when I have the action implemented but I may totally have something setup wrong. I am going to have the customer reach out to you directly and we can figure this out! Thanks again 🙏🏻

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 15, 2022, 3:31am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/14 "2022-03-15T03:31:15Z")

</div>

Hi @christopherkuchta,

Thank you for your response.

After digging into this, I was able to find the same observations as you when looking at my Mailchimp Dashboard. However, when calling the Mailchimp API’s List members info endpoint, I managed to see the included user.

I will look into this further to see what is happening. Once I have more information.

Thanks!

---

<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:** [March 30, 2022, 3:32am UTC](https://community.auth0.com/t/rule-for-creating-new-user-in-mailchimp-when-user-signs-up-with-auth0/79380/15 "2022-03-30T03:32:06Z")

</div>

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