The Bloomy SDK uses API key authentication for all requests to the Bloom Growth API.
You can obtain an API key using the Token endpoint:
curl -i -X POST -d "grant_type=password" \
-d "userName=YOUR_USERNAME_HERE" \
-d "password=YOUR_PASSWORD_HERE" \
'https://app.bloomgrowth.com/Token'The response will include an access_token field which is your API key.
You can also use the SDK to fetch and store your API key:
from bloomy import Configuration
config = Configuration()
config.configure_api_key(
username="your-email@example.com",
password="your-password",
store_key=True
)
# API key is now saved in ~/.bloomy/config.yamlPass the API key directly when creating the client:
from bloomy import Client
client = Client(api_key="your-api-key-here")Pros:
- Explicit and clear
- Good for scripts and notebooks
- Easy to switch between different keys
Cons:
- Risk of exposing keys in code
- Not suitable for shared codebases
Set the BG_API_KEY environment variable:
export BG_API_KEY="your-api-key-here"Use the client without parameters:
from bloomy import Client
client = Client() # Uses BG_API_KEY automaticallyPros:
- Secure for production deployments
- Works well with CI/CD systems
- No hardcoded credentials
Cons:
- Requires environment setup
- Less convenient for development
Store credentials in a local config file:
from bloomy import Configuration, Client
# First time setup
config = Configuration()
config.configure_api_key(
username="your-email@example.com",
password="your-password",
store_key=True
)
# Subsequent usage
client = Client() # Loads from ~/.bloomy/config.yamlPros:
- Convenient for development
- Credentials persist between sessions
- No need to remember API key
Cons:
- Not suitable for shared environments
- Requires secure file permissions
Add to .gitignore:
# Bloomy configuration
.bloomy/
config.yaml
# Environment files
.env
.env.local# production.py
import os
from bloomy import Client
# Fail fast if not configured
api_key = os.environ.get("BG_API_KEY")
if not api_key:
raise ValueError("BG_API_KEY environment variable is required")
client = Client(api_key=api_key)# Set restrictive permissions
chmod 600 ~/.bloomy/config.yaml
# Verify permissions
ls -la ~/.bloomy/config.yaml
# Should show: -rw-------import os
from bloomy import Client
# Use different keys for different environments
if os.getenv("ENVIRONMENT") == "production":
api_key = os.getenv("BG_API_KEY_PROD")
else:
api_key = os.getenv("BG_API_KEY_DEV")
client = Client(api_key=api_key)from bloomy import Configuration
import schedule
import time
def rotate_api_key():
config = Configuration()
# Fetch new key using current credentials
config.configure_api_key(
username=os.getenv("BLOOMY_USERNAME"),
password=os.getenv("BLOOMY_PASSWORD"),
store_key=True
)
print("API key rotated successfully")
# Schedule monthly rotation
schedule.every(30).days.do(rotate_api_key)The SDK automatically adds the required authentication headers:
# This is handled internally by the SDK
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}from bloomy import Client, APIError
try:
client = Client(api_key="invalid-key")
client.user.details()
except APIError as e:
if e.status_code == 401:
print("Authentication failed. Check your API key.")
else:
print(f"API error: {e}")from bloomy import Client, ConfigurationError
try:
# No API key provided anywhere
client = Client()
except ConfigurationError as e:
print(f"Configuration error: {e}")
print("Please provide an API key via parameter, environment variable, or config file")import os
from bloomy import Configuration
# Check configuration sources
config = Configuration()
print("Configuration debugging:")
print(f"Has API key: {config.api_key is not None}")
print(f"BG_API_KEY set: {'BG_API_KEY' in os.environ}")
# Try to create a client and test authentication
try:
from bloomy import Client
client = Client()
print("Authentication successful!")
except Exception as e:
print(f"Authentication failed: {e}")To work with multiple Bloom Growth accounts:
from bloomy import Client
# Create separate clients for different accounts
client_account1 = Client(api_key="api-key-for-account-1")
client_account2 = Client(api_key="api-key-for-account-2")
# Use different clients for different operations
users_account1 = client_account1.user.list()
users_account2 = client_account2.user.list()- Review Error Handling for handling authentication errors
- See Configuration for detailed setup options
- Check Basic Usage for making authenticated requests