---
title: Environment Variables
description: Learn how to add environment variables to Vercel projects for use in v0 chats
type: guide
---

# Environment Variables



v0 uses your Vercel project's environment variables when generating code. The setup is:

1. Ensure the chat has a Vercel project attached.
2. Add environment variables using the Vercel API.
3. v0 automatically uses those variables when generating code.

## 1. Ensure the chat has a Vercel project [#1-ensure-the-chat-has-a-vercel-project]

Environment variables are stored on Vercel projects. Before adding them, make sure the chat has a `vercelProjectId`.

<CustomCodeBlock languages="['TypeScript', 'cURL']" defaultLanguage="TypeScript">
  <CodeVariant
    language="TypeScript"
    title="TypeScript Example"
    code="`import { v0 } from 'v0-sdk'

const chat = await v0.chats.get({
  chatId: 'chat_123',
})

let vercelProjectId = chat.vercelProjectId

if (!vercelProjectId) {
  const result = await v0.chats.createVercelProject({
    chatId: 'chat_123',
  })
  vercelProjectId = result.vercelProjectId
}`"
  />

  <CodeVariant
    language="cURL"
    title="cURL Example"
    code="`# Check if the chat has a Vercel project
curl -X GET &#x22;https://api.v0.app/v2/chats/chat_123&#x22; \\
  -H &#x22;Authorization: Bearer $V0_API_KEY&#x22;

# If vercelProjectId is missing, create one
curl -X POST &#x22;https://api.v0.app/v2/chats/chat_123/vercel-project&#x22; \\
  -H &#x22;Authorization: Bearer $V0_API_KEY&#x22; \\
  -H &#x22;Content-Type: application/json&#x22;`"
  />
</CustomCodeBlock>

For more details, see [Create Vercel Project](/api/v2/reference/chats/create-vercel-project).

## 2. Add environment variables with the Vercel API [#2-add-environment-variables-with-the-vercel-api]

Use the Vercel API to add environment variables to the project. This step happens outside the v0 Platform API.

```typescript
// Add environment variables to the Vercel project
await fetch(
  `https://api.vercel.com/v10/projects/${vercelProjectId}/env`,
  {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.VERCEL_TOKEN}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      key: "DATABASE_URL",
      value: "postgresql://user:pass@host:5432/db",
      type: "encrypted",
      target: ["production", "preview", "development"],
    }),
  }
)
```

For the full Vercel API reference, see [Environment Variables](https://vercel.com/docs/rest-api/projects/create-one-or-more-environment-variables).

<Callout type="warn">
  **Sensitive variables**: v0 runs in the development environment. Vercel does not expose sensitive environment variables to the development environment, so avoid using the sensitive type for variables that v0 needs to access.
</Callout>

## 3. v0 uses the variables automatically [#3-v0-uses-the-variables-automatically]

Once environment variables are added to the Vercel project, v0 can access them when generating code in that chat. The assistant sees which variables are available and generates code that references them.

For example, if you add `DATABASE_URL` and then prompt v0 to build a database-connected app, the generated code will use `process.env.DATABASE_URL` automatically.

<Callout type="info">
  **When changes take effect**: v0 reads environment variables from the Vercel project when it generates code. After adding, updating, or removing variables, sending a message will make the changes take effect in the chat and its preview.
</Callout>

<CustomCodeBlock languages="['TypeScript', 'cURL']" defaultLanguage="TypeScript">
  <CodeVariant
    language="TypeScript"
    title="TypeScript Example"
    code="`import { v0 } from 'v0-sdk'

// After adding DATABASE_URL to the Vercel project...
await v0.chats.messages.send({
  chatId: 'chat_123',
  prompt: 'Add a Postgres database connection using the DATABASE_URL',
})

// v0 generates code that uses process.env.DATABASE_URL`"
  />

  <CodeVariant
    language="cURL"
    title="cURL Example"
    code="`# After adding DATABASE_URL to the Vercel project...
curl -X POST &#x22;https://api.v0.app/v2/chats/chat_123/messages&#x22; \\
  -H &#x22;Authorization: Bearer $V0_API_KEY&#x22; \\
  -H &#x22;Content-Type: application/json&#x22; \\
  -d '{
    &#x22;prompt&#x22;: &#x22;Add a Postgres database connection using the DATABASE_URL&#x22;
  }'`"
  />
</CustomCodeBlock>
