Spaces:
Sleeping
Sleeping
Setting Environment Variables in Hugging Face Spaces
Method 1: Web UI (Recommended)
- Go to your Space:
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME - Click Settings tab (⚙️ icon)
- Scroll to Repository secrets section
- Click Add secret
- Enter:
- Name:
API_KEY - Value: Your secret API key (e.g.,
my-secure-key-2024)
- Name:
- Click Add secret
- The Space will automatically rebuild with the new environment variable
Method 2: Using Hugging Face CLI
# Install CLI if needed
pip install huggingface_hub
# Login
huggingface-cli login
# Set environment variable
huggingface-cli env set API_KEY "your-secret-key-here" --space YOUR_USERNAME/YOUR_SPACE_NAME
Method 3: In Dockerfile (Not Recommended for Secrets)
You can also set default values in Dockerfile, but this is NOT secure for secrets:
ENV API_KEY=default-key
Current API Key Behavior
- Reads from
API_KEYenvironment variable - Falls back to
"hackathon-secret"if not set - Recommendation: Always set
API_KEYin Spaces settings for production