Commet
  • Pricing
Log InTry out
Introduction

Quickstart

Integrate with PythonIntegrate with FlaskIntegrate with FastAPIIntegrate with Django

Learn

Resources

SDK ReferenceError HandlingTestingCLI

Plugins

Better Auth
DocumentationKnowledge BaseBuild with AIAPI ReferenceWebhooks

Integrate with Flask

Add billing and payments to your Flask application.

Install

pip install commet-sdk flask
uv add commet-sdk flask
poetry add commet-sdk flask

Configure

.env
COMMET_API_KEY=ck_sandbox_xxx
commet_client.py
import os
from commet import Commet

commet = Commet(
    api_key=os.environ["COMMET_API_KEY"],
    environment="sandbox",
)

Subscribe

routes/billing.py
from flask import Blueprint, request, jsonify
from commet_client import commet

billing = Blueprint("billing", __name__)


@billing.route("/subscribe", methods=["POST"])
def subscribe():
    data = request.get_json()

    commet.customers.create(
        email=data["email"],
        external_id=data["external_id"],
    )

    subscription = commet.subscriptions.create(
        external_id=data["external_id"],
        plan_code="pro",
    )

    return jsonify({"checkout_url": subscription.data["checkout_url"]})

Check Access

routes/billing.py
@billing.route("/subscription/<external_id>")
def get_subscription(external_id):
    sub = commet.subscriptions.get(external_id)
    return jsonify({"status": sub.data["status"]})


@billing.route("/features/<feature>/<external_id>")
def check_feature(feature, external_id):
    result = commet.features.check(code=feature, external_id=external_id)
    return jsonify({"allowed": result.data["allowed"]})

Track Usage

routes/billing.py
@billing.route("/usage", methods=["POST"])
def track_usage():
    data = request.get_json()

    commet.usage.track(
        external_id=data["external_id"],
        feature="api_calls",
        value=1,
    )

    return jsonify({"tracked": True})

Usage is aggregated and billed at end of period.

Customer Portal

routes/billing.py
@billing.route("/portal")
def portal():
    result = commet.portal.get_url(external_id="user_123")
    return redirect(result.data["portal_url"])

Webhooks

routes/webhooks.py
import os
from flask import Blueprint, request
from commet import Webhooks

webhooks_bp = Blueprint("webhooks", __name__)
webhooks = Webhooks()


@webhooks_bp.route("/webhooks/commet", methods=["POST"])
def handle_webhook():
    payload = webhooks.verify_and_parse(
        raw_body=request.get_data(as_text=True),
        signature=request.headers.get("x-commet-signature"),
        secret=os.environ["COMMET_WEBHOOK_SECRET"],
    )

    if payload is None:
        return "Invalid signature", 401

    if payload["event"] == "subscription.activated":
        # handle activation
        pass

    return "", 200

Start Server

app.py
from flask import Flask
from routes.billing import billing
from routes.webhooks import webhooks_bp

app = Flask(__name__)
app.register_blueprint(billing, url_prefix="/billing")
app.register_blueprint(webhooks_bp)

if __name__ == "__main__":
    app.run(port=3000)

Related

  • Subscriptions
  • Track Usage
  • Customer Portal
  • SDK Reference

How is this guide?

Integrate with Python

Install and configure the Commet Python SDK.

Integrate with FastAPI

Add billing and payments to your FastAPI application.

On this page

Install
Configure
Subscribe
Check Access
Track Usage
Customer Portal
Webhooks
Start Server
Related