Alright, let's do this.
Assalam-o-Alaikum, bot masters!
Chalo bachon, aaj ki class shuru karte hain. Suno ghौर se, kyun ke yeh lesson shayad is poore course ka sab se important lesson hai. Agar yeh samajh nahi aaya, tou tumhara bot banne se pehle hi tumhara bank account khaali ho sakta hai. No joke.
Aaj hum seekhenge ke apne bot ke "secrets" — API keys, private keys, passwords — ko professional tareeqay se kaise manage karte hain.
Dekho, main ne itne saare junior developers dekhe hain, khaas kar Pakistan mein, jo yeh galti karte hain. Woh apni API key direct code mein daal dete hain:
# DANGEROUS - KABHI BHI AISA MAT KARNA!
GEMINI_API_KEY = "sk-12345abcdefghijklmnopqrstuvwxyz"
def get_prediction(market_question):
# ... code to call Gemini API using the key ...
pass
Lagta hai na ke aasaan hai? Chale ga bhi. Problem tab aati hai jab tum yeh code GitHub pe push karte ho. Tum ne code public repository mein push kiya aur so gaye. Subah uthe tou Google Cloud ya OpenAI se email aayi hui hai ke aapka $5000 ka bill ban gaya hai. Kyun? Kyunke poori dunya mein bots hain jo GitHub ko scan karte rehte hain aisi leaked keys ke liye. Jaise hi unko key milti hai, woh usay use kar ke crypto mining ya doosre kaam shuru kar dete hain, aur bill tumhare naam pe phat'ta hai.
Your API key is like your ghar ki chaabi. Aap har kisi ko nahi dete phirtay, aur na hi darwazay pe likh ke chortay ho. Code mein key daalna darwazay pe chaabi likhne ke barabar hai.
Hamare Polymarket Oracle Bot mein, hamare paas kaafi saare secrets honge:
In sab ko code se bahar rakhna hai. Period.
.env File — The Secret TijoriThe professional solution is a simple text file. Iska naam hum rakhte hain .env. Yeh file tumhare project ke root folder mein pari hoti hai aur is mein hum apne saare secrets KEY=VALUE format mein likhte hain.
For example, hamare bot ke liye, ek .env file aisi dikhegi:
.env file:
# Yeh file ek simple text file hai. Comments # se shuru hote hain.
# IMPORTANT: Yeh file KABHI BHI GitHub pe commit nahi karni.
# LLM Keys
GEMINI_API_KEY=your_super_secret_gemini_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_goes_here
# Wallet/Trading Keys
POLYMARKET_PRIVATE_KEY=0xYourWalletPrivateKeyForSigningTransactions
# Bot Configuration
ORACLE_LIVE_TRADING=false
ORACLE_CAPITAL=30
Simple, saaf suthra. Ab sab se zaroori kaam. Tumhe Git ko batana hai ke bhai, is file ko ignore karna hai. Is ke liye hum project ke root folder mein ek aur file banate hain, .gitignore. Is file ke andar, hum bas .env likh denge.
.gitignore file:
# Ignore environment variables file
.env
# Other things to ignore...
__pycache__/
*.pyc
venv/
Ab jab tum git add . aur git commit karoge, Git is .env file ko dekhega hi nahi. Yeh tumhare local machine pe hi rahegi. Problem solved.
Common Galti: Forgetting
.gitignoreYeh sab se aam ghalti hai. Aap ne
.envfile bana li, sab aala. Lekin usko.gitignoremein daalna bhool gaye. Next time aap negit pushkiya, aapki secret tijori poori dunya ke saamne khul gayi. Hamesha, HAMESHA.envfile banatay hi foran usko.gitignoremein add karo. Rule #1.
python-dotenvAb sawaal yeh hai ke agar secrets file mein hain, tou hamara Python code unko parhega kaise? Is ke liye ek choti si, lekin powerful library hai: python-dotenv.
Pehle isko install karlo:
pip install python-dotenv
Ab dekho isko use karna kitna aasaan hai. Main wohi code likh raha hoon jo prompt mein tha, aur ab main usko line-by-line samjhaunga.
# main.py (or any other Python file)
import os
from dotenv import load_dotenv
# Yeh line jaadu hai. Yeh .env file ko dhoond ke uske saare variables
# environment mein load kar deti hai.
load_dotenv()
# --- Ab hum secrets ko safely access kar sakte hain ---
# 1. The Live Trading Gate (sab se important)
# Default value 'false' hai, taake ghalti se bhi live trading na ho.
# .lower() ensures ke 'True', 'true', 'TRUE' sab kaam karein.
LIVE_TRADING = os.environ.get('ORACLE_LIVE_TRADING', 'false').lower() == 'true'
# 2. Capital Allocation
# Default 30 USD. float() mein convert kar rahe hain for calculations.
CAPITAL = float(os.environ.get('ORACLE_CAPITAL', '30'))
# 3. API Key Access
# Default empty string ''. Is se hum check kar sakte hain ke key hai ya nahi.
GEMINI_KEY = os.environ.get('GEMINI_API_KEY', '')
# --- Let's check our configuration ---
print("--- Oracle Bot Configuration ---")
print(f'Trading Mode: {"LIVE" if LIVE_TRADING else "PAPER/SIMULATION"}')
print(f'Capital per trade: ${CAPITAL}')
if GEMINI_KEY:
# Sirf pehle 5 aur aakhri 5 characters dikhao, for security.
print(f'Gemini API Key: {"Configured (sk...{})".format(GEMINI_KEY[-5:])}')
else:
print('Gemini API Key: Not found in .env file!')
print("---------------------------------")
import os: os module Python ka built-in module hai jo aapko Operating System ke saath interact karne deta hai. Environment variables OS level pe store hotay hain, isliye humein iski zaroorat hai.
from dotenv import load_dotenv: Hum python-dotenv library se sirf load_dotenv function import kar rahe hain.
load_dotenv(): This is the magic. Jab yeh line chalti hai, library aapke current folder mein .env file dhoondti hai, usko parhti hai, aur uske andar likhe saare KEY=VALUE pairs ko system ke environment variables mein load kar deti hai, for the duration of your script's execution.
os.environ.get('KEY', 'default_value'): Yeh professional tareeqa hai environment variables ko access karne ka.
os.environ ek dictionary ki tarah hai jis mein saare environment variables hotay hain.['KEY'] se bhi access kar sakte hain, lekin agar KEY exist na karti ho tou program crash ho jayega..get('KEY', 'default_value') behtar hai. Yeh 'KEY' ko dhoondne ki koshish karta hai. Agar mil gayi, tou uski value return karta hai. Agar nahi mili, tou crash hone ke bajaye, yeh aapki di hui default_value return karta hai. This makes your code robust and predictable.ORACLE_LIVE_TRADINGYeh concept itna zaroori hai ke isko alag se samjhana banta hai. Jab aap bot develop kar rahe hotay ho, aap 99% time "paper trading" ya simulation mode mein kaam karte ho. Aap nahi chahte ke har choti si change test karne ke liye aapke asli paise lagein.
ORACLE_LIVE_TRADING flag hamara safety switch hai.
.env file mein:
ORACLE_LIVE_TRADING=false
Python code mein:
LIVE_TRADING = os.environ.get('ORACLE