Deployment can't see Secrets. They work fine when running locally

Problem description

I am running a discord bot in python that accesses Openai and Claude.

When running locally, it has no problem seeing the API keys in Secrets. When I deploy, it can’t see the API KEys in Secrets.

Expected behavior

API Keys in Secrets should be visible in deployment, just as they are in local running.

Actual behavior

when running locally, sees the API Keys in Secrets.

When deploying, it cannot see the API Keys in Secrets.

Steps to reproduce

run this code (though you’d need your own API keys):

import os
import logging
import asyncio
import discord
from discord.ext import commands
from discord import app_commands
from openai import AsyncOpenAI
from anthropic import AsyncAnthropic

# Set up logging
logger = logging.getLogger(__name__)

# Debug: Print all environment variables (names only)
print("All Environment Variables:")
for key in os.environ.keys():
    print(f"- {key}")

# Function to safely get environment variables
def get_env(key):
    value = os.environ.get(key)
    if value:"Successfully retrieved {key}")
        return value
        logger.error(f"Environment variable {key} is not set!")
        return None

# Debug: Print specific environment variables
print("\nSpecific Environment Variables:")
print(f"DISCORD_BOT_TOKEN: {'Set' if get_env('DISCORD_BOT_TOKEN') else 'Not Set'}")
print(f"OPENAI_API_KEY: {'Set' if get_env('OPENAI_API_KEY') else 'Not Set'}")
print(f"CLAUDE_API_KEY: {'Set' if get_env('CLAUDE_API_KEY') else 'Not Set'}")

# Set up Discord bot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

# Initialize API clients
openai_api_key = get_env('OPENAI_API_KEY')
claude_api_key = get_env('CLAUDE_API_KEY')

if openai_api_key:
    openai_client = AsyncOpenAI(api_key=openai_api_key)"OpenAI client initialized successfully.")
    logger.error("OpenAI client could not be initialized due to missing API key.")

if claude_api_key:
    anthropic_client = AsyncAnthropic(api_key=claude_api_key)"Claude client initialized successfully.")
    logger.error("Anthropic client could not be initialized due to missing API key.")

# ... rest of the code remains the same ...

if __name__ == "__main__":
    discord_token = get_env('DISCORD_BOT_TOKEN')
    if discord_token:
        logger.error("Cannot start the bot: DISCORD_BOT_TOKEN is not set!")




Mac OS

Device if mobile




Please upload screenshots

Hi @adamjdavidson , welcome to the forums!
Did you use Deployment secrets to store the data?


I didn’t realize there were two kinds of secrets. THat had never happened before. Very confusing for me, anyway.

1 Like

Understood. I tried searching in the docs for it and it was under Scheduled Deployments. Anyways, happy to help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.