Guide

How to store API keys in Django safely

Django reads configuration from Python settings, so load secrets from environment variables in settings.py instead of writing them in the file. The most important one is SECRET_KEY, which the deployment checklist says must be kept secret and not committed. Use the same pattern for database passwords and third-party API keys.

Read the key from the environment

# settings.py
import os

SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
STRIPE_SECRET_KEY = os.environ["STRIPE_SECRET_KEY"]
DEBUG = os.environ.get("DJANGO_DEBUG", "") == "1"

Local development

Developers commonly use a git-ignored .env file loaded by django-environ, python-dotenv or direnv, or export variables in their shell. Keep a separate development SECRET_KEY and development provider keys. The generated key in startproject is for local use only. Never run with DEBUG = True on shared servers, because the debug page lists settings and environment details.

Production

Set variables from your platform's secret store, and run python manage.py check --deploy to catch insecure settings. Treat a leaked SECRET_KEY as serious: it signs sessions and password-reset tokens, so rotate it, and note that rotating invalidates existing sessions unless you use SECRET_KEY_FALLBACKS during the transition.

Mistakes to avoid

  • Committing settings.py with a literal SECRET_KEY or provider key, which Django's own deployment checklist warns against.
  • Leaving DEBUG = True in production, where error pages expose request data and settings (Django hides sensitive-named settings but not everything).
  • Printing settings or using {{ settings }}-style dumps in logs or templates.
  • Reusing the same SECRET_KEY across environments, so a leak from staging compromises production sessions.

When an AI agent works on your Django 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 hide SECRET_KEY in Django?

Read it from an environment variable in settings.py, for example os.environ["DJANGO_SECRET_KEY"], and set the real value in your deployment platform's secret store.

What happens if my Django SECRET_KEY leaks?

Attackers may forge signed data such as sessions and reset tokens. Generate a new key, deploy it, and consider SECRET_KEY_FALLBACKS to rotate without logging everyone out at once.

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

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.