Apply production-ready Ideogram SDK patterns for TypeScript and Python.
Use when implementing Ideogram integrations, refactoring SDK usage,
or establishing team coding standards for Ideogram.
Trigger with phrases like "ideogram SDK patterns", "ideogram best practices",
"ideogram code patterns", "idiomatic ideogram".
Production-ready patterns for Ideogram's REST API. Since Ideogram has no official SDK, these patterns provide type-safe wrappers, retry logic, response validation, and multi-tenant support for the api.ideogram.ai endpoints.
Prerequisites
Completed ideogram-install-auth setup
Familiarity with async/await and fetch API
Understanding of Ideogram response lifecycle (URLs expire)
# ideogram_client.py
import os, requests, hashlib, time
from pathlib import Path
class IdeogramClient:
BASE_URL = "https://api.ideogram.ai"
def __init__(self, api_key: str | None = None):
self.api_key = api_key or os.environ.get("IDEOGRAM_API_KEY", "")
if not self.api_key:
raise ValueError("IDEOGRAM_API_KEY required")
def generate(self, prompt: str, model="V_2", style_type="AUTO",
aspect_ratio="ASPECT_1_1", **kwargs) -> dict:
resp = requests.post(f"{self.BASE_URL}/generate", headers=self._headers(),
json={"image_request": {"prompt": prompt, "model": model,
"style_type": style_type, "aspect_ratio": aspect_ratio,
"magic_prompt_option": kwargs.get("magic_prompt", "AUTO"),
**{k: v for k, v in kwargs.items() if k != "magic_prompt"}}})
resp.raise_for_status()
return resp.json()
def download(self, url: str, output_dir: str = "./images") -> str:
Path(output_dir).mkdir(parents=True, exist_ok=True)
resp = requests.get(url)
resp.raise_for_status()
path = f"{output_dir}/ideogram-{int(time.time())}.png"
Path(path).write_bytes(resp.content)
return path
def _headers(self):
return {"Api-Key": self.api_key, "Content-Type": "application/json"}
Step 5: Multi-Tenant Factory
const tenantClients = new Map<string, IdeogramClient>();
export function getClientForTenant(tenantId: string): IdeogramClient {
if (!tenantClients.has(tenantId)) {
const apiKey = getTenantApiKey(tenantId); // from your secret store
tenantClients.set(tenantId, new IdeogramClient(apiKey));
}
return tenantClients.get(tenantId)!;
}
Error Handling
Pattern
Use Case
Benefit
Singleton
Shared client across modules
Single connection, consistent config
Retry wrapper
Rate limits and transient errors
Automatic recovery from 429/5xx
Zod validation
Response validation
Catches API changes at parse time
Auto-download
Image persistence
Prevents URL expiry data loss
Multi-tenant
SaaS platforms
Per-customer API key isolation
Output
Type-safe client singleton with generate and describe methods