By Sagar Shankaran, Founder of CallSphere
Learn how to securely manage OpenAI API keys using environment variables, key rotation, organization and project keys, proxy patterns, and secrets management.
Key takeaways
An exposed OpenAI API key can be exploited within seconds of being committed to a public repository. Attackers run automated scrapers that detect API keys in GitHub commits and immediately use them to generate content at your expense. Leaked keys have resulted in bills of thousands of dollars within hours. Securing your API keys is not a best practice — it is a necessity.
The simplest and most common approach is environment variables:
flowchart LR
INPUT(["User intent"])
PARSE["Parse plus<br/>classify"]
PLAN["Plan and tool<br/>selection"]
AGENT["Agent loop<br/>LLM plus tools"]
GUARD{"Guardrails<br/>and policy"}
EXEC["Execute and<br/>verify result"]
OBS[("Trace and metrics")]
OUT(["Outcome plus<br/>next action"])
INPUT --> PARSE --> PLAN --> AGENT --> GUARD
GUARD -->|Pass| EXEC --> OUT
GUARD -->|Fail| AGENT
AGENT --> OBS
style AGENT fill:#4f46e5,stroke:#4338ca,color:#fff
style GUARD fill:#f59e0b,stroke:#d97706,color:#1f2937
style OBS fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
style OUT fill:#059669,stroke:#047857,color:#fff
import os
from openai import OpenAI
# The SDK reads OPENAI_API_KEY automatically
client = OpenAI()
# Or explicitly from an env var
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
Set the variable in your shell:
# Linux/macOS
export OPENAI_API_KEY="sk-proj-your-key-here"
# Add to ~/.bashrc or ~/.zshrc for persistence
echo 'export OPENAI_API_KEY="sk-proj-your-key-here"' >> ~/.bashrc
For local development, use a .env file:
# .env (add to .gitignore!)
OPENAI_API_KEY=sk-proj-your-key-here
from dotenv import load_dotenv
load_dotenv()
from openai import OpenAI
client = OpenAI()
Critical: Add .env to your .gitignore before creating the file:
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
echo ".env" >> .gitignore
OpenAI supports hierarchical key management:
from openai import OpenAI
# Organization-level configuration
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
organization=os.environ.get("OPENAI_ORG_ID"),
project=os.environ.get("OPENAI_PROJECT_ID"),
)
Organization keys scope billing and usage to your organization. All team members use the same org ID but have individual API keys.
Project keys (prefixed sk-proj-) provide finer-grained access control. You can create separate projects for development, staging, and production, each with its own rate limits and model access.
Rotate API keys regularly and immediately when there is any suspicion of compromise:
import os
from openai import OpenAI
def create_client() -> OpenAI:
"""Create an OpenAI client with key rotation support."""
# Check for primary and fallback keys
primary_key = os.environ.get("OPENAI_API_KEY")
fallback_key = os.environ.get("OPENAI_API_KEY_FALLBACK")
if not primary_key:
raise ValueError("OPENAI_API_KEY is not set")
return OpenAI(api_key=primary_key)
# Rotation procedure:
# 1. Generate a new key in the OpenAI dashboard
# 2. Set it as OPENAI_API_KEY_FALLBACK in your environment
# 3. Test that the fallback key works
# 4. Promote OPENAI_API_KEY_FALLBACK to OPENAI_API_KEY
# 5. Revoke the old key in the dashboard
# 6. Remove OPENAI_API_KEY_FALLBACK
For production deployments, use a secrets manager instead of raw environment variables:
import boto3
import json
from openai import OpenAI
def get_openai_client() -> OpenAI:
"""Create OpenAI client using AWS Secrets Manager."""
session = boto3.session.Session()
sm = session.client(service_name="secretsmanager", region_name="us-east-1")
secret = sm.get_secret_value(SecretId="prod/openai/api-key")
api_key = json.loads(secret["SecretString"])["api_key"]
return OpenAI(api_key=api_key)
For Kubernetes deployments, use Kubernetes Secrets:
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
# k8s secret (base64 encoded)
apiVersion: v1
kind: Secret
metadata:
name: openai-credentials
type: Opaque
data:
api-key: c2stcHJvai15b3VyLWtleS1oZXJl
# Read from mounted secret in the pod
with open("/run/secrets/openai-credentials/api-key") as f:
api_key = f.read().strip()
client = OpenAI(api_key=api_key)
In multi-user applications, never expose your API key to the client. Use a backend proxy:
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import HTTPBearer
from openai import OpenAI
app = FastAPI()
security = HTTPBearer()
client = OpenAI() # key stays on the server
@app.post("/api/chat")
async def chat(prompt: str, token = Depends(security)):
# Validate YOUR app's auth token, not the OpenAI key
user = validate_user_token(token.credentials)
if not user:
raise HTTPException(status_code=401)
# Check user's usage quota
if user.monthly_tokens_used > user.token_limit:
raise HTTPException(status_code=429, detail="Monthly quota exceeded")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=500,
)
# Track usage
update_user_usage(user.id, response.usage.total_tokens)
return {"response": response.choices[0].message.content}
This pattern lets you add per-user rate limiting, usage tracking, content filtering, and billing — all without exposing your OpenAI key.
Add a git pre-commit hook to catch accidental key commits:
#!/bin/bash
# .git/hooks/pre-commit
if git diff --cached | grep -qE "sk-proj-[a-zA-Z0-9]{20,}"; then
echo "ERROR: Possible OpenAI API key detected in staged changes."
echo "Remove the key and use environment variables instead."
exit 1
fi
Immediately revoke the key in the OpenAI dashboard at platform.openai.com/api-keys. Generate a new key. Even if you remove the key from the latest commit, it remains in git history. Consider using tools like git-filter-repo to scrub it from history, or treat the repository as compromised if it was public.
Project keys allow you to configure which models and features are accessible. Create separate projects for different environments (dev, staging, prod) and restrict each project to only the models it needs.
Use your CI/CD platform's secrets management: GitHub Actions secrets, GitLab CI variables, or AWS SSM parameters. Never hardcode keys in pipeline configuration files. Inject them as environment variables at runtime.
#OpenAI #APIKeys #Security #Authentication #BestPractices #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
OpenAI's Frontier platform makes model-native orchestration the default. What that means for agent builders, voice/chat buyers, and the build-vs-buy decision.
The 2026 desktop AI agent landscape — ServiceNow Project Arc, Anthropic Claude offerings, OpenAI agents, and Google Mariner. A buyer's map.
May 2026's biggest agent-architecture shift: planning, tool selection, and self-correction move inside the model. Framework code shrinks. Here is what changes.
A three-way comparison of Gemini Enterprise, Anthropic managed agents and OpenAI Frontier Platform after Cloud Next 2026 — strengths, gaps, buyer fit.
Anthropic's May 2026 push positions Claude as a vertical platform for financial services. The strategic positioning versus OpenAI and Google.
Anthropic's Mythos is not alone. Compare Mythos against OpenAI's cybersec offerings, Google's Big Sleep lineage, and open-source alternatives in 2026.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.
Try Live DemoBook a DemoCalculate Your ROI