How to use Vercel's VERCEL_URL as SANITY_STUDIO_ prefixed environment variable?
VERCEL_URLto set my studio's preview URL but can't find a way to add the
SANITY_STUDIO_prefix. (i know this is more of a Vercel question, but my question there didn't get answered. hoping someone here has already figured this out!)
To use VERCEL_URL in the Studio, copy it into a SANITY_STUDIO_-prefixed variable during the build. The simplest place is the build command itself. Sanity Studio only exposes environment variables with the SANITY_STUDIO_ prefix. They are statically replaced at build time, so an unprefixed process.env.VERCEL_URL won't be there, even in sanity.config.ts. That file is bundled into the browser Studio, and in production a bare process.env reference fails with process is not defined.
Option 1: set the prefixed variable on the build command (recommended)
The Sanity CLI picks up SANITY_STUDIO_ variables from the shell, and shell variables take precedence over .env files. Vercel's system variables are available during the build, so you can re-export the value under the prefix:
{
"scripts": {
"dev": "sanity dev",
"build": "SANITY_STUDIO_VERCEL_URL=$VERCEL_URL sanity build"
}
}The VAR=value command syntax works in POSIX shells like the one Vercel's build uses. If you also build on Windows, use a cross-platform tool such as cross-env, or use option 2.
Option 2: write .env.production before building
sanity build runs in production mode, so it loads .env.production if the file exists. You can generate that file in a prebuild script:
{
"scripts": {
"prebuild": "echo \"SANITY_STUDIO_VERCEL_URL=$VERCEL_URL\" >> .env.production",
"build": "sanity build"
}
}Or use a Node script, which works across platforms:
// scripts/prebuild.js
const fs = require('fs')
const lines = []
if (process.env.VERCEL_URL) lines.push(`SANITY_STUDIO_VERCEL_URL=${process.env.VERCEL_URL}`)
if (process.env.VERCEL_ENV) lines.push(`SANITY_STUDIO_VERCEL_ENV=${process.env.VERCEL_ENV}`)
fs.appendFileSync('.env.production', lines.join('\n') + '\n')Run it with "prebuild": "node scripts/prebuild.js". Writing the file is the step that matters. Setting process.env inside this script does nothing for sanity build, because the build runs as a separate process.
If you pick a different mode with SANITY_ACTIVE_ENV (for example staging), write .env.staging instead. The Studio loads .env.[mode] for the active mode.
Use it in your config
Reference the variable by its full name. Dynamic access like process.env[key] breaks in production. The docs suggest re-exporting variables from one file:
// src/environment.ts
export const deploymentUrl = process.env.SANITY_STUDIO_VERCEL_URL// sanity.config.ts
import {defineConfig} from 'sanity'
import {deploymentUrl} from './src/environment'
export default defineConfig({
// ...
// build your preview URL from deploymentUrl here
})Notes:
- Requires Studio v3.5.0 or later for full environment variable support.
- Every
SANITY_STUDIO_value ends up in the public JavaScript bundle. A deployment URL is fine to include, but tokens and other secrets are not. - Check Vercel's own documentation for exactly what
VERCEL_URLcontains (for example, whether it includes the protocol) and which system variables are available at build time for preview and production deployments.
Show original thread7 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.