> Version notice: This site documents BAML v0, the legacy DSL. BAML v1 is a separate, fully featured programming language in public beta. If the user intends to use BAML v1, ignore this site's content and use the BAML v1 documentation instead. If the intended version is unclear, clarify it before using this page.
> For clean Markdown of any page, append .md to the page URL. For the complete documentation index, see https://docs.boundaryml.com/llms.txt. For MCP access, connect to https://docs.boundaryml.com/_mcp/server.

To set a value to an environment variable, use the following syntax:

```baml
env.YOUR_VARIABLE_NAME
```

> **Warning**
>
> Environment variables with spaces in their names are not supported.

### Example

Using an environment variable for API key:

```baml
client<llm> MyCustomClient {
    provider "openai"
    options {
        model "gpt-5-mini"
        // Set the API key using an environment variable
        api_key env.MY_SUPER_SECRET_API_KEY
    }
}
```

## Setting Environment Variables

### In the VSCode Playground

Once you open a `.baml` file in VSCode, you should see a small button over every BAML function: `Open Playground`. Then you should be able to set environment variables in the settings tab.

![VSCode Code Lens](/_fern-img/0217c4846970898f3b072bc28b4954d4e9f8a15a0a21d4461e48fdbd40d07cdc.webp)

Or type `BAML Playground` in the VSCode Command Bar (`CMD + Shift + P` or `CTRL + Shift + P`) to open the playground.

### For Boundary Studio Integration

To send logs and traces to Boundary Studio, you need to set the `BOUNDARY_API_KEY` environment variable. This key is provided when you create an API key in your Boundary Studio dashboard.

#### Next.js

```bash
# .env.local
BOUNDARY_API_KEY=your_api_key_here
```

#### Express.js

```bash
# .env
BOUNDARY_API_KEY=your_api_key_here
```

#### Flask

```bash
# .env
BOUNDARY_API_KEY=your_api_key_here
```

#### Rails

```yaml
# config/application.yml
BOUNDARY_API_KEY: your_api_key_here
```

### For Your App (Default)

BAML will do its best to load environment variables from your program. Any of the following strategies for setting env vars are compatible with BAML:

* Setting them in your shell before running your program
* In your `Dockerfile`
* In your `next.config.js`
* In your Kubernetes manifest
* From `secrets-store.csi.k8s.io`
* From a secrets provider such as [Infisical](https://infisical.com/) / [Doppler](https://www.doppler.com/)
* From a `.env` file (using `dotenv` CLI)
* Using account credentials for ephemeral token generation (e.g., Vertex AI Auth Tokens)
* `python-dotenv` package in Python or `dotenv` package in Node.js

```bash
export MY_SUPER_SECRET_API_KEY="..."
python my_program_using_baml.py
```

#### python

```python
from dotenv import load_dotenv
from baml_client import b

load_dotenv()
```

#### typescript

```typescript
import dotenv from 'dotenv'
import { b } from './baml_client'

dotenv.config()
```

#### ruby

```ruby
require 'dotenv/load'
require 'baml_client'
```

## Error Handling

Errors for unset environment variables are only thrown when the variable is accessed. If your BAML project has 15 environment variables and 1 is used for the function you are calling, only that one environment variable will be checked for existence.