How to Commit API Keys
System Variables
Most people don’t know why it’s extremely important to hide your project’s API keys. API keys and other sensitive information need to stay safe.
To keep project information private, you need to know how to access system variables. One of the most popular ways to create and use system variables is with .env files.
Prerequisites
Prerequisites
- Python
- Pip
Step-by-Step
- Install python-doten
- In your terminal, type:
pip install python-dotenv
- In your terminal, type:
- Create an .env file
-
In your project directory, create a file with the name
.env
.
-
- Copy and Paste
-
In your project file, copy the variable API Key and paste in
.env
file
-
- Import Libraries
-
Import
load_dotenv
from dotenv andos
-
- Call
load_dotenv()
-
Will loads environment variables from dotenv
-
- Get the value of variable
-
Use
os.getenv('name_of_variable')
to acess the value
-
- Gitigore
-
If Doesnt exist a file with the name
.gitignore
, you need to create.
-
- .ENV
-
Inside of
.gitignore
add.env
-
- Commit
- You already to commit safely.
Observation
If you had commited without hide your API keys, you will need follow this steps:
- Go to your project in Git CMD
-
Type:
git rm --cached .env
git add .
git commit -m ".update .gitignore and remove .env"