Skip to main content

Official Python client for shardd — a globally distributed credit ledger with automatic regional failover.

Project description

shardd

PyPI Python versions License: MIT

Official Python client for shardd — a globally distributed credit ledger with a sub-10ms write path in every region.

  • Zero config — pass an API key; the SDK picks the closest healthy edge.
  • Automatic failover — transient 5xx/timeouts fall over to the next region, reusing the idempotency nonce so retries collapse.
  • Sync + asyncShardd and AsyncShardd share the same method surface.
  • Fully typed — dataclasses, no runtime introspection magic.

Install

pip install shardd

30-second quickstart

import os
from shardd import Shardd

shardd = Shardd(os.environ["SHARDD_API_KEY"])

# Credit 500 units to user:alice in the `my-app` bucket.
result = shardd.create_event("my-app", "user:alice", 500)
print("new balance =", result.balance)

# Read back the whole bucket.
balances = shardd.get_balances("my-app")
for row in balances.accounts:
    print(f"{row.account} = {row.balance}")

Get an API key at https://app.shardd.xyzKeys.

Async

import asyncio
from shardd import AsyncShardd

async def main():
    async with AsyncShardd(api_key) as shardd:
        result = await shardd.create_event("my-app", "user:alice", -100)
        print(result.balance)

asyncio.run(main())

API

Method (sync & async) Purpose
Shardd(api_key, *, edges=None, timeout_s=30.0, http=None) Build a client.
create_event(bucket, account, amount, *, note=None, idempotency_nonce=None, max_overdraft=None, min_acks=None, ack_timeout_ms=None, hold_amount=None, hold_expires_at_unix_ms=None) Charge, credit, reserve, or release balance.
charge(bucket, account, amount, **kw) Debit sugar.
credit(bucket, account, amount, **kw) Credit sugar.
list_events(bucket) Event history for a bucket.
get_balances(bucket) All balances in a bucket.
get_account(bucket, account) One account's balance + holds.
edges() Current regional directory.
health(base_url=None) Pinned (or specified) edge's health snapshot.

Idempotency

Every create_event carries an idempotency_nonce. If you don't supply one, the SDK generates a UUID v4 for you. For safe retries, capture the nonce client-side and reuse it:

import uuid

nonce = str(uuid.uuid4())
result = shardd.create_event(
    "my-app", "user:alice", -100,
    note="order #9821",
    idempotency_nonce=nonce,
)
# A retry with the same `nonce` returns the original event and
# `result.deduplicated is True` — no double charge.

Failover behavior

The three prod regions (use1.api.shardd.xyz, euc1.api.shardd.xyz, ape1.api.shardd.xyz) are baked in as defaults. On the first request the client parallel-probes /gateway/health on all three, picks the lowest-latency healthy one, and pins it. If that edge returns 503/ 504/timeouts/connect-errors, the SDK marks it unavailable for 60s and retries the request once against the next-best candidate. Non-retryable errors (400, 401, 403, 404, 422) surface immediately — no retry, no failover.

Override the edges for local or self-hosted clusters:

shardd = Shardd(
    api_key,
    edges=[
        "http://localhost:8081",
        "http://localhost:8082",
        "http://localhost:8083",
    ],
)

Error handling

from shardd import Shardd, InsufficientFundsError, ShardError

try:
    shardd.create_event("my-app", "user:alice", -1000)
except InsufficientFundsError as err:
    print(f"short {1000 - err.available_balance} credits")
except ShardError as err:
    if err.retryable:
        # queue for retry
        ...
    else:
        raise

License

MIT © shardd

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

shardd-0.1.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

shardd-0.1.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shardd-0.1.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for shardd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e6c2687c903d62c4374d7d1a1eecd0b51fb2ed8137466e3694bfab1d61ddbc99
MD5 ebe1a417382b76b02e87538c1d4c121d
BLAKE2b-256 6a1dc5de33626cd12b7e7f38d520ec322f1ab689d3e68cf517463895e0ee110e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: shardd-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for shardd-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c690f0a40283425c7348129195e087da8f0d93441cf030dbb27d223346a42a8d
MD5 b7db0e7a71bb1d4e5b3fc24ddfb9650b
BLAKE2b-256 7ba5c2462544dc69b112b2007c374ac6979485619625840a4c26cb1546a5f515

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