#VERCEL_URL - dynamic Convex env

27 messages · Page 1 of 1 (latest)

old mesa
#

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!

brave mangoBOT
#

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](https://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!
old mesa
#

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.).

modern jay
#

Are you using Convex preview deploys, so every preview deploy gets its own deployment?

old mesa
#

Yes that's right

modern jay
#

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)

old mesa
#

OK perfect thank you Tom! I will try this. 😄

modern jay
#

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

old mesa
#

Ah yes another solid option!

old mesa
#

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

Thinking it's possibly the "once it's provisioned" piece... don't get any helpful errors in the vercel deploy logs.

#

@modern jay 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.

modern jay
#

Ah maybe the preview flow doesn't write out the convex_url anywhere?

#

We'll add something if there's no good solution here

old mesa
#

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.

modern jay
#

oh no you're right, that's the relevant one

old mesa
#

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.

modern jay
#

yeah I think it was the preview,I was confused

torpid elk
#

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

old mesa
#

Oooooh! Let me give that a shot. 🙂

torpid elk
#

($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)

old mesa
#

WOOHOOOOOOOO!!!! fixed

#

Thank you so much everyone!

boreal oracle
#

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:

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.)