Metadata-Version: 2.5
Name: getaccord
Version: 0.6.4
Summary: Let AI agents spend within limits you set: holds, sign-off and exact payments, enforced on chain.
Project-URL: Homepage, https://useaccord.ai
Author: Accord
License-Expression: Apache-2.0
Keywords: ai agents,payments,sui,x402
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: cryptography>=42
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Description-Content-Type: text/markdown

# getaccord (Python)

Let AI agents spend within limits you set. An agent holds money with sellers, then pays everyone at once or no one; the limits are enforced on chain. The same API as [`@getaccord/sdk`](https://www.npmjs.com/package/@getaccord/sdk), for Python 3.9+.

Test mode: test dollars only.

## Quickstart

1. Make an agent key in your project folder. It's created on your machine and never leaves it:

   ```bash
   npx @getaccord/cli init
   ```

   This writes `ACCORD_AGENT_KEY` to `.env`, which the Python SDK reads as is.

2. Install:

   ```bash
   pip install getaccord
   ```

3. Buy something: hold money with each seller, then commit.

   ```python
   from getaccord import Accord

   accord = Accord()                        # reads ACCORD_AGENT_KEY and ACCORD_URL from the env or ./.env
   intent = accord.intent(budget=4500)      # whole cents; budget_micros= for exact sub-cent amounts
   intent.hold("skyfare", 3000, ref="SFO to JFK")
   intent.hold("stayco", 1500, ref="2 nights")
   result = intent.commit()                 # everyone is paid, or no one is
   print(result.status, result.intent["proofUrl"])
   ```

Amounts are whole cents (`int`) or exact micros (`amount_micros=`, millionths of a dollar). Floats are refused, so no amount is ever rounded.

## Buying

| | |
|---|---|
| `accord.intent(budget, *, budget_micros, coin, mode, expires_in, key)` | Opens a purchase. `coin`: the usual dollar coin or `"USDC"`. `mode`: `"all"` (everyone or no one) or `"each"`. |
| `intent.hold(seller, amount, *, amount_micros, ref, type)` | Locks money with one seller. `type`: `"reserve"` or `"deliver"`. |
| `intent.holds([...])` | Several holds in one step (one transaction). |
| `intent.commit(wait="60s")` | Waits for every seller to confirm, then pays them all. In `"all"` mode a decline or timeout releases everything. |
| `intent.abort()` | Calls it off; every held dollar comes back. |
| `intent.results()`, `intent.accept(i)`, `intent.reject(i)` | Deliver holds: check the work before anyone is paid. |
| `accord.resume(intent_id)` | A purchase made earlier, by its ID: commit, abort or add holds after a restart. |
| `accord.balance()` | What's available, and what's left of the key's weekly budget. |

Errors are `AccordError` with a stable `.code` (`limit_exceeded`, `seller_not_allowed`, `insufficient_balance`, `awaiting_approval`, `too_many_holds`, ...) and a message that says what to do.

A purchase over the key's sign-off limit waits for a person: `hold()` raises `awaiting_approval` and no money is locked. For a test key, the owner approves it with `accord.test.approve(intent.id, owner_token)`, passing the owner token `init` saved to `~/.accord/owner-<key id>`. The file holds comment lines and one line `ACCORD_OWNER_TOKEN=own_…`: pass only the `own_…` value. The SDK never reads that file itself, so an agent with only its key can't approve its own purchases. Dashboard keys are approved at app.useaccord.ai.

After a restart, `accord.resume(intent_id)` picks a purchase up again to commit or abort it.

Async: `AsyncAccord` has the same methods, awaitable.

```python
import asyncio
from getaccord import AsyncAccord

async def main():
    accord = AsyncAccord()
    intent = await accord.intent(budget=4500)
    await intent.hold("skyfare", 3000)
    result = await intent.commit()
    print(result.status)

asyncio.run(main())
```

## Paying x402 sellers

Sellers that charge per request with [x402](https://www.x402.org) can add an Accord option to their 402.

```python
from getaccord.x402 import pay

response = pay("https://seller.example/report")   # quote, hold, commit, fetch with the proof
print(response.status, response.json())
```

Several sellers in one purchase: `quote(url)`, `hold(intent, offer)` for each, `intent.commit()`, then `fetch_paid(offer, intent.id, result.digest, indexes)`.

Selling with x402: add `accord_requirement(pay_to=..., usd_cents=25, public_url='https://api.example.com')` (the URL buyers reach you at; required) to your 402's `accepts`, then check each paid request with `verify_accord_payment(header, requirement, resource=url)`. See `examples/x402_gateway.py`.

## Selling (webhooks)

Accord tells your server about each hold with a signed webhook. Answer whether you can deliver:

```python
from getaccord import Handler

handler = Handler(
    on_hold=lambda event: True,                 # ready (or False, or {"declined": "why"})
    on_commit=lambda event: print("paid", event["intentId"]),
)                                               # reads ACCORD_WEBHOOK_SECRET

# Flask
@app.post("/accord")
def accord_webhook():
    status, body = handler.handle(request.get_data(), request.headers)
    return body, status
```

With `async def` callbacks (FastAPI, aiohttp), use `status, body = await handler.handle_async(raw, headers)`.

`handle()` checks the `Accord-Signature` header (HMAC-SHA256, a 5-minute window against replays, old and new secrets during a rotation) and answers each event once. Give it the raw request body. `verify_signature(raw, header, secret)` is there on its own too.

## Examples

- `examples/trip_booker.py`: a flight, a hotel and a car, all paid or none (`--fail ridenow` to see a decline release everything).
- `examples/seller_webhook.py`: a seller answering webhooks with no framework.
- `examples/x402_gateway.py`: an x402 gateway paid through Accord.

## Tests

```bash
pip install -e '.[dev]'
pytest                                                    # unit tests
ACCORD_LIVE=1 ACCORD_URL=https://api.useaccord.ai pytest  # plus a live test-mode purchase
```

Signatures are checked byte-for-byte against the TypeScript SDK's.

## License

Apache-2.0
