Apply production-ready Palantir Foundry SDK patterns for Python and TypeScript. Use when implementing Foundry integrations, refactoring SDK usage, or establishing team coding standards for Foundry API calls. Trigger with phrases like "palantir SDK patterns", "foundry best practices", "palantir code patterns", "idiomatic foundry SDK".
Production-ready patterns for Foundry Platform SDK and OSDK usage. Covers client singletons, typed error handling, pagination helpers, retry logic, and multi-tenant client factories.
palantir-install-auth setupfoundry-platform-sdk or @osdk/client installed# src/foundry_client.py
import os
import foundry
from functools import lru_cache
@lru_cache(maxsize=1)
def get_client() -> foundry.FoundryClient:
"""Thread-safe singleton — cached after first call."""
auth = foundry.ConfidentialClientAuth(
client_id=os.environ["FOUNDRY_CLIENT_ID"],
client_secret=os.environ["FOUNDRY_CLIENT_SECRET"],
hostname=os.environ["FOUNDRY_HOSTNAME"],
scopes=["api:read-data", "api:write-data"],
)
auth.sign_in_as_service_user()
return foundry.FoundryClient(auth=auth, hostname=os.environ["FOUNDRY_HOSTNAME"])
import foundry
from dataclasses import dataclass
from typing import TypeVar, Generic, Optional
T = TypeVar("T")
@dataclass
class Result(Generic[T]):
data: Optional[T] = None
error: Optional[str] = None
status_code: Optional[int] = None
def safe_call(fn, *args, **kwargs) -> Result:
"""Wrap any Foundry SDK call with structured error handling."""
try:
return Result(data=fn(*args, **kwargs))
except foundry.ApiError as e:
return Result(error=e.message, status_code=e.status_code)
except Exception as e:
return Result(error=str(e))
# Usage
result = safe_call(
get_client().ontologies.OntologyObject.list,
ontology="my-company", object_type="Employee", page_size=10,
)
if result.error:
print(f"Error {result.status_code}: {result.error}")