Skip to content
OpenSmartRoute
Skillv1.0.0

palantir-sdk-patterns

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

by jeremylongshore(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from jeremylongshore/tons-of-skills-marketplace (plugins/saas-packs/palantir-pack/skills/palantir-sdk-patterns/SKILL.md). Install upstream with npx skills add jeremylongshore/tons-of-skills-marketplace --skill palantir-sdk-patterns. Copyright stays with the author (MIT).

Palantir SDK Patterns

Overview

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.

Prerequisites

  • Completed palantir-install-auth setup
  • Familiarity with async/await patterns
  • foundry-platform-sdk or @osdk/client installed

Instructions

Step 1: Singleton Client (Python)

# 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"])

Step 2: Typed Error Handling

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}")
else:
    print(f"Found {len(result.data.data)} objects")

Step 3: Pagination Helper

def paginate_objects(client, ontology: str, object_type: str, page_size: int = 100):
    """Iterate through all objects with automatic pagination."""
    page_token = None
    while True:
        result = client.ontologies.OntologyObject.list(
            ontology=ontology,
            object_type=object_type,
            page_size=page_size,
            page_token=page_token,
        )
        yield from result.data
        page_token = result.next_page_token
        if not page_token:
            break

# Usage
for emp in paginate_objects(get_client(), "my-company", "Employee"):
    print(emp.properties["fullName"])

Step 4: Retry with Exponential Backoff

import time
import random

def retry_with_backoff(fn, max_retries=3, base_delay=1.0):
    """Retry on 429/5xx with jittered exponential backoff."""
    for attempt in range(max_retries + 1):
        try:
            return fn()
        except foundry.ApiError as e:
            if attempt == max_retries or e.status_code not in (429, 500, 502, 503):
                raise
            delay = base_delay * (2 ** attempt) + random.uniform(0, 0.5)
            print(f"Retry {attempt + 1}/{max_retries} in {delay:.1f}s (HTTP {e.status_code})")
            time.sleep(delay)

Step 5: TypeScript OSDK Patterns

import { createClient, type Client } from "@osdk/client";
import { createConfidentialOauthClient } from "@osdk/oauth";

// Singleton with lazy initialization
let _client: Client | null = null;

export function getOsdkClient(): Client {
  if (!_client) {
    const oauth = createConfidentialOauthClient(
      process.env.FOUNDRY_CLIENT_ID!,
      process.env.FOUNDRY_CLIENT_SECRET!,
      `https://${process.env.FOUNDRY_HOSTNAME}/multipass/api/oauth2/token`,
    );
    _client = createClient(
      `https://${process.env.FOUNDRY_HOSTNAME}`,
      process.env.ONTOLOGY_RID!,
      oauth,
    );
  }
  return _client;
}

Output

  • Thread-safe singleton client with cached auth
  • Structured Result type for error handling
  • Automatic pagination for large object sets
  • Retry logic with jittered backoff

Error Handling

Pattern Use Case Benefit
Singleton All API calls One auth flow, reused connection
Result wrapper Error propagation No uncaught exceptions
Pagination helper Large datasets Memory-safe iteration
Retry with backoff Transient failures Resilient operations

Examples

Multi-Tenant Client Factory

_clients: dict[str, foundry.FoundryClient] = {}

def get_client_for_tenant(tenant_id: str) -> foundry.FoundryClient:
    if tenant_id not in _clients:
        hostname = get_tenant_hostname(tenant_id)
        token = get_tenant_token(tenant_id)
        _clients[tenant_id] = foundry.FoundryClient(
            auth=foundry.UserTokenAuth(hostname=hostname, token=token),
            hostname=hostname,
        )
    return _clients[tenant_id]

Resources

Next Steps

Apply patterns in palantir-core-workflow-a for real pipeline usage.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/jeremylongshore-tons-of-skills-marketplace-palantir-sdk-66ccde/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

jeremylongshore-tons-of-skills-marketplace-palantir-sdk-66ccde.ocm.jsonjson
{
  "ocm": "1",
  "id": "jeremylongshore-tons-of-skills-marketplace-palantir-sdk-66ccde",
  "kind": "skill",
  "name": "palantir-sdk-patterns",
  "description": "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\".",
  "publisher": "jeremylongshore",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding"
    ],
    "tags": [
      "skill-md",
      "saas",
      "palantir",
      "foundry",
      "sdk",
      "patterns",
      "typescript",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "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\"."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/jeremylongshore/tons-of-skills-marketplace",
      "path": "plugins/saas-packs/palantir-pack/skills/palantir-sdk-patterns/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/jeremylongshore/tons-of-skills-marketplace/blob/HEAD/plugins/saas-packs/palantir-pack/skills/palantir-sdk-patterns/SKILL.md",
      "key": "jeremylongshore/tons-of-skills-marketplace/plugins/saas-packs/palantir-pack/skills/palantir-sdk-patterns/SKILL.md"
    },
    "compatibility": "Designed for Claude Code",
    "allowed_tools": [
      "Read,",
      "Write,",
      "Edit"
    ],
    "license": "MIT"
  },
  "instructions": "# Palantir SDK Patterns\n\n## Overview\n\nProduction-ready patterns for Foundry Platform SDK and OSDK usage. Covers client singletons, typed error handling, pagination helpers, retry logic, and multi-tenant client factories.\n\n## Prerequisites\n\n- Completed `palantir-install-auth` setup\n- Familiarity with async/await patterns\n- `foundry-platform-sdk` or `@osdk/client` installed\n\n## Instructions\n\n### Step 1: Singleton Client (Python)\n\n```python\n# src/foundry_client.py\nimport os\nimport foundry\nfrom functools import lru_cache\n\n@lru_cache(maxsize=1)\ndef get_client() -> foundry.FoundryClient:\n    \"\"\"Thre",
  "cost": {
    "context_tokens": 1323
  }
}

Fetch it by URL: GET /api/v1/registry/jeremylongshore-tons-of-skills-marketplace-palantir-sdk-66ccde/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.