recvd
The Recvd API for Python. Standard library only, Python 3.9 or later.
Recvd is a merchant of record: it is the seller on the receipt for your products, on its own checkout. This package reads your products, sales and subscriptions, reports usage, spends credits, checks webhooks, and meters your OpenAI and Anthropic calls. The full guide is at recvd.co/docs.
Recvd is in pilot and is operated by Levraq LLC. It is a software platform, not a bank, a lender or a collection agency. Payments are processed and held by a licensed payment partner.
pip install recvd
Use
Make a key on the Developers page of the Recvd app. Keep it on your server.
import os
from recvd import Recvd
recvd = Recvd(os.environ["RECVD_API_KEY"])
# Find the sale your user just made, by the reference you put on the checkout link.
sales = recvd.sales.list(reference="user_8231")["data"]
# Every sale, page after page.
for sale in recvd.sales.all():
print(sale["id"], sale["status"])
# Report usage. Give your own idempotency_key so the same usage reported twice counts once.
recvd.usage_events.record(subscription_id=subscription_id, meter="output_tokens", quantity=1200, idempotency_key=request_id)
Every request in the API has a method: recvd.products, sales, subscriptions, usage_charges, usage_events, tasks, credits, discount_codes, partners, license_keys, portal, disputes, buyers, stats, events and checkout_links. Changing things needs a key made to write. Answers are plain dicts; recvd.types has their shapes for a type checker.
A write that takes an idempotency key always carries one. Leave it out and the SDK makes one, and uses the same one when it tries the request again. A read, and a write with a key, is tried again after a network failure, a 429 or a 5xx. Any other write is sent once.
Errors
from recvd import RecvdError
try:
recvd.credits.use(reference="user_8231", amount=5)
except RecvdError as error:
if error.status == 409:
... # str(error) is what Recvd said: "Not enough credits: 2 left, 5 asked for"
error.status is the HTTP status, and 0 when Recvd could not be reached (RecvdConnectionError).
Webhooks
from recvd import verify_webhook
# body is the raw request body, exactly as received.
event = verify_webhook(body, request.headers.get("Recvd-Signature"), os.environ["RECVD_WEBHOOK_SECRET"])
if event["type"] == "sale.paid" and not event["test"]:
unlock(event["data"]["sale"]["reference"])
It raises RecvdSignatureError for a wrong signature or an event more than five minutes old.
Metering OpenAI and Anthropic calls
Wrap the call. The tokens the answer reports are sent to Recvd as usage events, keyed by the answer's own id, so the same answer counted twice counts once.
metered = recvd.meter(
subscription_id=subscription_id, # or reference= / buyer_email= for prepaid credits
meters={"input": "input_tokens", "output": "output_tokens"}, # your product's meters
per=1000, # tokens in one unit; a call is rounded up to whole units
on_error=lambda error, usage: log.warning("usage not counted: %s", error),
)
message = metered(lambda: anthropic.messages.create(model=model, max_tokens=1024, messages=messages))
# Streams too: counted once the stream has been read to its end.
stream = metered(lambda: openai.chat.completions.create(model=model, messages=messages, stream=True, stream_options={"include_usage": True}))
for chunk in stream:
...
# Async clients work the same way.
message = await metered(lambda: async_anthropic.messages.create(model=model, max_tokens=1024, messages=messages))
- Only the counts and the answer's id are sent. No prompt, no answer and no model name leave your server.
- Nothing here can fail your model call. If Recvd refuses the usage or cannot be reached,
on_erroris told and the answer comes back as it was. - It reads OpenAI chat completions and responses, and Anthropic messages, streamed or not, and the
with client.messages.stream(...)helper. With OpenAI chat completions a stream reports usage only when you passstream_options={"include_usage": True}; without iton_erroris told that nothing was counted. - OpenAI counts cached tokens inside its input figure and Anthropic counts them beside it. Here
inputis the uncached part for both, andcache_readandcache_writeare separate. Cached tokens go to theinputmeter unless you give them meters of their own. - Without
meters, one event is sent for all the tokens together: with prepaid credits andper=1, one credit is one token. To count another way, giveunits=lambda usage: .... - The report is sent on a thread, so the answer does not wait for it. Call
metered.flush()before your process ends, or passbackground=Falseto send it before the answer is handed back.
Metadata
Release files for recvd 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| recvd-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for recvd-0.1.0-py3-none-any.whl |
Release files / recvd-0.1.0-py3-none-any.whl
| Download URL | recvd-0.1.0-py3-none-any.whl |
|---|---|
| Size | 20.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cfef15332fde2dbbd3ee9461189cbbc8bc5da9e0e61f4bee570bac7040fde292
|
|
BLAKE2b-256 checksum How to use checksums |
deeb0b7e3e3135525e9a9cdae5c76c0bd195e603519a7f8c179ffef726c3e19c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for recvd 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.