Skip to main content

Official Python SDK for xR2 product API

Project description

xR2 SDK (Python)

Official Python client for xR2 APIs.

Installation

pip install xr2-sdk

Quickstart (Sync)

from xr2_sdk.client import xR2Client

client = xR2Client(api_key="YOUR_PRODUCT_API_KEY")

# Check API key validity
key_response = client.check_api_key()
if key_response.ok:
    print(f"API key valid for user: {key_response.data.user}")

# Get prompt
prompt_response = client.get_prompt(slug="welcome")

if prompt_response.ok:
    prompt = prompt_response.data
    print(f"Prompt: {prompt.user_prompt}")

    # Track an event
    event_response = client.track_event(
        trace_id=prompt.trace_id,
        event_name="signup_success",
        source_name="web_app",
        user_id="user_123",
        metadata={"plan": "premium", "referral_code": "ABC123"},
    )

    if event_response.ok:
        print(f"Event tracked: {event_response.data.event_id}")

    # Track a purchase event with value
    purchase_response = client.track_event(
        trace_id=prompt.trace_id,
        event_name="purchase_completed",
        source_name="web_app",
        user_id="user_123",
        value=99.99,
        currency="USD",
        metadata={"order_id": "order_67890", "product_id": "prod_456"},
    )

Quickstart (Async)

import asyncio
from xr2_sdk.client import AsyncxR2Client

async def main():
    client = AsyncxR2Client(api_key="YOUR_KEY")
    try:
        # Check API key validity
        key_response = await client.check_api_key()
        if key_response.ok:
            print(f"API key valid for user: {key_response.data.user}")

        # Get prompt
        prompt_response = await client.get_prompt(slug="welcome")

        if prompt_response.ok:
            prompt = prompt_response.data

            # Track event
            event_response = await client.track_event(
                trace_id=prompt.trace_id,
                event_name="cta_clicked",
                source_name="mobile_app",
                user_id="user_001",
                session_id="session_xyz",
                metadata={"button_text": "Get Started", "page": "homepage"},
            )

            if event_response.ok:
                print(f"Event tracked: {event_response.data.event_id}")
    finally:
        await client.aclose()

asyncio.run(main())

Endpoints

  • GET /api/v1/check-api-key → validates API key and returns username
  • POST /api/v1/get-prompt → returns prompt content and trace_id
  • POST /api/v1/events → records an event associated with trace_id

Configuration

  • api_key: Product API key (sent as Authorization: Bearer <key>)
  • timeout: Request timeout (seconds)
  • total_retries, backoff_factor: Retry policy (sync) / lightweight retry (async)

API Methods

check_api_key()

Validate your API key and get the associated username.

Parameters: None

Returns: Response[CheckAPIKeyResponse]

  • response.data.ok: Always True if valid
  • response.data.user: Username of the API key owner

get_prompt()

Parameters:

  • slug (required): The prompt slug identifier
  • version_number (optional): Specific version number to fetch
  • status (optional): Version status filter - draft, testing, production, inactive, deprecated

Returns: Response[PromptContentResponse]

  • Access data: response.data.trace_id, response.data.user_prompt, etc.
  • Check success: if response.ok:

track_event()

Required Parameters:

  • trace_id: Trace ID from get_prompt() response
  • event_name: Event name as defined in dashboard (e.g., "signup_success", "purchase_completed")
  • source_name: Source identifier (e.g., "web_app", "mobile_app", "john_doe")

Optional Parameters:

  • user_id: User identifier for tracking
  • session_id: Session identifier for analytics
  • value: Numeric value (for revenue tracking, order amounts, etc.)
  • currency: Currency code (e.g., "USD", "EUR")
  • metadata: Dictionary of custom fields as defined in event definition schema

Returns: Response[EventResponse]

  • Access data: response.data.event_id, response.data.timestamp, etc.
  • Check success: if response.ok:

Important Notes:

  • Before tracking events, define them at https://xr2.uk/analytics/events
  • Set event name and required/optional fields in the dashboard
  • Field validation happens automatically based on your event definitions
  • Events are deduplicated by trace_id + event_name

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xr2_sdk-0.2.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xr2_sdk-0.2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file xr2_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: xr2_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for xr2_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 03033e20f86cf9d2eeaf6339a50cd0cd5c721e8679abfa324930f3abe2974b8f
MD5 c8ac1c2eb36760a0bb0359756b0eca77
BLAKE2b-256 de3768b54bab053c9398e5900c10fc90cea7042bf23caad1868e94c8496d1c93

See more details on using hashes here.

File details

Details for the file xr2_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xr2_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for xr2_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f36ebc9bc01bee13dff304a017be97b488042cd5dbcc65d0ae08a7d4197625e
MD5 f72d72a13b3b5add4ef39e7961268b9a
BLAKE2b-256 0170cfb863135d4aa7064ed5932d44fff46d27623b7eff57f36301c6c710cf3b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page