Skip to main content

Official Python SDK for RevHold - AI business assistant for SaaS analytics

Project description

revhold-python

Official Python SDK for RevHold - AI business assistant for SaaS analytics.

Installation

pip install revhold-python
# or
poetry add revhold-python

Quick Start

from revhold import RevHold

revhold = RevHold(api_key='your_api_key_here')

# Track a usage event
revhold.track_event(
    user_id='user_123',
    event_name='document_created',
    event_value=1,
)

# Ask AI a question
insight = revhold.ask_ai(
    question='Which users are most engaged this week?'
)
print(insight['answer'])

API Reference

Constructor

revhold = RevHold(
    api_key='your_api_key',     # Required: Your RevHold API key
    base_url='...',             # Optional: Override API base URL
    timeout=30,                 # Optional: Request timeout in seconds
)

track_event()

Track a single usage event.

revhold.track_event(
    user_id='user_123',              # Required
    event_name='feature_used',       # Required
    event_value=1,                   # Optional, defaults to 1
    timestamp='2025-01-07T...'       # Optional, defaults to now
)

Returns: dict

{
    'success': True,
    'message': 'Usage event recorded',
    'eventId': 'evt_xyz789'
}

track_batch()

Track multiple events efficiently.

revhold.track_batch([
    {'user_id': 'user_1', 'event_name': 'feature_used'},
    {'user_id': 'user_2', 'event_name': 'document_created'},
    {'user_id': 'user_3', 'event_name': 'export_completed'},
])

Returns: dict

{
    'success': True,
    'message': 'Batch events tracked successfully',
    'count': 3,
    'errors': None  # or list of errors if some failed
}

ask_ai()

Ask the AI a question about your usage data.

result = revhold.ask_ai(
    question='Which users are most engaged this week?'
)

print(result['answer'])       # AI-generated insight
print(result['confidence'])   # 'high' | 'medium' | 'low'
print(result['dataPoints'])   # Number of events analyzed

Returns: dict

{
    'answer': 'Based on your usage data...',
    'confidence': 'high',
    'dataPoints': 127
}

get_usage()

Retrieve recent usage events.

usage = revhold.get_usage(
    limit=10,               # Optional: max 1000
    user_id='user_123'      # Optional: filter by user
)

print(usage['events'])
print(usage['total'])

Returns: dict

{
    'events': [
        {
            'eventId': 'evt_xyz789',
            'userId': 'user_abc123',
            'eventName': 'feature_used',
            'eventValue': 1,
            'timestamp': '2025-01-07T14:30:00Z'
        }
    ],
    'total': 1,
    'limit': 10
}

Error Handling

The SDK raises RevHoldError for all API errors:

from revhold import RevHold, RevHoldError

revhold = RevHold(api_key='your_key')

try:
    revhold.track_event(
        user_id='user_123',
        event_name='feature_used'
    )
except RevHoldError as e:
    print(f'Status: {e.status_code}')
    print(f'Code: {e.error_code}')
    print(f'Message: {e.message}')
    
    if e.status_code == 429:
        print('Rate limit - retry after 60s')
    elif e.status_code == 402:
        print('Plan limit reached - upgrade')

Error Properties

  • status_code: int - HTTP status code (401, 402, 429, 500, etc.)
  • error_code: str - Machine-readable error code
  • message: str - Human-readable error message
  • details: dict - Additional error context

Error Helper Methods

if e.is_rate_limit_error:
    # Handle rate limiting (429)
    pass

if e.is_payment_required_error:
    # Handle plan limits (402)
    pass

if e.is_authentication_error:
    # Handle invalid API key (401)
    pass

if e.is_network_error:
    # Handle network issues
    pass

Context Manager

Use the SDK as a context manager for automatic cleanup:

with RevHold(api_key='your_key') as revhold:
    revhold.track_event(
        user_id='user_123',
        event_name='feature_used'
    )
# Session automatically closed

Type Hints

The SDK includes full type hints for better IDE support:

from typing import Dict, Any
from revhold import RevHold

revhold = RevHold(api_key='your_key')

# Type hints work automatically
result: Dict[str, Any] = revhold.track_event(
    user_id='user_123',
    event_name='feature_used'
)

Examples

Track user activity

# When a user creates a document
revhold.track_event(
    user_id=request.user.id,
    event_name='document_created',
    event_value=1
)

# When a user exports data
revhold.track_event(
    user_id=request.user.id,
    event_name='data_exported',
    event_value=1
)

Batch tracking

# Track multiple events efficiently
events = [
    {
        'user_id': user.id,
        'event_name': 'daily_active',
        'event_value': 1
    }
    for user in active_users
]

revhold.track_batch(events)

AI insights

# Get churn insights
churn_analysis = revhold.ask_ai(
    question='Which users are at risk of churning?'
)

# Identify upsell opportunities
upsell_opportunities = revhold.ask_ai(
    question='Which trial users are most likely to upgrade?'
)

# Analyze feature adoption
feature_adoption = revhold.ask_ai(
    question='What features do power users use most?'
)

Environment Variables

import os
from revhold import RevHold

# Load API key from environment
revhold = RevHold(api_key=os.environ['REVHOLD_API_KEY'])

Rate Limits

  • Usage events: 1,000 requests/minute
  • AI questions: 10 requests/minute
  • Get usage: 100 requests/minute

Rate limit info is included in error responses:

try:
    revhold.ask_ai(question='...')
except RevHoldError as e:
    if e.status_code == 429:
        retry_after = e.details.get('retryAfter', 60)
        print(f'Retry after {retry_after} seconds')

Requirements

  • Python 3.8 or higher
  • requests library (automatically installed)

Development

# Clone the repository
git clone https://github.com/revhold/python-sdk.git
cd python-sdk

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black revhold/

# Type checking
mypy revhold/

Support

License

MIT

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

revhold_python-1.0.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

revhold_python-1.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file revhold_python-1.0.0.tar.gz.

File metadata

  • Download URL: revhold_python-1.0.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for revhold_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9295fb38317357fae8f09f1f55e15b119e28aa103c0e6fbd7ce639d8f1b079f4
MD5 c56336979d7c4e269ca677351e5cb106
BLAKE2b-256 9dd732f66ca9da5211f3e8291d9b7500c4ed7198f4444ee808a0901edd4f2593

See more details on using hashes here.

File details

Details for the file revhold_python-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for revhold_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c64e6254046490f0133c258c17a1ac7b98952a9b92959c8a853f4f00d957d336
MD5 2d037ca775fb1f7b7c39080778443e93
BLAKE2b-256 c0e4371fdaa934a166ab8d8e45bca53a93129e882ab01fe19d74274f48e32571

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