SubscriptionKore

# Examples

This section contains example applications demonstrating SubscriptionKore usage.

## FastAPI Integration

See `examples/fastapi_app.py` for a complete FastAPI application.

## Multi-Provider Setup

See `examples/multi_provider_app.py` for an example with multiple payment providers.

## Running Examples

To run the examples:

    # Install with all dependencies
    pip install -e .[all]

    # Run FastAPI example
    make run-fastapi-example

    # Run multi-provider example
    make run-multi-provider-example

## Basic Example

    """Example FastAPI application using SubscriptionKore."""

    from fastapi import Depends, FastAPI, HTTPException
    from pydantic import BaseModel

    from subscriptionkore import (
        SubscriptionKoreConfig,
        SubscriptionActivated,
        SubscriptionCanceled,
        PaymentFailed,
    )
    from subscriptionkore.config import StripeConfig
    from subscriptionkore.core.exceptions import EntityNotFoundError
    from subscriptionkore.integrations. fastapi import (
        create_webhook_router,
        get_customer_manager,
        get_entitlement_service,
        get_subscription_manager,
        setup_subscriptionkore,
    )
    from subscriptionkore.services import CustomerManager, EntitlementService, SubscriptionManager
    from subscriptionkore. services.webhook_processor import WebhookResult

    # Configuration - load from environment in production
    config = SubscriptionKoreConfig(
        database_url="postgresql+asyncpg://user:password@localhost: 5432/subscriptionkore",
        redis_url="redis://localhost:6379/0",
        stripe=StripeConfig(
            api_key="sk_test_your_stripe_key",
            webhook_secret="whsec_your_webhook_secret",
        ),
    )

    # Create FastAPI app
    app = FastAPI(title="My SaaS App")

    # Setup SubscriptionKore
    subscriptionkore = setup_subscriptionkore(app, config)

    # Event handlers - register before app starts
    async def on_subscription_activated(event: SubscriptionActivated) -> None:
        """Handle subscription activation."""
        print(f"Subscription activated: {event.subscription.id}")
        # Send welcome email, provision resources, etc.

    async def on_subscription_canceled(event: SubscriptionCanceled) -> None:
        """Handle subscription cancellation."""

## Multi-Provider Example

    """Example showing multi-provider usage."""

    from fastapi import Depends, FastAPI, HTTPException
    from pydantic import BaseModel

    from subscriptionkore import SubscriptionKoreConfig, ProviderType
    from subscriptionkore. config import (
        StripeConfig,
        PaddleConfig,
        LemonSqueezyConfig,
        ChargebeeConfig,
    )
    from subscriptionkore.integrations.fastapi import (
        create_webhook_router,
        get_customer_manager,
        get_subscription_manager,
        setup_subscriptionkore,
    )
    from subscriptionkore. services import CustomerManager, SubscriptionManager

    # Multi-provider configuration
    config = SubscriptionKoreConfig(
        database_url="postgresql+asyncpg://user:password@localhost: 5432/subscriptionkore",
        redis_url="redis://localhost:6379/0",

        # Configure multiple providers
        stripe=StripeConfig(
            api_key="sk_test_stripe_key",
            webhook_secret="whsec_stripe_secret",
        ),
        paddle=PaddleConfig(
            api_key="paddle_api_key",
            webhook_secret="paddle_webhook_secret",
            environment="sandbox",
        ),
        lemonsqueezy=LemonSqueezyConfig(
            api_key="lemonsqueezy_api_key",
            webhook_secret="lemonsqueezy_webhook_secret",
            store_id="12345",
        ),
        chargebee=ChargebeeConfig(
            site="your-site",
            api_key="chargebee_api_key",
            webhook_username="webhook_user",
            webhook_password="webhook_pass",
        ),

        # Set default provider
        default_provider=ProviderType. STRIPE,