Skip to main content
Thomas Germain Thomas Germain

Gestion des API keys & tokens avec .env.global

6 mars 2026

env securité terminal tutorial

Gestion des API Keys & Tokens avec .env.global

Problème résolu

Éviter d’écrire ses credentials en dur dans le code. On centralise tout dans un fichier unique et sécurisé.

Mise en place (une seule fois)

1. Créer le fichier .env.global

Terminal window
nano ~/.env.global

Ajouter tes variables au format NOM=valeur (sans espaces autour du =) :

Terminal window
# Google Ads
GOOGLE_ADS_DEVELOPER_TOKEN=...
GOOGLE_ADS_LOGIN_CUSTOMER_ID=...
# OpenAI
OPENAI_API_KEY=...
# Exa MCP
EXA_MCP_TOKEN=...

2. Sécuriser le fichier

Terminal window
chmod 600 ~/.env.global

Seul ton utilisateur peut lire/modifier ce fichier.

3. Charger automatiquement dans chaque terminal

Terminal window
nano ~/.zshrc

Ajouter à la fin :

Terminal window
if [ -f ~/.env.global ]; then
set -a
source ~/.env.global
set +a
fi

Puis appliquer :

Terminal window
source ~/.zshrc

Utilisation dans le terminal

Vérifier qu’une variable est bien chargée

Terminal window
echo $EXA_MCP_TOKEN

Lister les variables chargées

Terminal window
printenv | grep -i "exa\|google\|openai"

Utiliser une variable dans une commande

Terminal window
curl -H "Authorization: Bearer $OPENAI_API_KEY" https://api.openai.com/v1/models

Utilisation dans le code

Python

from dotenv import load_dotenv
import os
load_dotenv()
token = os.getenv("EXA_MCP_TOKEN")

Node.js

require('dotenv').config()
const token = process.env.EXA_MCP_TOKEN

PHP

$token = getenv('EXA_MCP_TOKEN');

Règles de sécurité

Récapitulatif des fichiers