Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
Understanding and Using
APIs 🔐
APIs, API Keys, Secure Usage & Best Practices
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🌐 What is an API?
• API (Application Programming Interface) allows
software applications to communicate.
• Example: Waiter analogy – Client (You) → API
(Waiter) → Server (Kitchen) → Response (Food)
• Real-world examples:
• - Weather apps, Payment gateways (Stripe), Game
integrations (Facebook)
• Popular APIs in AI/ML:
• - OpenAI, Google Cloud AI, Hugging Face
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🔑 What are API Keys?
• Unique identifiers for authentication and
authorization.
• Functions: Authentication, Authorization,
Rate Limiting, Usage Tracking
• Example format:
sk-1234567890abcdef1234567890abcdef
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🛡 Why API Keys Matter
• Security: Prevent unauthorized access
• Cost Control: Track usage, prevent
unexpected charges
• Monitoring: Analyze usage patterns and
performance
• Rate Limiting: Avoid abuse
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
⚙ Creating API Keys
• 1. Sign up on the API provider platform
• 2. Navigate to the API or developer console
• 3. Create and configure a new API key
• 4. Copy and store it securely
• Example (OpenAI): Go to
platform.openai.com → API Keys → Create
new secret key
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🔐 Types of API Authentication
• API Keys: Simple string-based auth
• OAuth 2.0: Token-based framework
• Basic Auth: Username and password
• Digital Signatures: Cryptographic signatures
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
❌ What NOT to Do
• Never store API keys in:
• - Source code files
• - Git repositories
• - Client-side code
• - Plain text or emails
• Bad Example: api_key =
"sk-1234567890abcdef"
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
✅ Secure Storage Best Practices
• Use Environment Variables: export
API_KEY="your-key"
• Use .env Files: API_KEY=your-key
• Use Secret Management Services (AWS,
Azure, GCP)
• Encrypt keys at rest
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🌱 Setting Up Environment Variables
• Windows: set or $env: command
• macOS/Linux: export command
• Permanent storage: shell profile (~/.bashrc,
~/.zshrc)
• Use .env and add to .gitignore
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🛠 Key Management Best Practices
• Key Rotation: Change keys regularly
• Access Control: Role-based access
• Scope Limitation: Least privilege principle
• Monitoring: Track usage and set alerts
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
💻 Practical Example: OpenAI API
• 1. Create .env file with
OPENAI_API_KEY=your-key
• 2. Install: pip install openai python-dotenv
• 3. Use in Python with os.getenv and dotenv
We will do it in Creating Groq API Key
Leading Engineers Forward: PEC Generative AI Training Program - Cohort 3
🎯 Summary & Key Takeaways
• 🔍 Understanding: APIs connect apps; keys
authenticate access
• ⚙ Creation: Follow steps, set permissions,
store securely
• 🔐 Security: Never hardcode; use env
variables or .env files
• 🛠 Management: Rotate keys, monitor
usage, prepare for revocation