For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
    • Overview
  • baml-cli
    • init
    • generate
    • test
    • serve
    • dev
    • fmt
  • Language Reference
      • comments
      • Environment Variables
      • string
      • int / float
      • bool
      • array (list)
      • map (dictionary)
      • image / audio
    • Types
    • function
    • test
    • template_string
    • client<llm>
    • class
    • enum
    • generator
  • Generated baml_client
    • with_options(..)
    • AbortSignal / Cancellation
    • Collector
    • logging / env vars
    • AsyncClient / SyncClient
    • TypeBuilder
    • ClientRegistry
    • client Option
    • OnTick
    • Multimodal
    • Image
    • Audio
    • Pdf
    • Video
  • Attributes
    • What are attributes?
    • @alias / @@alias
    • @description / @@description
    • @skip
    • @assert
    • @check
    • Jinja in Attributes
    • @@dynamic
  • LLM Client Providers
    • Overview
    • AWS Bedrock
    • Anthropic
    • Google AI: Gemini
    • Google: Vertex
    • OpenAI
    • OpenAI Responses API
    • OpenAI from Azure
    • OpenRouter
    • openai-generic
    • Microsoft Foundry (openai-generic)
    • Cerebras (openai-generic)
    • Groq (openai-generic)
    • Hugging Face (openai-generic)
    • Keywords AI (openai-generic)
    • Llama API (openai-generic)
    • Litellm (openai-generic)
    • LM Studio (openai-generic)
    • Ollama (openai-generic)
    • Vercel AI Gateway (openai-generic)
    • Tinfoil (openai-generic)
    • TogetherAI (openai-generic)
    • Unify AI (openai-generic)
    • vLLM (openai-generic)
  • LLM Client Strategies
    • Timeout Configuration
    • Retry Policy
    • Fallback
    • Round Robin
  • Prompt Syntax
    • What is jinja?
    • Jinja Filters
    • ctx.output_format
    • ctx.client
    • _.role
    • Variables
    • Conditionals
    • Loops
  • Editor Extension Settings
    • baml.cliPath
    • baml.generateCodeOnSave
    • baml.enablePlaygroundProxy
    • baml.syncExtensionToGeneratorVersion
Help on Discord
LogoLogo
On this page
  • Example
  • Setting Environment Variables
  • In the VSCode Playground
  • For Boundary Studio Integration
  • For Your App (Default)
  • Error Handling
Language ReferenceGeneral BAML Syntax

Environment Variables

Was this page helpful?
Edit this page
Previous

string

Next
Built with

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

1env.YOUR_VARIABLE_NAME
Environment variables with spaces in their names are not supported.

Example

Using an environment variable for API key:

1client<llm> MyCustomClient {
2 provider "openai"
3 options {
4 model "gpt-5-mini"
5 // Set the API key using an environment variable
6 api_key env.MY_SUPER_SECRET_API_KEY
7 }
8}

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

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
Express.js
Flask
Rails
$# .env.local
$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 / Doppler
  • 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
$export MY_SUPER_SECRET_API_KEY="..."
$python my_program_using_baml.py
python
typescript
ruby
1from dotenv import load_dotenv
2from baml_client import b
3
4load_dotenv()

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.