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
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!
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.).
Are you using Convex preview deploys, so every preview deploy gets its own deployment?
Yes that's right
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)OK perfect thank you Tom! I will try this. š
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
Ah yes another solid option!
Still struggling. Really want the CLI env set approach to work:
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.
Ah maybe the preview flow doesn't write out the convex_url anywhere?
We'll add something if there's no good solution here
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.
oh no you're right, that's the relevant one
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.
yeah I think it was the preview,I was confused
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 forOooooh! Let me give that a shot. š
(
$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)WOOHOOOOOOOO!!!! :fixed:
Thank you so much everyone!