Skip to main content

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 + async — Shardd 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.xyz → Keys.

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

Metadata

Release files for shardd 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shardd 0.1.0
File Size Uploaded
shardd-0.1.0.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shardd 0.1.0
File Interpreter ABI Platform
shardd-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.4 kB

Release files / shardd-0.1.0.tar.gz

Download URL shardd-0.1.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e6c2687c903d62c4374d7d1a1eecd0b51fb2ed8137466e3694bfab1d61ddbc99
BLAKE2b-256 checksum
How to use checksums
6a1dc5de33626cd12b7e7f38d520ec322f1ab689d3e68cf517463895e0ee110e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / shardd-0.1.0-py3-none-any.whl

Download URL shardd-0.1.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c690f0a40283425c7348129195e087da8f0d93441cf030dbb27d223346a42a8d
BLAKE2b-256 checksum
How to use checksums
7ba5c2462544dc69b112b2007c374ac6979485619625840a4c26cb1546a5f515
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page