Environment Variables
v0 uses your Vercel project's environment variables when generating code. The setup is:
- Ensure the chat has a Vercel project attached.
- Add environment variables using the Vercel API.
- v0 automatically uses those variables when generating code.
Ensure the chat has a Vercel project
Environment variables are stored on Vercel projects. Before adding them, make sure the chat has a vercelProjectId.
import { v0 } from 'v0'const chatResult = await v0.chats.get({ chatId: 'chat_123',})if (chatResult.error) throw new Error(chatResult.error.message)let vercelProjectId = chatResult.data.vercelProjectIdif (!vercelProjectId) { const projectResult = await v0.chats.createVercelProject({ chatId: 'chat_123', }) if (projectResult.error) throw new Error(projectResult.error.message) vercelProjectId = projectResult.data.vercelProjectId}For more details, see Create Vercel Project.
Add environment variables with the Vercel API
Use the Vercel API to add environment variables to the project. This step happens outside the v0 API.
For the full Vercel API reference, see Environment Variables.
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.
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.
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.
import { v0 } from 'v0'// After adding DATABASE_URL to the Vercel project...const result = await v0.messages.send({ chatId: 'chat_123', message: 'Add a Postgres database connection using the DATABASE_URL',})if (result.error) throw new Error(result.error.message)// v0 generates code that uses process.env.DATABASE_URLPrivate packages
Private packages use your team's shared NPM_TOKEN or NPM_RC, rather than the project environment variables above. Configure the shared credential as described in Private Dependencies, then pass credentialAccess: { sharedNpm: true } when you create a chat. API chats do not use these shared credentials by default.
The same option is available when creating a chat with streaming or async responses (v0.chats.createStream and v0.chats.createAsync). It is saved on the chat and applies to later messages and sandbox restarts. It is not supported when authenticating with a Vercel OIDC project principal.
GitHub access
Pass credentialAccess: { github: true } to let the chat's sandbox use gh and git against the chat's GitHub repository, for example to open a pull request. With a v0 API key, the sandbox uses your GitHub credentials.
The option is saved on the chat and applies to later messages and sandbox restarts.