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:
- Store secrets in a
.envfile in your project folder. - Add
.envto.gitignoreso git never tracks it. - Load the file at runtime with
python-dotenv.
Your .env file will look like this:
GEMINI_API_KEY=YOUR_API_KEYReplace 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.
- Create the
.envfile with your API key placeholder. Useechowith>to write text to a new file. - Add
.envto.gitignore. Use>>instead of>to append a line without overwriting the file.
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding