Doogibo
Doogiboā€¢2w ago

VERCEL_URL - dynamic Convex env

Hey there! I need to figure out the best way to handle VERCEL_URL. We are constructing clerk invitations in our Convex functions and need to generate a redirect url for the invitations. We want this to work for our preview deploys and prod. Prod is easy technically as it's a static domain and we could just explicitly set VERCEL_URL in the dashboard, but this doesn't work for previews. So what is the best/easiest option for setting this in Convex during Vercel deployments? Thank you!
17 Replies
Convex Bot
Convex Botā€¢2w 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!
Doogibo
DoogiboOPā€¢2w ago
Alternatively I guess we could pass it as an argument to our functions from the client, since VERCEL_URL is automatically set there. Easier... thoughts? EDIT: I really don't like this approach actually... would involve passing it down in so many cases (any endpoint that fires a notification action, etc.).
ballingt
ballingtā€¢2w ago
Are you using Convex preview deploys, so every preview deploy gets its own deployment?
Doogibo
DoogiboOPā€¢2w ago
Yes that's right
ballingt
ballingtā€¢2w ago
Cool, so one way would be setting the environment variable in your preview deploy build script, like npx convex env set VERCEL_URL $VERCEL_URL (I think that syntax is right?) ie take whatever the value of $VERCEL_URL is in the build environment and set that value in the preview Convex deployment (once its been provisioned)
Doogibo
DoogiboOPā€¢2w ago
OK perfect thank you Tom! I will try this. šŸ˜„
ballingt
ballingtā€¢2w ago
A similar approach would be some kind of initializaiton script, a mutation that sets one configuration data in a table but you could have more custom logic around this than envvars
Doogibo
DoogiboOPā€¢2w ago
Ah yes another solid option! Still struggling. Really want the CLI env set approach to work:
npx convex deploy --cmd 'npm run build' --preview-run 'system/dummy:seedForPreviewDeployments' && npx convex env set VERCEL_URL $VERCEL_URL
npx convex deploy --cmd 'npm run build' --preview-run 'system/dummy:seedForPreviewDeployments' && npx convex env set VERCEL_URL $VERCEL_URL
Thinking it's possibly the "once it's provisioned" piece... don't get any helpful errors in the vercel deploy logs. @ballingt Well this is actually what we get: Finished running function "system/dummy:seedForPreviewDeployments" āœ– Please set CONVEX_DEPLOY_KEY to a new key which you can find on your Convex dashboard. Which we don't get unless we include the second env set command.
ballingt
ballingtā€¢2w ago
Ah maybe the preview flow doesn't write out the convex_url anywhere? We'll add something if there's no good solution here
Doogibo
DoogiboOPā€¢2w ago
That would be great. Still no dice on my end šŸ˜¦ && echo "Deploy key exists: $CONVEX_DEPLOY_KEY" && npx convex env set VERCEL_URL $VERCEL_URL Does print out the key correctly. Interesting Oh you said CONVEX_URL, my bad.
ballingt
ballingtā€¢2w ago
oh no you're right, that's the relevant one
Doogibo
DoogiboOPā€¢2w ago
Let me try deploying again to see, I didn't copy it but I feel like it was the preview one not the prod one.
ballingt
ballingtā€¢2w ago
yeah I think it was the preview,I was confused
sshader
sshaderā€¢2w ago
Chiming in since it sounds like you're trying to set a convex env variable on a preview deployment -- npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF foo bar might be what you're looking for
Doogibo
DoogiboOPā€¢2w ago
Oooooh! Let me give that a shot. šŸ™‚
sshader
sshaderā€¢2w ago
($VERCEL_GIT_COMMIT_REF is the default name we use for preview deployments running in vercel, I think npx convex env might try by default to set things on a personal dev deployment but get confused when running in Vercel)
Doogibo
DoogiboOPā€¢2w ago
WOOHOOOOOOOO!!!! :fixed: Thank you so much everyone!

Did you find this page helpful?