Skip to main content

Installation

# Core client
pip install cred-protocol

# With FastAPI integration
pip install cred-protocol[fastapi]

Quick Start

import asyncio
from cred_protocol import CredClient

async def main():
    async with CredClient(api_key="cred_sk_...") as cred:
        result = await cred.evaluate(
            wallet_address="0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
            policy="standard",
        )

        print(result.trust_score)   # 75
        print(result.trust_tier)    # TrustTier.VERIFIED
        print(result.all_passed)    # True

asyncio.run(main())

Evaluate Options

Using a Policy Template

result = await cred.evaluate(
    wallet_address="0x...",
    policy="standard",
)

Using Custom Gates

result = await cred.evaluate(
    wallet_address="0x...",
    gates=["human", "verified", "established"],
    operator="WEIGHTED",
    weights={"human": 0.4, "verified": 0.35, "established": 0.25},
    composite_threshold=60,
)

With Dynamic Pricing

result = await cred.evaluate(
    wallet_address="0x...",
    policy="reputation",
    include_pricing=True,
    base_price_usdc=0.01,
)

print(result.price_multiplier)      # 0.25
print(result.suggested_price_usdc)  # 0.0025

TrustResult Fields

FieldTypeDescription
wallet_addressstrThe evaluated wallet
trust_scoreint0–100 composite score
trust_tierTrustTierTRUSTED, VERIFIED, LIMITED, UNTRUSTED, BLOCKED
confidencefloat0.0–1.0 data confidence
all_passedboolWhether all gates passed
gate_resultslist[GateScoreBreakdown]Per-gate breakdown
price_multiplierfloat | NoneDynamic pricing multiplier
suggested_price_usdcfloat | NoneComputed price
reputationWalletReputation | NoneBehavioral history
cachedboolWhether this was a cache hit
request_idstrUnique request ID
challengedict | None402 challenge body (if gates failed)

Error Handling

from cred_protocol import CredClient, CredAPIError

try:
    result = await cred.evaluate(wallet_address="0x...", policy="standard")
except CredAPIError as e:
    print(e.status)  # 401, 429, etc.
    print(e.body)    # Error details

FastAPI Integration

The require_trust() dependency gates endpoints with a single line:
import os
from fastapi import Depends, FastAPI
from cred_protocol.fastapi import require_trust
from cred_protocol import TrustResult

app = FastAPI()

@app.get("/resource")
async def get_resource(
    trust: TrustResult = Depends(require_trust(
        api_key=os.environ["CRED_API_KEY"],
        policy="standard",
    )),
):
    return {
        "data": "...",
        "wallet": trust.wallet_address,
        "score": trust.trust_score,
        "tier": trust.trust_tier.value,
    }

FastAPI Configuration

require_trust(
    api_key="cred_sk_...",           # Or set CRED_API_KEY env var
    base_url="https://...",          # Defaults to production
    policy="standard",               # Named template
    # gates=["human", "verified"],   # Or custom gates
    # operator="AND",
    include_pricing=False,
    base_price_usdc=None,
    on_fail="deny",                  # "deny" (403), "challenge" (402), "pass"
)

Wallet Extraction

The FastAPI dependency checks (in order):
  1. X-Wallet-Address request header
  2. ?wallet= query parameter
Override with a custom extractor:
from fastapi import Request
from cred_protocol.fastapi import CredTrustDependency

trust_dep = CredTrustDependency(
    api_key=os.environ["CRED_API_KEY"],
    policy="standard",
    extract_wallet=lambda req: req.headers.get("x-agent-wallet"),
)

@app.get("/resource")
async def get_resource(trust: TrustResult = Depends(trust_dep)):
    ...

Failure Responses

ResponseWhen
400No wallet address found on the request
402Gates failed, on_fail="challenge"
403Gates failed with on_fail="deny", or wallet is blocked
429Cred API rate limit exceeded
502Upstream gate provider error

Using Without FastAPI

The core CredClient works with any async Python code:
from cred_protocol import CredClient

# As async context manager (recommended)
async with CredClient(api_key="cred_sk_...") as cred:
    result = await cred.evaluate(wallet_address="0x...", policy="standard")

# Or manage lifecycle manually
cred = CredClient(api_key="cred_sk_...")
result = await cred.evaluate(wallet_address="0x...", policy="standard")
await cred.close()