Skip to content

How to use Vercel's VERCEL_URL as SANITY_STUDIO_ prefixed environment variable?

7repliesLast updated: Nov 13, 2026

has anyone hosting their studio on Vercel figured out a way to use its system environment variables ? i want to use the
VERCEL_URL
to 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!)
AI Update

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.

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:

Show original thread
7 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.

Related contributions