Doogibo
Doogibo•6mo 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!
18 Replies
Convex Bot
Convex Bot•6mo 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•6mo 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•6mo ago
Are you using Convex preview deploys, so every preview deploy gets its own deployment?
Doogibo
DoogiboOP•6mo ago
Yes that's right
ballingt
ballingt•6mo 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•6mo ago
OK perfect thank you Tom! I will try this. šŸ˜„
ballingt
ballingt•6mo 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•6mo 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•6mo 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•6mo 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•6mo ago
oh no you're right, that's the relevant one
Doogibo
DoogiboOP•6mo 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•6mo ago
yeah I think it was the preview,I was confused
sshader
sshader•6mo 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•6mo ago
Oooooh! Let me give that a shot. šŸ™‚
sshader
sshader•6mo 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•6mo ago
WOOHOOOOOOOO!!!! :fixed: Thank you so much everyone!
Michael
Michael•2w ago
For posterity, I ended up doing something similar, but since I needed more env vars I did this to avoid the 255 char limit on vercel build instructions:
build_envs.sh
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_URL $VERCEL_URL && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_BRANCH_URL $VERCEL_BRANCH_URL && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_PROJECT_PRODUCTION_URL $VERCEL_PROJECT_PRODUCTION_URL
build_envs.sh
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_URL $VERCEL_URL && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_BRANCH_URL $VERCEL_BRANCH_URL && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_PROJECT_PRODUCTION_URL $VERCEL_PROJECT_PRODUCTION_URL
then my build cmd in the Vercel dashboard is: npx convex deploy --cmd 'npm run build' --preview-run internal.foo.bar && source ./build_envs.sh (I also have a convoluted way to make sure the Clerk webhook replays to all preview envs, the setup for which is what the foo.bar command is standing in for.)

Did you find this page helpful?