SegOps AIDocs

Python Admin SDK (`segops_admin`)

The `segops_admin` package is the server-side Python Admin SDK for SegOps. It gives you programmatic access to every platform surface — segments, products, schemas, activations, AI queries, landing pages, and analytics — the same way the dashboard does.

Server-side only. Requires a secret API key (sk_…). Public (pk_) and MCP (mk_) keys are rejected. Zero required runtime dependencies (stdlib only). Python 3.9+.

TypeScript users: the @segops/admin npm package exposes the same resource namespaces and method names. See the TypeScript Admin SDK docs.


Install#

bash
pip install segops_admin

Quick Start#

python
import os
from segops_admin import SegOpsAdmin

client = SegOpsAdmin(api_key=os.environ["SEGOPS_API_KEY"])

# List segments (first page)
page = client.segments.list()
print(page["count"], "segments")

# Trigger a recompute
client.segments.recompute(12)

# Iterate all segments across pages
for segment in client.segments.iterate():
    print(segment["id"], segment["name"])

Resources#

NamespaceBase pathNotable actions
segments/segments/recompute, members, membership, export, preview, estimate, versions
product_segments/product-segments/recompute
products/pim/products/bulk_upsert, score, score_all, facets, schema, import_csv, import_status, confirm_import
schemas/schemas/infer, validate
activations/activations/sync
users/explorer/users/search, get, events, segments, explain, publish_sandbox
queries/ai-reach/queries/run, results, simulate, dashboard, win_rate
pages/ai-pages/pages/bulk_generate, regenerate, build, publish, export, query
analytics/analytics/overview, segments, overlap, activations, usage (read-only)

CRUD#

Every resource (except users and analytics) exposes the same CRUD surface:

python
client.segments.list({"page": 1, "page_size": 20})  # paginated dict
client.segments.iterate()                             # generator over all items
client.segments.get(id)
client.segments.create({"name": "VIP", "definition": {...}})
client.segments.update(id, {"name": "VIP v2"})       # PATCH by default
client.segments.delete(id)                            # returns None on 204

Pagination#

python
# Single page — returns a dict with count / next / previous / results
page = client.segments.list({"page": 2, "page_size": 50})
print(page["count"])        # total items
print(page["results"])      # list[dict] for this page

# Iterator — transparently fetches all pages
for segment in client.segments.iterate():
    process(segment)

# With initial params
for seg in client.segments.iterate({"page_size": 100}):
    process(seg)

Errors#

python
from segops_admin import SegOpsApiError

try:
    client.segments.get(9999)
except SegOpsApiError as exc:
    print(exc.status)   # HTTP status code, e.g. 404
    print(exc.body)     # parsed JSON body or raw string

SegOpsConfigError is raised at construction time for bad API keys.


Idempotency#

All mutating operations (create, recompute, bulk_upsert, export, etc.) automatically send a unique Idempotency-Key header. This is forward-compatible with upcoming server-side idempotency storage (24h replay window) — the header is sent today even before the server honours it.


Retries#

The client retries on 429 and 5xx responses with exponential backoff:

  • Default: 3 retries, cap 30s
  • Honors Retry-After when injecting a custom transport
python
client = SegOpsAdmin(api_key="sk_...", max_retries=5)
client_no_retry = SegOpsAdmin(api_key="sk_...", max_retries=0)

Webhook Verification#

SegOps signs outbound webhook payloads with HMAC-SHA256 and sends:

X-SegOps-Signature: sha256=<hex>

Verify the signature before processing:

python
from segops_admin.webhook import verify_webhook

# Flask example
raw_body = request.get_data()
sig = request.headers.get("X-SegOps-Signature", "")

if not verify_webhook(raw_body, sig, os.environ["SEGOPS_WEBHOOK_SECRET"]):
    abort(401)

event = request.get_json()
python
# Django example
raw_body = request.body
sig = request.headers.get("X-SegOps-Signature", "")

if not verify_webhook(raw_body, sig, settings.SEGOPS_WEBHOOK_SECRET):
    return HttpResponse(status=401)

CLI#

The package installs a segops-admin CLI entry point:

bash
# Install
pip install segops_admin

# Required
export SEGOPS_API_KEY=sk_...

# Optional — override the API base URL
export SEGOPS_API_URL=https://api.segops.ai/api

# Commands
segops-admin segments list
segops-admin segments list --page=2 --page_size=50
segops-admin segments get 12
segops-admin segments create segment.json
segops-admin segments recompute 12
segops-admin segments members 12
segops-admin segments membership user-123
segops-admin segments export 12 --format=parquet
segops-admin segments delete 12

segops-admin products list
segops-admin products import products.csv
segops-admin products bulk-upsert rows.json
segops-admin products score 9
segops-admin products score-all
segops-admin products facets

segops-admin schemas list
segops-admin schemas validate page_viewed payload.json

segops-admin activations list
segops-admin activations sync 4

segops-admin queries list
segops-admin queries run 3
segops-admin queries simulate query.json
segops-admin queries dashboard

segops-admin pages list
segops-admin pages get 8
segops-admin pages export 8 --format=html
segops-admin pages publish 8
segops-admin pages regenerate 8

segops-admin users search --q=alice
segops-admin users get user-123
segops-admin users events user-123

segops-admin analytics overview
segops-admin analytics usage

segops-admin --help

All output is JSON. Exit codes: 0 success, 1 API error, 2 usage error.


Parity with @segops/admin#

FeatureTypeScriptPython
Key validation (sk_ only)
All 9 resource namespaces
Identical method names✅ (snake_case per Python convention)
list() + iterate()
Idempotency-Key on mutations
429/5xx retry + backoff
verifyWebhook / verify_webhook
CLI entry point✅ (npx @segops/admin)✅ (segops-admin)
CSV import (multipart)
Zero runtime dependencies✅ (stdlib only)

Method names follow Python convention: bulkUpsertbulk_upsert, scoreAllscore_all, winRatewin_rate, importCsvimport_csv. Resource namespace productSegments becomes product_segments.