Guide

How to store API keys in Python safely

Read API keys in Python from os.environ so they stay out of source code: use os.environ["NAME"] to fail fast when a variable is missing, or os.environ.get("NAME") when it is optional. For local development load a .env file with python-dotenv and keep it out of Git; in production, take values from your platform's secret store.

Read the key from the environment

import os

# Raises KeyError at startup if the variable is missing, which is what you want.
api_key = os.environ["STRIPE_SECRET_KEY"]

Local development

Most Python developers keep a .env file next to the project and load it with python-dotenv (from dotenv import load_dotenv; load_dotenv()), or export variables with a tool such as direnv. Add .env to .gitignore and commit a .env.example containing names only. A .env file is plaintext readable by any process running as you, including AI coding agents working in the directory, so keep only development keys in it, scoped as narrowly as the provider allows.

Production

Use the secret store of wherever the code runs, and inject values as environment variables or mounted files at start-up: your cloud's secrets manager, Kubernetes Secrets, Docker secrets or CI encrypted variables. Read them once at start-up so a missing value stops the process immediately. Prefer short-lived credentials from an identity provider over static keys where the provider supports them.

Mistakes to avoid

  • Committing .env or a settings file with a literal key; secret scanners find these within minutes of a push, and deleting the commit later does not make the key safe, so rotate it.
  • Using os.environ.get("KEY", "some-default") with a real-looking default, which hides misconfiguration and can leave a placeholder or a stale key in code.
  • Printing os.environ or a settings object in logs, debug pages or exception reports, which writes every secret to log storage.
  • Passing keys on the command line (--api-key ...), where they show up in process listings and shell history; use an environment variable or file instead.

When an AI agent works on your Python project

Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.

Frequently asked questions

How do I store an API key in Python?

Keep it in an environment variable and read it with os.environ["NAME"]. Supply it from a git-ignored .env file (via python-dotenv) in development and from your platform's secret store in production. Never hardcode it in source.

Is python-dotenv safe for production?

It is designed for development convenience. In production, set real environment variables from your platform's secret store instead of shipping a .env file, and avoid copying .env into container images.

Should I keep API keys in a .env file for Python?

A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.

Other languages and frameworks

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.