too_easy
too_easy2w ago

CONVEX_SITE_URL undefined in functions?

Seems process.env.CONVEX_SITE_URL inside queries is undefined? But the docs say that this system env var is always available inside convex functions?
4 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Nicolas
Nicolas2w ago
It should definitely be defined in queries. Do you have some example function where it doesn't work?
too_easy
too_easyOP2w ago
@Nicolas figured out that env.CONVEX_SITE_URL becomes undefined when using ctx.runQuery(api.getUrl). However, using useQuery(api.getUrl) - i.e. using the function directly via the client it is defined as it should be. Using a function via another function it seems to become undefined. Repro:
export const getTest = query({
handler: async (ctx) => {
console.log({
URL: process.env.CONVEX_CLOUD_URL,
SITE: process.env.CONVEX_SITE_URL,
});

return {
URL: process.env.CONVEX_CLOUD_URL,
SITE: process.env.CONVEX_SITE_URL,
};
},
});
export const getTest = query({
handler: async (ctx) => {
console.log({
URL: process.env.CONVEX_CLOUD_URL,
SITE: process.env.CONVEX_SITE_URL,
});

return {
URL: process.env.CONVEX_CLOUD_URL,
SITE: process.env.CONVEX_SITE_URL,
};
},
});
This logs and returns correctly when doing either on frontend:
convexReactClient.query(api.getTest, {})
convexReactClient.query(api.getTest, {})
OR
const {} = useQuery(api.getTest, {})
const {} = useQuery(api.getTest, {})
However doing this in convex backend:
const someOtherQuery = getTest = query({
handler: async (ctx) => {
const envVars = await ctx.runQuery(api.getTest, {});
console.log(envVars) // {URL: undefined, SITE: undefined}
}
});
const someOtherQuery = getTest = query({
handler: async (ctx) => {
const envVars = await ctx.runQuery(api.getTest, {});
console.log(envVars) // {URL: undefined, SITE: undefined}
}
});
Logs and returns undefined - so assuming some issue with propagation of env variables with ctx transfer between functions?
lee
lee2w ago
Yeah this is because ctx.runQuery is designed to be used with components (convex.dev/components), which do not have access to CONVEX_SITE_URL (because they don't know where their http actions will be exposed). We recommend you call a helper function instead of doing ctx.runQuery https://docs.convex.dev/understanding/best-practices#use-helper-functions-to-write-shared-code
Best Practices | Convex Developer Hub
This is a list of best practices and common anti-patterns around using Convex.

Did you find this page helpful?