Using a Custom Client

By default, all functions create a NotifiedByClient that reads your API key from Django settings. You can pass a custom client if you need to use a different API key or base URL (useful for testing or multi-tenant setups).

Creating a Custom Client

from notifiedby import NotifiedByClient, subscribe_to_flow

client = NotifiedByClient(
    api_key='your-other-api-key',
    base_url='http://localhost:8000'
)

subscribe_to_flow(
    flow_trigger='WELCOME_SIGNUP',
    email='test@example.com',
    client=client
)

All flow, recipient, and flag functions accept an optional client parameter.

Use Cases

Testing

Use a custom client for testing with mock servers or different environments:

import pytest
from notifiedby import NotifiedByClient

@pytest.fixture
def test_client():
    return NotifiedByClient(
        api_key='test-key',
        base_url='http://localhost:8000'
    )

def test_user_signup_flow(test_client):
    from notifiedby import subscribe_to_flow

    subscribe_to_flow(
        flow_trigger='WELCOME_SIGNUP',
        email='test@example.com',
        client=test_client
    )
    # Assert the flow was triggered...

Multi-Tenant Applications

Different clients can use different API keys for different organizations:

def get_client_for_organization(org_id):
    """Get a client configured for a specific organization"""
    api_key = get_api_key_for_org(org_id)
    return NotifiedByClient(api_key=api_key)

def send_org_email(org_id, email_data):
    client = get_client_for_organization(org_id)
    from notifiedby import send_email_via_api

    return send_email_via_api(email_data, client=client)

Development vs Production

Use different endpoints for development and production:

import os
from notifiedby import NotifiedByClient

def get_notifiedby_client():
    if os.getenv('DJANGO_ENV') == 'development':
        return NotifiedByClient(
            api_key=os.getenv('NOTIFIEDBY_DEV_KEY'),
            base_url='https://dev-api.notifiedby.com'
        )
    else:
        # Production uses default settings
        return NotifiedByClient()

Client Configuration Options

The NotifiedByClient constructor accepts:

NotifiedByClient(
    api_key=None,      # API key (defaults to settings.NOTIFIEDBY_API_KEY)
    base_url=None,     # Base URL (defaults to https://api.notifiedby.com)
)

The client automatically:

  • Adds the API key to Authorization: Api-Key <key> header

  • Adds encryption key to Encryption-Key header if configured

  • Sets appropriate timeouts and error handling

  • Handles JSON serialization/deserialization