#?

18 messages · Page 1 of 1 (latest)

hasty heath
#

when i deploy to vercel how do i also auto update the env on convex , do i have to change the vercel.json

reef frigateBOT
#

Hi @hasty heath. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

You don’t need to change vercel.json for Convex itself, but there are two different “env” directions to think about:


1. Vercel → Convex (set Convex env vars during a Vercel deploy)

You can update Convex deployment environment variables from your Vercel build by calling the Convex CLI in the build command.

For preview deployments, Convex recommends:

npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_URL $VERCEL_URL

This sets VERCEL_URL on the Convex preview deployment whose name matches the branch ($VERCEL_GIT_COMMIT_REF). [VERCEL_URL env]

You’d typically append this to your existing build command, e.g. in Vercel’s Build Command:

npx convex deploy --cmd 'npm run build' \
  --preview-run 'someFunction' && \
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_URL $VERCEL_URL

For production you can use npx convex env set NAME value without --preview-name. [CLI env; Env vars]

No vercel.json change is required; this is configured in Vercel’s project “Build & Development Settings”.


2. Convex → Vercel (point frontend at the right Convex deployment)

This is handled by npx convex deploy when you set your Vercel Build Command to:

npx convex deploy --cmd 'npm run build'
#

Convex reads CONVEX_DEPLOY_KEY, provisions/updates the right deployment, and sets CONVEX_URL (or NEXT_PUBLIC_CONVEX_URL etc.) for your build so the frontend talks to the correct Convex backend. [Connect to Vercel; How it works; Preview deployments]

Again, this is done via the Vercel build command, not vercel.json.


So:

  • Use npx convex deploy --cmd 'npm run build' as your Vercel build command.
  • If you want to auto-update Convex env vars from Vercel, chain npx convex env set ... after that command.
  • You do not need to modify vercel.json for this.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@hasty heath Kapa.ai is still learning and improving, please let me know how I did by reacting below.

hasty heath
#

@reef frigate If you want to auto-update Convex env vars from Vercel, chain npx convex env set ... after that command how

reef frigateBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You do it by extending the Vercel Build Command so it runs npx convex deploy first, then one or more npx convex env set commands.

1. For preview deployments (most common)

In Vercel → Project → Settings → Build & Development SettingsBuild Command, use something like:

npx convex deploy --cmd 'npm run build' \
  --preview-run 'internal.system:initPreview' && \
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_URL $VERCEL_URL

Explanation:

  • npx convex deploy --cmd 'npm run build'
    – creates/updates the Convex preview deployment and builds your app. [Vercel previews; deploy CLI]
  • --preview-run 'internal.system:initPreview' (optional)
    – runs a Convex function only for preview deployments. [Vercel previews]
  • npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF VERCEL_URL $VERCEL_URL
    – writes the Vercel preview URL into the Convex preview deployment whose name matches the branch ($VERCEL_GIT_COMMIT_REF). [VERCEL_URL thread; env CLI]

If you need multiple vars, you can put them in a script (as shown in the Discord thread) and source it after convex deploy. [VERCEL_URL script]

2. For production deployments

In the Production build command you can do:

npx convex deploy --cmd 'npm run build' && \
npx convex env set API_BASE_URL <https://api.example.com>
#

Here npx convex env set targets the production deployment (because the build is running with a production CONVEX_DEPLOY_KEY). [env CLI; Vercel prod deploy]

No vercel.json changes are required; everything is configured via the Vercel dashboard build command.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@hasty heath Kapa.ai is still learning and improving, please let me know how I did by reacting below.

hasty heath
#

@reef frigate what if i ahve multiple env to update, like ERNV_A, ENV_B, ENV_C anmd etc

reef frigateBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You can set as many Convex env vars as you want; you just call npx convex env set once per variable, or use a small script.

For preview deployments on Vercel, the Discord example does exactly this with multiple vars: [VERCEL_URL script]

# build_envs.sh
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_A $ENV_A && \
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_B $ENV_B && \
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_C $ENV_C

Then in Vercel’s Build Command:

npx convex deploy --cmd 'npm run build' --preview-run internal.foo.bar && \
source ./build_envs.sh

Key points:

  • --preview-name $VERCEL_GIT_COMMIT_REF makes sure you’re writing to the Convex preview deployment for that branch. [VERCEL_URL thread]
  • For production, you can omit --preview-name and just run npx convex env set ENV_A value etc; it will target the deployment associated with the CONVEX_DEPLOY_KEY in that build. [Env CLI; Env vars]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@hasty heath Kapa.ai is still learning and improving, please let me know how I did by reacting below.

hasty heath
reef frigateBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You can keep your vercel.json and just extend the buildCommand to run convex env set after convex deploy.

For example, if you want to sync multiple env vars (ENV_A, ENV_B, ENV_C) from Vercel into the Convex preview deployment for that branch, you can follow the pattern from the Discord thread and call npx convex env set once per variable with --preview-name $VERCEL_GIT_COMMIT_REF.[VERCEL_URL script]

Option 1: Inline in vercel.json

{
  "$schema": "<https://openapi.vercel.sh/vercel.json",>
  "buildCommand": "npx convex deploy --cmd 'npm run build' && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_A $ENV_A && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_B $ENV_B && npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_C $ENV_C"
}

Notes:

  • npx convex deploy --cmd 'npm run build' is the recommended build command for Vercel so Convex and your frontend stay in sync.[Vercel deploy; How it works]
  • --preview-name $VERCEL_GIT_COMMIT_REF targets the Convex preview deployment created for that branch name.[VERCEL_URL thread; Preview deploys]
  • Each npx convex env set writes one Convex env var.[Env CLI; Env vars]

Option 2: Use a script (cleaner for many vars)

Create build_envs.sh in your repo:

#
# build_envs.sh
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_A $ENV_A && \
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_B $ENV_B && \
npx convex env set --preview-name $VERCEL_GIT_COMMIT_REF ENV_C $ENV_C

Then in vercel.json:

{
  "$schema": "<https://openapi.vercel.sh/vercel.json",>
  "buildCommand": "npx convex deploy --cmd 'npm run build' && source ./build_envs.sh"
}

This mirrors the working example from the Discord thread, just with your own env names.[VERCEL_URL script]

For production builds, drop --preview-name ... so convex env set writes to the production deployment associated with the CONVEX_DEPLOY_KEY in that environment.[Env CLI]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: