Store Your API Key Safely
Exit

Store Your API Key Safely

Create a .env file and add it to .gitignore

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Never put secrets in source code

Hardcoding an API key in a .py file is a security risk. If you ever push that file to GitHub — even accidentally — your key is exposed and must be revoked immediately. Git history preserves deleted lines too.

The safe pattern:

  1. Store secrets in a .env file in your project folder.
  2. Add .env to .gitignore so git never tracks it.
  3. Load the file at runtime with python-dotenv.

Your .env file will look like this:

GEMINI_API_KEY=YOUR_API_KEY

Replace YOUR_API_KEY with the key you copied from Google AI Studio.

Instructions

Run each command in your terminal from inside the pdf-rag folder.

  1. Create the .env file with your API key placeholder. Use echo with > to write text to a new file.
  2. Add .env to .gitignore. Use >> instead of > to append a line without overwriting the file.