Environment variables in Actions?

I’m currently implementing this:

But where do you put environment variables in Auth0 for actions?

Hi @stoplion,

Thanks for reaching out to the Auth0 Community!

You can set environment variables as Secrets in Actions.

Please see our Write Your First Action documentation to learn more.

May I help you with anything else?

Thanks,
Rueben

Hi @stoplion,

Attaching the code snippet below:

const { MongoClient } = require("mongodb");

exports.onExecutePostUserRegistration = async (event) => {
  const newUser = { sub: event.user.sub };
  const client = await MongoClient.connect(event.secrets.MONGO_DB);
  const db = client.db();
  await db.collection("users").insertOne(newUser);
  client.close();
};

The URL value of the event.secrets.MONGO_DB should be your MongoDB URL, which should be in the format:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

For example:
mongodb://mongodb0.example.com:27017

Reference: https://www.mongodb.com/docs/manual/reference/connection-string/

I hope this helps!

Please let me know if I can do anything else to help.

Thanks,
Rueben

1 Like

Thanks. One last thing. I’m using MongoDB Cloud, and it requires I whitelist urls. Currently the action isn’t working (not saving a user). My guess is that I need to whitelist Auth0? Is there a URL to the Auth0 action I can give MongoDB Cloud?

1 Like

Hi @stoplion,

Yes, most definitely! You can find this information on our Auth0 IP Addresses for Allow Lists documentation.

Please feel free to reach out again if you have any more questions.

Thanks,
Rueben

Hey there!

As this topic is related to Actions and Rules & Hooks are being deprecated soon in favor of Actions, I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!