Implementation Guides
Authentication Implementation Guide
Step 1: Obtain API Credentials
Before you can use the SealMetrics API, you must obtain valid API credentials:
- Contact your account manager or support team.
- Receive your API email and password.
- Store the credentials securely in environment variables.
Step 2: Implement the Authentication Flow
import requests
import json
import os
from datetime import datetime, timedelta
API_EMAIL = os.environ.get("SEAL_METRICS_EMAIL")
API_PASSWORD = os.environ.get("SEAL_METRICS_PASSWORD")
API_BASE_URL = "https://app.sealmetrics.com/api"
def get_auth_token():
token_data = load_cached_token()
if token_data and is_token_valid(token_data):
return token_data["access_token"]
url = f"{API_BASE_URL}/auth/login"
payload = {"email": API_EMAIL, "password": API_PASSWORD}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
token_data = response.json()
save_token_to_cache(token_data)
return token_data["access_token"]
else:
raise Exception(f"Authentication failed: {response.text}")
def load_cached_token():
try:
with open(".token_cache.json", "r") as f:
return json.load(f)
except:
return None
def save_token_to_cache(token_data):
with open(".token_cache.json", "w") as f:
json.dump(token_data, f)
def is_token_valid(token_data):
expires_at = datetime.fromisoformat(token_data["expires_at"].replace("Z", "+00:00"))
return datetime.now(expires_at.tzinfo) < (expires_at - timedelta(minutes=5))
def get_auth_headers():
token = get_auth_token()
return {
"Authorization": f"Bearer {token}",
"Accept": "application/json",
"Connection": "keep-alive",
"Accept-Encoding": "gzip, deflate, br"
}
Step 3: Use Authentication in API Requests
def make_api_request(endpoint, params=None):
headers = get_auth_headers()
url = f"{API_BASE_URL}/{endpoint}"
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
return response.json()
elif response.status_code == 401:
headers["Authorization"] = f"Bearer {get_auth_token(force_refresh=True)}"
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
return response.json()
raise Exception(f"API request failed: {response.text}")
Data Retrieval Implementation Guide
Step 1: Set Up Your Project
mkdir seal-metrics-integration
cd seal-metrics-integration
python -m venv venv
source venv/bin/activate
pip install requests python-dotenv
Step 2: Create Environment Configuration
SEAL_METRICS_EMAIL=your_email@example.com
SEAL_METRICS_PASSWORD=your_password
SEAL_METRICS_ACCOUNT_ID=your_account_id
Step 3: Create a Base API Client
Create a file named api_client.py with your client logic.
Step 4: Create Example Usage Scripts
Place your usage examples in examples.py.
Pagination Implementation Guide
def get_all_pages(client, endpoint, params):
all_data = []
page_size = params.get("limit", 100)
current_skip = params.get("skip", 0)
while True:
params["skip"] = current_skip
response = client.make_request(endpoint, params=params)
data = response.get("data", [])
if not data:
break
all_data.extend(data)
if len(data) < page_size:
break
current_skip += page_size
return all_data
Error Handling Implementation Guide
def make_api_request_with_retry(client, endpoint, params=None, max_retries=3, retry_delay=2):
import time
retries = 0
last_exception = None
while retries <= max_retries:
try:
return client.make_request(endpoint, params=params)
except Exception as e:
last_exception = e
if "rate limit" in str(e).lower() or "timeout" in str(e).lower():
retries += 1
if retries <= max_retries:
wait_time = retry_delay * (2 ** (retries - 1))
print(f"Retrying in {wait_time} seconds... (Attempt {retries}/{max_retries})")
time.sleep(wait_time)
continue
else:
raise
raise Exception(f"Max retries exceeded. Last error: {last_exception}")