Leverprice Python SDK
Server-side SDK for Leverprice price experiments. No required dependencies, Python 3.9+, works with Django, Flask, FastAPI, Starlette, aiohttp and plain WSGI/ASGI.
Your payments stay yours. The SDK gives your server a number. Your own code creates the checkout with your own provider keys. Leverprice never sees card data or provider API keys.
Installation
pip install leverprice
Signature checks use cryptography when it is installed and a bundled pure-Python verifier otherwise. To install it alongside: pip install "leverprice[crypto]".
Quick start
Create one client per process (for example in settings.py or app.py) and share it:
import os
from leverprice import Leverprice
leverprice = Leverprice(
secret_key=os.environ.get("LEVERPRICE_SECRET_KEY"), # server-only
products={
# Local guardrails: prices outside these bounds are never returned.
"pro": {"currency": "usd", "fallback_price": 1900, "min_price": 1500, "max_price": 3900},
},
)
leverprice.preload() # optional: fetch the config at startup instead of on the first request
1. Show the price
get_price reads the visitor's cookie and user agent from the request. Append quote.set_cookie to your response; apply_cookies does it for the common frameworks.
Django
from django.shortcuts import render
from leverprice import apply_cookies
def pricing(request):
user_id = request.user.pk if request.user.is_authenticated else None
quote = leverprice.get_price("pro", request, user_id=user_id)
return apply_cookies(render(request, "pricing.html", {"quote": quote}), quote)
Flask
from flask import make_response, render_template, request
@app.get("/pricing")
def pricing():
quote = leverprice.get_price("pro", request)
response = make_response(render_template("pricing.html", quote=quote))
for cookie in quote.set_cookie:
response.headers.add("Set-Cookie", cookie)
return response
FastAPI
from fastapi import FastAPI, Request, Response
@app.get("/pricing")
async def pricing(request: Request, response: Response):
quote = await leverprice.get_price_async("pro", request)
for cookie in quote.set_cookie:
response.headers.append("set-cookie", cookie)
return {"price": quote.price, "currency": quote.currency}
2. Create the checkout
Price the checkout on the server again. Never trust a price sent by the browser.
import stripe
quote = leverprice.get_price("pro", request)
session = stripe.checkout.Session.create(
mode="payment",
line_items=[
{
"quantity": 1,
"price_data": {"currency": quote.currency, "unit_amount": quote.price, "product_data": {"name": "Pro"}},
}
],
metadata=quote.metadata, # signed attribution: pass it whole
payment_intent_data={"metadata": quote.metadata},
success_url=success_url,
cancel_url=cancel_url,
)
3. Report the purchase
In your webhook handler (or connect your provider's webhooks to Leverprice directly):
event = stripe.Webhook.construct_event(payload, signature_header, endpoint_secret)
if event["type"] == "checkout.session.completed":
session = event["data"]["object"]
leverprice.track_purchase(
provider="stripe",
order_id=session["payment_intent"],
amount=session["amount_total"] - (session["total_details"] or {}).get("amount_tax", 0),
currency=session["currency"],
metadata=session["metadata"],
timestamp=event["created"], # Unix seconds are detected
)
Refunds: track_refund(provider=..., order_id=..., currency=..., refund_id=..., amount=...), or refunded_total=... for the order's cumulative refunded amount.
Guarantees
- Fails safe. If Leverprice is off, slow (no config within 500 ms), down, misconfigured or returns anything unexpected,
get_pricereturns yourfallback_price. It never raises for those reasons. - Verified config. Root key → keyset → config signature chain, version-monotonic (no rollback, no revoked keys), time-limited.
- Bounded. Every price is checked against your local
min_price,max_priceandcurrency. - Sticky and tamper-proof. Visitors keep their price. The assignment cookie is HMAC-signed with your project's assignment key, so rotating your API key does not reshuffle visitors.
- Private. Visitor and user ids are HMAC-hashed on your server; Leverprice only sees the hash.
- No price flips. A visitor who got the fallback during an outage keeps it for the rest of their session.
- Search engines see the standard price. Crawlers and link previews are never put into experiments.
- Never blocks your process. Background work runs on daemon threads; the async API never blocks the event loop.
Requests and cookies
Pass the framework's request object: Django HttpRequest, Flask/Werkzeug request, Starlette/FastAPI Request, aiohttp Request, a WSGI environ, an ASGI scope, any object with a headers mapping, or a plain dict of headers. Header names are case-insensitive. cookie= and user_agent= override what is read from the request.
quote.set_cookie is a list of Set-Cookie header values: usually empty, one value when the assignment cookie (__pp) changes, or the outage pin (__pp_f). Append every value. apply_cookies(response, quote) supports Django, Flask/Werkzeug, Starlette/FastAPI and aiohttp responses and WSGI header lists. Django has no API for raw Set-Cookie values, so there it loads them into response.cookies.
Pricing several products for one page? Pass the same request object to each get_price call: the quotes share one visitor, and the last quote's cookie carries every assignment. Plain header dicts are not shared; pass the request itself.
Exposures
An exposure is one person seeing an experiment price. With exposure="auto" (default) the SDK logs it the first time a visitor gets a price from an experiment, once per visitor and experiment. Prefetch and prerender requests (Sec-Purpose, Purpose, X-Purpose, X-Moz, Next-Router-Prefetch) are priced but not counted.
If you compute the price before it is visible, use manual mode:
quote = leverprice.get_price("pro", request, exposure="manual")
# later, when the price is on screen:
leverprice.log_exposure(quote)
Exposures are sent in the background within flush_interval_ms (2 s). Call flush() before a short-lived process exits; long-lived servers need nothing.
Bots
Crawlers, link previewers, headless browsers, uptime monitors and command-line clients get the product's standard price (or your fallback if no config is available) with reason "bot": no assignment, no cookie, no exposure. An empty user agent counts as a person. Pass bot=True or bot=False to use your own detection; the built-in check is leverprice.is_bot(user_agent).
Kill switch
Set LEVERPRICE_DISABLED=1 (or true) in the environment, or pass disabled=True. It is read on every call: get_price returns your fallback price with reason "disabled", sets no cookies, makes no network calls and logs nothing; tracking calls do nothing. A missing or malformed secret_key behaves the same way (a malformed key is reported once through on_error).
Outages
When no usable config is available (reason "no_config"), the quote also sets __pp_f=1 for 30 minutes. While a browser sends it, it gets the fallback price with reason "pinned_fallback" and is not enrolled; each pinned request extends it, so the price changes only after 30 minutes of inactivity. Purchases and refunds that cannot be delivered are kept and retried on the next flush.
Trust chain and updates
The SDK embeds only Leverprice's offline root public keys (ROOT_PUBLIC_KEYS). Each config carries a keyset of online signing keys signed by a root key, and must be signed by one of those keys. Keyset versions only move forward within a process, so a revoked key cannot come back; MIN_KEYSET_VERSION sets the floor for fresh processes. Config versions only move forward, and configs expire. For local development pass trusted_root_keys={"r1": "<dev root public key>"}.
The config is revalidated every 30 s with an ETag. When any Leverprice response announces a newer version in Leverprice-Config-Version, the SDK fetches it right away.
Threads, asyncio and forking servers
The client is thread-safe. Every method has an _async variant for asyncio apps (get_price_async, track_purchase_async, track_refund_async, flush_async, refresh_async, preload_async, close_async). The client works under pre-forking servers (gunicorn, uWSGI): a child process starts with fresh threads, locks and an empty event queue. It also works as a context manager (with / async with), which flushes on exit.
Reference
get_price(product_key, request=None, *, cookie=None, user_agent=None, user_id=None, exposure="auto", bot=None) -> Quote. Raises UnknownProductError only if product_key is missing from products.
| Quote field | |
|---|---|
product_key |
The key you asked for |
price |
Minor units; use it for display and as the checkout amount |
currency |
Lowercase ISO 4217, from your product guard |
is_fallback |
True when price is your fallback_price |
reason |
See below |
experiment_id, variant_id |
Set when reason is "experiment", else None |
visitor_id |
Hashed visitor id Leverprice sees; None unless reason is "experiment" |
metadata |
{pp_exp, pp_var, pp_vid, pp_sig} for the checkout (signed); {} otherwise |
set_cookie |
Set-Cookie values to append (zero or more) |
checkout_url |
Hosted checkout link for this price, or None |
exposure_logged |
An exposure for this visitor and experiment has been logged |
reason |
Price |
|---|---|
experiment |
The visitor's sticky variant |
base_price |
Standard price (no experiment running) |
bot |
Standard price, or fallback if unavailable; never enrolled |
disabled |
Fallback: kill switch, or no valid secret key |
no_config |
Fallback: no usable signed config (timeout, outage, rejected or expired); sets the pin |
pinned_fallback |
Fallback: this browser got a fallback during an outage in this session |
unknown_product |
Fallback: the config has no such product |
currency_mismatch |
Fallback: the config's currency differs from your guard |
out_of_bounds |
Fallback: a price is outside your (or Leverprice's) min/max |
| Method | |
|---|---|
log_exposure(quote) |
Logs a manual-mode exposure; returns True if one was queued |
track_purchase(*, provider, order_id, amount, currency, metadata=None, timestamp=None) |
Reads pp_exp, pp_var, pp_vid, pp_sig from metadata; sends right away |
track_refund(*, provider, order_id, currency, refund_id=None, amount=None, refunded_total=None, timestamp=None) |
Sends right away |
flush() |
Sends queued events and waits (at most 500 per request; purchases and refunds are kept on failure) |
refresh() / preload() |
Fetch the config now / only if none is usable |
close() |
Flushes and stops the flush timer |
Providers: stripe, polar, lemonsqueezy, dodo, other. Timestamps are milliseconds, Unix seconds or a datetime. None of these methods raise.
| Option | Default | |
|---|---|---|
secret_key |
required | pp_sk_live_… or pp_sk_test_… |
products |
required | Product key → {currency, fallback_price, min_price?, max_price?} or ProductGuard |
disabled |
False |
Kill switch |
api_url |
https://api.leverprice.com |
|
timeout_ms |
500 |
Longest wait for a config when none is usable |
refresh_interval_ms |
30000 |
Config revalidation period |
max_stale_ms |
86400000 |
Stop using a config this long after the server last confirmed it |
cookie_name |
"__pp" |
The outage pin is <cookie_name>_f |
cookie_max_age_seconds |
15552000 |
180 days |
cookie_secure |
True |
Set False only for plain-HTTP development |
cookie_domain |
None |
|
flush_interval_ms |
2000 |
Longest time exposures wait before being sent |
trusted_root_keys |
ROOT_PUBLIC_KEYS |
For staging and local development |
transport |
UrllibTransport() |
Any object with request(method, url, headers, body, timeout) |
on_error |
log a warning | Receives every SDK error; exceptions it raises are ignored |
Errors are logged on the leverprice logger when no on_error is given.
License
MIT
Metadata
Release files for leverprice 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| leverprice-0.3.0.tar.gz | 46.8 kB | With provenance | Details for leverprice-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| leverprice-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for leverprice-0.3.0-py3-none-any.whl |
Total release size: 81.6 kB
Release files / leverprice-0.3.0.tar.gz
| Download URL | leverprice-0.3.0.tar.gz |
|---|---|
| Size | 46.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
828745c8ac44959e2822bee1c02f70c9fbab6cd6fc2b9c0b513c1e5df5f55f50
|
|
BLAKE2b-256 checksum How to use checksums |
58386192139e7a776a970b16df5dbda58c23921698368a293b4edfaa386cb07a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.
Transparency logRelease files / leverprice-0.3.0-py3-none-any.whl
| Download URL | leverprice-0.3.0-py3-none-any.whl |
|---|---|
| Size | 34.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4e76c787b00cdebdfc6c72d0e973a456b96741b2a8385f207a8708630256a62d
|
|
BLAKE2b-256 checksum How to use checksums |
d6c3f8125636d38e371525e8892224717b29cf5a8546456febf2280a6cbb3ed7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.
Transparency logSecurity for leverprice 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for leverprice-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.