#run a function only in prod env
3 messages · Page 1 of 1 (latest)
Hi @deft saddle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To limit a function to only run in the production environment in Convex, you can use environment variables. Here's how you can achieve this:
-
Set up an environment variable (e.g.,
CONVEX_ENV) in your production deployment through the Convex dashboard. Set it to "production" or any value you prefer for the production environment. -
In your Convex function, you can then check this environment variable and only execute the main logic if it's set to the production value. Here's an example:
export default function myFunction() {
if (process.env.CONVEX_ENV !== "production") {
return; // Exit early if not in production
}
// Your production-only logic here
}
This approach allows you to control the execution of your function based on the environment. The function will run in all environments, but it will only execute the main logic in production.
It's important to note that you shouldn't condition your function exports on environment variables, as mentioned in the Convex documentation on accessing environment variables (https://docs.convex.dev/production/environment-variables#accessing-environment-variables):
Note that you should not condition your Convex function exports on environment variables. The set of Convex functions that can be called is determined during deployment and is not reevaluated when you change an environment variable.
Instead, you should perform the environment check inside the function body, as shown in the example above.