Skip to main content
Open in Kaggle  Open in Colab  Download Notebook
This documentation page is also available as an interactive notebook. You can launch the notebook in Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the above links.
Set up API credentials for OpenAI, Anthropic, and other AI providers so Pixeltable can access them.

Problem

You need to call AI services (OpenAI, Anthropic, Gemini, etc.) from your data pipeline. These services require API keys, but you don’t want to hardcode credentials in your notebooks or scripts.

Solution

What’s in this recipe:
  • Set API keys using environment variables
  • Store keys in a config file for all projects
  • Use getpass for one-time session keys
You configure API keys using one of three methods, depending on your needs. Pixeltable automatically discovers credentials from environment variables or config files—no code changes needed.

Setup

%pip install -qU pixeltable
import pixeltable as pxt
import os
from pathlib import Path

Option 1: environment variables

Use when: CI/CD pipelines, Docker containers, production deployments Set the environment variable in your shell before running Python:
# In your terminal (temporary, current session only)
export OPENAI_API_KEY="sk-..."

# Or add to ~/.bashrc or ~/.zshrc (permanent)
echo 'export OPENAI_API_KEY="sk-..."' >> ~/.zshrc
You can also set it in Python (useful for testing):
# Set in Python (current process only)
# os.environ['OPENAI_API_KEY'] = 'sk-...'

# Check if a key is set

Option 2: config file

Use when: Local development, want credentials available to all Pixeltable projects Create ~/.pixeltable/config.toml:
# ~/.pixeltable/config.toml
[openai]
api_key = "sk-..."

[anthropic]
api_key = "sk-ant-..."

[google]
api_key = "AIza..."
You can check if the config file exists:
# Check config file location
config_dir = Path.home() / '.pixeltable'
config_file = config_dir / 'config.toml'

Option 3: getpass (interactive)

Use when: Shared notebooks, demos, one-time sessions Prompt for the key at runtime—it won’t be saved anywhere:
import getpass

# Uncomment to use interactively:
# if 'OPENAI_API_KEY' not in os.environ:
#     os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key: ')

Verify your configuration

Test that Pixeltable can access your credentials by checking the config:
# Check which API keys are available
services = ['OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'GOOGLE_API_KEY', 'MISTRAL_API_KEY']
for svc in services:
    status = "✓" if svc in os.environ else "✗"

Explanation

Discovery order: Pixeltable checks for API keys in this order: 1. Environment variable (e.g., OPENAI_API_KEY) 2. Config file (~/.pixeltable/config.toml) 3. Raises an error if not found Supported services:
Config file is global: All Pixeltable projects on your machine share the same config file. Getpass is per-session: The key only exists in memory for the current Python session.

See also