# payfence

Python SDK for API monetization via [PayFence](https://payfence.io).

## Install

```bash
pip install payfence
```

## ASGI Middleware (FastAPI / Starlette)

```python
from fastapi import FastAPI
from payfence import PayFenceMiddleware

app = FastAPI()
app.add_middleware(PayFenceMiddleware, api_key="your_site_api_key")
```

## Direct Authorize Call

```python
from payfence import authorize

decision = authorize(
    api_key="your_site_api_key",
    token="pf_live_xxx",
    method="GET",
    path="/api/search",
)

if decision["decision"] == "allow":
    # serve the request
    pass
```

## HMAC Verification (Proxy Mode)

```python
from payfence import verify_signature

valid = verify_signature(
    secret="your_origin_secret",
    method=request.method,
    path=request.url.path,
    timestamp=request.headers["x-payfence-timestamp"],
    request_id=request.headers["x-payfence-request-id"],
    body=await request.body(),
    signature=request.headers["x-payfence-signature"],
)
```

Zero external dependencies — uses only Python stdlib.
