Skip to main content

Overview

The billing client provides access to subscription management, Stripe Checkout session creation, and the Stripe Billing Portal. Access the billing client via client.billing.

Get Subscription

Retrieve the current subscription status for the authenticated developer:
from grantex import Grantex

with Grantex(api_key="gx_live_...") as client:
    sub = client.billing.get_subscription()

    print(f"Plan: {sub.plan}")
    print(f"Status: {sub.status}")
    print(f"Current period ends: {sub.current_period_end}")

SubscriptionStatus

FieldTypeDescription
planstrCurrent plan ("free", "pro", or "enterprise").
statusstrSubscription status ("active", "past_due", "canceled").
current_period_endstr | NoneISO 8601 timestamp when the current billing period ends.

Create Checkout

Create a Stripe Checkout session for upgrading to a paid plan. Returns a URL to redirect the user to:
from grantex import Grantex, CreateCheckoutParams

with Grantex(api_key="gx_live_...") as client:
    checkout = client.billing.create_checkout(CreateCheckoutParams(
        plan="pro",
        success_url="https://myapp.com/billing/success",
        cancel_url="https://myapp.com/billing/cancel",
    ))

    print(f"Redirect to: {checkout.checkout_url}")

CreateCheckoutParams

ParameterTypeRequiredDescription
planstrYesThe plan to subscribe to ("pro" or "enterprise").
success_urlstrYesURL to redirect to after successful payment.
cancel_urlstrYesURL to redirect to if the user cancels.

CheckoutResponse

FieldTypeDescription
checkout_urlstrThe Stripe Checkout session URL.

Create Portal

Create a Stripe Billing Portal session for managing an existing subscription. Returns a URL to redirect the user to:
from grantex import Grantex, CreatePortalParams

with Grantex(api_key="gx_live_...") as client:
    portal = client.billing.create_portal(CreatePortalParams(
        return_url="https://myapp.com/settings",
    ))

    print(f"Redirect to: {portal.portal_url}")

CreatePortalParams

ParameterTypeRequiredDescription
return_urlstrYesURL to redirect to when the user exits the portal.

PortalResponse

FieldTypeDescription
portal_urlstrThe Stripe Billing Portal session URL.

Example: Billing Flow

from grantex import Grantex, CreateCheckoutParams, CreatePortalParams

with Grantex(api_key="gx_live_...") as client:
    # Check current plan
    sub = client.billing.get_subscription()

    if sub.plan == "free":
        # Upgrade to pro
        checkout = client.billing.create_checkout(CreateCheckoutParams(
            plan="pro",
            success_url="https://myapp.com/billing/success",
            cancel_url="https://myapp.com/billing/cancel",
        ))
        print(f"Upgrade: {checkout.checkout_url}")
    else:
        # Manage existing subscription
        portal = client.billing.create_portal(CreatePortalParams(
            return_url="https://myapp.com/settings",
        ))
        print(f"Manage: {portal.portal_url}")