Skip to main content

Official Python SDK for the Codelet public API

Project description

Codelet Python SDK

Official Python client for the Codelet public API.

Install

pip install codelet

For local development from this monorepo:

python3 -m venv .venv
source .venv/bin/activate
pip install -e .

Usage

from codelet import Codelet, CodeletError
from codelet_generated.models.cancel_subscription_request import CancelSubscriptionRequest

codelet = Codelet(api_key="cl_test_…")  # or cl_live_…

# Ingest (auto idempotency_key when omitted)
codelet.ingest({
    "customer_id": "org_42",
    "metric": "api_call",
    "quantity": "1",
})

me = codelet.account.get_me()
customers = codelet.customers.list_customers()
codelet.subscriptions.cancel_subscription(
    "subs_…",
    CancelSubscriptionRequest(mode="at_period_end"),
)

Optional client options: base_url, retries, headers.

Examples

Create metrics before plans, credit packs, or ingest. Unknown metric names return 400.

Create a metric

from codelet_generated.models.create_metric_request import CreateMetricRequest

metric = codelet.metrics.create_metric(
    CreateMetricRequest(
        name="api_request",
        display_name="API Request",
        aggregation="sum",
    )
)

Create a credit pack

from codelet_generated.models.create_credit_pack_request import CreateCreditPackRequest

pack = codelet.credit_packs.create_credit_pack(
    CreateCreditPackRequest(
        metric="api_request",
        name="500 API requests",
        price_amount="25.00",
        currency="USD",
        quantity_granted="500",
    )
)

Create and publish a plan

from codelet_generated.models.create_plan_request import CreatePlanRequest

plan = codelet.plans.create_plan(
    CreatePlanRequest(
        name="Pro",
        description="Usage-based Pro plan",
        version={
            "currency": "USD",
            "billing_period": "monthly",
            "billing_mode": "postpaid",
            "fixed_fees": [
                {
                    "fee_type": "period_charge",
                    "name": "Platform fee",
                    "amount": "29.00",
                },
            ],
            "usage_fees": [
                {
                    "pricing_model": "unit",
                    "billing_timing": "in_arrears",
                    "included_quantity": "1000",
                    "unit_price": "0.002",
                    "metric": "api_request",
                },
            ],
        },
    )
)

# Drafts are not checkout-ready until published
codelet.plans.publish_plan(plan.id)

Create checkout

Credit pack (one-time purchase):

from codelet_generated.models.create_checkout_request import CreateCheckoutRequest

pack_checkout = codelet.checkout.create_checkout(
    CreateCheckoutRequest(
        customer_id="org_42",
        pack_id="pack_…",
        success_url="https://yourapp.com/billing/success",
        cancel_url="https://yourapp.com/billing/cancel",
        idempotency_key="purchase:order-123",
    )
)
# Redirect the browser to pack_checkout.checkout_url

Subscription (plan):

plan_checkout = codelet.checkout.create_checkout(
    CreateCheckoutRequest(
        customer_id="org_42",
        plan_id="plan_…",
        success_url="https://yourapp.com/welcome",
    )
)
# Redirect the browser to plan_checkout.checkout_url

Pass either customer_id or customer_email, not both. Exactly one of pack_id or plan_id is required.

API reference

All generated methods are available on the Codelet instance (for example codelet.customers.list_customers). Prefer codelet.ingest(…) over codelet.usage.ingest_event(…) when recording usage; the helper fills idempotency_key when omitted.

codelet.ingest(body)

Ingest a usage event. Same as usage.ingest_event, but generates idempotency_key when omitted. Accepts a dict or IngestEventRequest.

Method Description
ingest(body) POST /v1/ingest

Account (codelet.account)

Method Description
get_me() API key context (project + environment)

Checkout (codelet.checkout)

Method Description
create_checkout(create_checkout_request) Create a credit pack or subscription checkout

Credit packs (codelet.credit_packs)

Method Description
list_credit_packs(metric=None, active="true", page_size=None, cursor=None) List packs (default: active only)
create_credit_pack(create_credit_pack_request) Create a pack
get_credit_pack(pack_id) Get a pack by ID
update_credit_pack(pack_id, update_credit_pack_request) Update name, description, and/or active

Customers (codelet.customers)

Method Description
list_customers(page_size=None, cursor=None) List customers
create_customer(create_customer_request) Create a customer
get_customer(customer_id) Get a customer
update_customer(customer_id, update_customer_request) Update editable fields
get_customer_balance(customer_id, metric=None) Credit balances (all metrics, or one)

Metrics (codelet.metrics)

Method Description
list_metrics(page_size=None, cursor=None) List metrics
create_metric(create_metric_request) Create a metric
get_metric(name) Get a metric by name
update_metric(name, update_metric_request) Partial update (today: display_name)

Plans (codelet.plans)

Method Description
list_plans(status="published", page_size=None, cursor=None) List plans (default: published)
create_plan(create_plan_request) Create a draft plan
get_plan(plan_id, include_pricing_details=False) Get a plan
publish_plan(plan_id) Publish the draft version

Subscriptions (codelet.subscriptions)

Method Description
list_subscriptions(customer=None, status=None, page_size=None, cursor=None) List subscriptions
get_subscription(subscription_id) Get a subscription
cancel_subscription(subscription_id, cancel_subscription_request) Cancel (at_period_end or immediate)
resume_subscription(subscription_id) Clear a scheduled cancellation

Usage (codelet.usage)

Method Description
ingest_event(ingest_event_request) Ingest a usage event (no auto idempotency key)

Request and response shapes match the OpenAPI spec. See also codelet.co/SKILL.md for merchant integration guidance.

Errors

from codelet import CodeletError, CodeletRateLimitError

try:
    codelet.ingest({...})
except CodeletRateLimitError:
    # 429
    pass
except CodeletError:
    # other API errors (status + body)
    pass

Generated APIs raise ApiException from the OpenAPI client. The top-level ingest helper maps those to CodeletError / CodeletRateLimitError.

Development

# from repo root
make sdk-generate
cd sdks/python && python3 -m venv .venv && source .venv/bin/activate && pip install -e .

Hand-written surface (src/codelet/) stays stable. Generated code under src/codelet_generated/ is overwritten by make sdk-generate.

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

codelet-0.1.0.tar.gz (84.0 kB view details)

Uploaded Source

Built Distribution

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

codelet-0.1.0-py3-none-any.whl (266.5 kB view details)

Uploaded Python 3

File details

Details for the file codelet-0.1.0.tar.gz.

File metadata

  • Download URL: codelet-0.1.0.tar.gz
  • Upload date:
  • Size: 84.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for codelet-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6e4f48cb2b54283a14bf22ae7ede52495061c9ba44a3d0325cd0735f5a55d685
MD5 27758807d860459857ff5ea105eb0d6b
BLAKE2b-256 e85065bcd5d30a347e42f19afb210e514ade0bea46ec60520886765aded6fa2e

See more details on using hashes here.

File details

Details for the file codelet-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codelet-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 266.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for codelet-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bb241e464852b2803db242844a36ca550e198f56e281c57707e059fc68b3363
MD5 84befc0ccb256d97b4d8f7e01a982c87
BLAKE2b-256 6aeb0de1102220682911fedeaae1dbef1e5aecf0548439274d1a8a7b171bbcc7

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