Skip to main content

Official Python SDK for the Bemby Notify API

Project description

Notification Platform SDK

Official Python SDK for the Notification Platform API.

License: MIT Python 3.8+

Features

  • Easy to use: Simple, intuitive API
  • Type hints: Full type annotation support
  • Comprehensive: Covers all API endpoints
  • Well documented: Extensive docstrings and examples
  • Error handling: Custom exceptions for different error types
  • Context manager: Automatic resource cleanup
  • Production ready: Battle-tested and reliable

Installation

pip install notification-platform-sdk

Quick Start

from notification_platform_sdk import NotificationPlatformClient

# Initialize the client
client = NotificationPlatformClient(
    base_url="https://notifications.example.com",
    api_key="np_your_api_key_here"
)

# Create a customer
customer = client.customers.create(
    email="john@example.com",
    first_name="John",
    last_name="Doe",
    phone="+1234567890"
)

# Send an email notification
notification = client.notifications.send(
    customer_id=customer["id"],
    channel="email",
    subject="Welcome to our platform!",
    body="Hi John, thanks for signing up!"
)

print(f"Notification sent with ID: {notification['id']}")

Authentication

You need an API key to use the SDK. To get an API key:

  1. Log in to your Notification Platform account
  2. Navigate to SettingsAPI Keys
  3. Click Create API Key
  4. Copy the key (it's only shown once!)
client = NotificationPlatformClient(
    base_url="https://your-instance.com",
    api_key="np_1234567890abcdef"
)

Usage Examples

Customers

Create a customer

customer = client.customers.create(
    email="alice@example.com",
    phone="+1234567890",
    first_name="Alice",
    last_name="Smith",
    preferences={
        "email": True,
        "sms": True,
        "push": False
    },
    metadata={
        "plan": "premium",
        "signup_date": "2024-01-15"
    }
)

List customers

# Get first 50 customers
customers = client.customers.list(limit=50)

# Pagination
customers_page_2 = client.customers.list(skip=50, limit=50)

Get a customer

customer = client.customers.get("customer-id-here")
print(customer["email"])

Update a customer

customer = client.customers.update(
    "customer-id-here",
    first_name="Alicia",
    preferences={"email": True, "sms": False}
)

Delete a customer

client.customers.delete("customer-id-here")

Get customer by email

customer = client.customers.get_by_email("alice@example.com")

Notifications

Send a simple notification

notification = client.notifications.send(
    customer_id="customer-id-here",
    channel="email",
    subject="Welcome!",
    body="Welcome to our platform!",
    priority="high"
)

Send SMS notification

notification = client.notifications.send(
    customer_id="customer-id-here",
    channel="sms",
    body="Your verification code is 123456"
)

Send with template

notification = client.notifications.send(
    customer_id="customer-id-here",
    channel="email",
    template_id="welcome_email",
    template_data={
        "name": "Alice",
        "company": "Acme Corp"
    }
)

Schedule a notification

notification = client.notifications.send(
    customer_id="customer-id-here",
    channel="email",
    subject="Reminder",
    body="Don't forget your appointment tomorrow!",
    scheduled_at="2024-12-25T10:00:00Z"
)

List notifications

# All notifications
notifications = client.notifications.list()

# Filter by customer
notifications = client.notifications.list(customer_id="customer-id-here")

# Filter by status
failed_notifications = client.notifications.list(status="failed")

# Filter by channel
email_notifications = client.notifications.list(channel="email")

Get notification

notification = client.notifications.get("notification-id-here")
print(notification["status"])

Update notification status

notification = client.notifications.update_status(
    "notification-id-here",
    status="delivered"
)

# Mark as failed with error
notification = client.notifications.update_status(
    "notification-id-here",
    status="failed",
    error_message="Recipient email bounced"
)

Retry a failed notification

notification = client.notifications.retry("notification-id-here")

Cancel a scheduled notification

notification = client.notifications.cancel("notification-id-here")

Get statistics

stats = client.notifications.get_stats(
    start_date="2024-01-01T00:00:00Z",
    end_date="2024-12-31T23:59:59Z"
)
print(stats)

Templates

Create a template

# Email template
template = client.templates.create(
    name="welcome_email",
    channel="email",
    subject="Welcome {{name}}!",
    body_template="Hi {{name}}, welcome to {{company}}!",
    variables=["name", "company"],
    metadata={"category": "onboarding"}
)

# SMS template
template = client.templates.create(
    name="otp_sms",
    channel="sms",
    body_template="Your OTP is {{code}}. Valid for {{minutes}} minutes.",
    variables=["code", "minutes"]
)

List templates

# All templates
templates = client.templates.list()

# Filter by channel
email_templates = client.templates.list(channel="email")

Get a template

template = client.templates.get("welcome_email")
print(template["body_template"])

Update a template

template = client.templates.update(
    "welcome_email",
    subject="Welcome {{name}} to {{company}}!",
    body_template="Hi {{name}}, thanks for joining {{company}}!"
)

Delete a template

client.templates.delete("old_template")

Render a template (preview)

rendered = client.templates.render(
    "welcome_email",
    data={"name": "Alice", "company": "Acme Corp"}
)
print(rendered["subject"])  # "Welcome Alice!"
print(rendered["body"])     # "Hi Alice, welcome to Acme Corp!"

Schedules

Create a recurring schedule

# Daily reminder at 9 AM
schedule = client.schedules.create(
    name="Daily reminder",
    customer_id="customer-id-here",
    channel="email",
    cron_expression="0 9 * * *",
    subject="Daily Reminder",
    body="Don't forget to check your tasks!",
    timezone="America/New_York"
)

# Weekly report on Mondays at 10 AM
schedule = client.schedules.create(
    name="Weekly report",
    customer_id="customer-id-here",
    channel="email",
    cron_expression="0 10 * * MON",
    template_id="weekly_report",
    template_data={"report_type": "sales"}
)

List schedules

# All schedules
schedules = client.schedules.list()

# Filter by customer
schedules = client.schedules.list(customer_id="customer-id-here")

# Only active schedules
active_schedules = client.schedules.list(enabled=True)

Get a schedule

schedule = client.schedules.get("schedule-id-here")
print(schedule["cron_expression"])

Update a schedule

# Change time
schedule = client.schedules.update(
    "schedule-id-here",
    cron_expression="0 10 * * *"  # Change to 10 AM
)

# Disable
schedule = client.schedules.update(
    "schedule-id-here",
    enabled=False
)

Delete a schedule

client.schedules.delete("schedule-id-here")

Enable/disable shortcuts

# Enable
schedule = client.schedules.enable("schedule-id-here")

# Disable
schedule = client.schedules.disable("schedule-id-here")

Get next run time

next_run = client.schedules.get_next_run("schedule-id-here")
print(next_run["next_run"])  # "2024-12-25T10:00:00Z"

Error Handling

The SDK provides custom exceptions for different error scenarios:

from notification_platform_sdk import (
    NotificationPlatformClient,
    AuthenticationError,
    NotFoundError,
    ValidationError,
    RateLimitError,
)

client = NotificationPlatformClient(base_url="...", api_key="...")

try:
    customer = client.customers.get("invalid-id")
except AuthenticationError as e:
    print(f"Authentication failed: {e.message}")
    print(f"Status code: {e.status_code}")
except NotFoundError as e:
    print(f"Customer not found: {e.message}")
except ValidationError as e:
    print(f"Validation error: {e.message}")
    print(f"Details: {e.response}")
except RateLimitError as e:
    print(f"Rate limit exceeded: {e.message}")
except Exception as e:
    print(f"Unexpected error: {e}")

Context Manager

Use the client as a context manager for automatic cleanup:

with NotificationPlatformClient(base_url="...", api_key="...") as client:
    customers = client.customers.list()
    for customer in customers:
        print(customer["email"])
# Connection automatically closed

Advanced Usage

Custom timeout

# Set custom timeout (default is 30 seconds)
client = NotificationPlatformClient(
    base_url="https://notifications.example.com",
    api_key="np_your_api_key",
    timeout=60  # 60 seconds
)

Pagination helper

def get_all_customers(client):
    """Get all customers with automatic pagination."""
    all_customers = []
    skip = 0
    limit = 100

    while True:
        batch = client.customers.list(skip=skip, limit=limit)
        if not batch:
            break
        all_customers.extend(batch)
        skip += limit

    return all_customers

customers = get_all_customers(client)

Bulk operations

# Create multiple customers
customers_data = [
    {"email": "user1@example.com", "first_name": "User1"},
    {"email": "user2@example.com", "first_name": "User2"},
    {"email": "user3@example.com", "first_name": "User3"},
]

created_customers = []
for data in customers_data:
    customer = client.customers.create(**data)
    created_customers.append(customer)

# Send bulk notifications
for customer in created_customers:
    client.notifications.send(
        customer_id=customer["id"],
        channel="email",
        subject="Welcome!",
        body="Thanks for joining!"
    )

Cron Expression Guide

For schedules, use standard cron expressions:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday=0)
│ │ │ │ │
│ │ │ │ │
* * * * *

Examples:

  • 0 9 * * * - Daily at 9:00 AM
  • 0 */6 * * * - Every 6 hours
  • 0 9 * * MON - Every Monday at 9:00 AM
  • 0 9 1 * * - First day of month at 9:00 AM
  • */15 * * * * - Every 15 minutes

Requirements

  • Python 3.8+
  • requests

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details

Support

Changelog

1.0.0 (2024-01-15)

  • Initial release
  • Full API coverage
  • Comprehensive documentation
  • Error handling
  • Context manager support

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

bemby_notify-1.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

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

bemby_notify-1.1.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file bemby_notify-1.1.0.tar.gz.

File metadata

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

File hashes

Hashes for bemby_notify-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5d5c836a11558ee5abfbca0c9cf1ffa8e95c5d9f232c65a1d947fb4176fbf4bd
MD5 c8a2a32dd0bfa059688718e1b2dd573c
BLAKE2b-256 2bb38b3a531b44a9e60688095d7d0edb7956ce82ad02b6634fd15370cf798dc5

See more details on using hashes here.

File details

Details for the file bemby_notify-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: bemby_notify-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for bemby_notify-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 970ddf3bd577a6db7245f8930d4c3b3185a661323bd6fb3024f221a499414a65
MD5 d6cfe677693e5609f08bee98f787d986
BLAKE2b-256 c4b188e582d14f61c1afa71a1319b97ad810cf60e686ad19937d51fc8976bcd8

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