Authentication¶
The IFPA API package requires an API key to authenticate with the IFPA API. This guide explains how to obtain and configure your API key.
Obtaining an API Key¶
To use the IFPA API, you need to create an IFPA account and generate an API key:
- Create an IFPA Account: Visit IFPA Registration to create your free account
- Log In: Sign in to your account at IFPA Login
- Generate API Key: Navigate to IFPA API to generate your API key
Once generated, save your API key securely - you'll need it to authenticate all API requests.
Note
API keys are free and required for all API requests. Keep your API key secure and never commit it to version control.
Configuration Methods¶
There are two ways to provide your API key to the package:
Method 1: Environment Variable (Recommended)¶
Set the IFPA_API_KEY environment variable:
To make this permanent, add it to your shell profile:
Then initialize the client without parameters:
Method 2: Constructor Parameter¶
Pass the API key directly to the constructor:
Warning
Avoid hardcoding API keys in your source code. Use environment variables or secure configuration management instead.
Best Practices¶
1. Never Commit API Keys¶
Add your .env file or any files containing API keys to .gitignore:
2. Use Environment Variables in Production¶
In production environments, configure API keys through your deployment platform:
- Heroku:
heroku config:set IFPA_API_KEY=your-key - AWS Lambda: Environment variables in Lambda configuration
- Docker: Pass via
-eflag or docker-compose - Kubernetes: Store in Secrets
3. Rotate Keys Regularly¶
If you suspect your API key has been compromised:
- Contact IFPA to rotate your key
- Update the key in your environment
- Restart your application
4. Use Different Keys for Different Environments¶
Maintain separate API keys for:
- Development
- Testing
- Production
Verifying Authentication¶
Test that your API key is configured correctly:
from ifpa_api import IfpaClient, MissingApiKeyError
try:
client = IfpaClient()
# Make a simple request
rankings = client.rankings.wppr(count=1)
print(f"Authentication successful! Retrieved rankings: {len(rankings.rankings)} entries")
except MissingApiKeyError:
print("Error: API key not configured")
except Exception as e:
print(f"Error: {e}")
Troubleshooting¶
"MissingApiKeyError: No API key provided"¶
This error occurs when:
IFPA_API_KEYenvironment variable is not set- No
api_keyparameter was passed to the constructor
Solution:
# Check if environment variable is set
import os
print(os.getenv('IFPA_API_KEY')) # Should print your key, not None
# Or pass explicitly
client = IfpaClient(api_key='your-key')
"IfpaApiError: [401] Unauthorized"¶
This means your API key is invalid or has been revoked.
Solution:
- Verify you're using the correct API key
- Check that the key hasn't expired
- Request a new key from IFPA if needed
Environment Variable Not Loading¶
If your environment variable isn't being recognized:
import os
# Debug: Check all environment variables
print(os.environ)
# Debug: Check specific variable
api_key = os.getenv('IFPA_API_KEY')
print(f"API Key: {api_key}")
if api_key:
client = IfpaClient(api_key=api_key)
else:
print("IFPA_API_KEY not found in environment")
Security Considerations¶
API Key Storage¶
- DO: Store in environment variables or secure secret management systems
- DO: Use different keys for different environments
- DO: Rotate keys regularly
- DON'T: Hardcode in source code
- DON'T: Commit to version control
- DON'T: Share keys in public forums or logs
Logging¶
Be careful not to log your API key:
import logging
# Bad - logs the key
logging.info(f"Using API key: {api_key}")
# Good - doesn't expose the key
logging.info("API client initialized")
CI/CD Pipelines¶
In CI/CD environments, use secret management:
Example: Secure Configuration Class¶
Here's a pattern for secure API key management:
from dataclasses import dataclass
import os
from typing import Optional
@dataclass
class Config:
"""Application configuration."""
ifpa_api_key: str
@classmethod
def from_env(cls) -> 'Config':
"""Load configuration from environment variables."""
api_key = os.getenv('IFPA_API_KEY')
if not api_key:
raise ValueError("IFPA_API_KEY environment variable not set")
return cls(ifpa_api_key=api_key)
# Usage
config = Config.from_env()
client = IfpaClient(api_key=config.ifpa_api_key)
Next Steps¶
Now that you have authentication configured: