> ## Documentation Index
> Fetch the complete documentation index at: https://shinydapps-bd9fa40b.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent SDK — Démarrage rapide

> Permettez à votre agent IA de payer automatiquement les API protégées par L402. Compatible avec les portefeuilles Blink ou Alby, contrôle du budget, prêt pour MCP et LangChain.

## Ce que cela fait

l402-kit embarque un **client** intégré qui permet à n'importe quel agent IA — ou n'importe quel script — d'appeler des API protégées par L402 sans avoir à écrire vous-même la boucle de paiement.

```
Agent  →  GET /api/data
API    →  402 + BOLT11 invoice
Agent  →  pay invoice (Blink / Alby)
Agent  →  GET /api/data  Authorization: L402 macaroon:preimage
API    →  200 ✓
```

Tout ce qui se passe entre les deux — analyse de la facture, appel au portefeuille, nouvelle tentative — est géré automatiquement.

***

## Node.js / TypeScript

### 1. Installation

```bash theme={null}
npm install l402-kit
```

### 2. Choisir un portefeuille

<Tabs>
  <Tab title="Blink">
    Obtenez vos identifiants sur [dashboard.blink.sv](https://dashboard.blink.sv) → Clés API.

    ```typescript theme={null}
    import { L402Client, BlinkWallet } from "l402-kit";

    const client = new L402Client({
      wallet: new BlinkWallet(
        process.env.BLINK_API_KEY!,
        process.env.BLINK_WALLET_ID!,
      ),
      budgetSats: 1000, // max spend per session
    });

    const res = await client.fetch("https://api.example.com/premium");
    const data = await res.json();
    console.log(data);
    ```
  </Tab>

  <Tab title="Alby">
    Obtenez votre jeton d'accès sur [getalby.com](https://getalby.com) → Paramètres → Jetons d'accès.

    ```typescript theme={null}
    import { L402Client, AlbyWallet } from "l402-kit";

    const client = new L402Client({
      wallet: new AlbyWallet(process.env.ALBY_TOKEN!),
      budgetSats: 1000,
    });

    const res = await client.fetch("https://api.example.com/premium");
    const data = await res.json();
    console.log(data);
    ```
  </Tab>
</Tabs>

***

## Python

### 1. Installation

```bash theme={null}
pip install l402kit
```

### 2. Appeler l'API

<Tabs>
  <Tab title="Blink">
    ```python theme={null}
    import os
    from l402kit import L402Client
    from l402kit.wallets import BlinkWallet

    client = L402Client(
        wallet=BlinkWallet(os.environ["BLINK_API_KEY"], os.environ["BLINK_WALLET_ID"]),
        budget_sats=1000,
        on_spend=lambda sats, url: print(f"Paid {sats} sats → {url}"),
    )

    r = client.get("https://api.example.com/premium")
    print(r.json())
    ```
  </Tab>

  <Tab title="Alby">
    ```python theme={null}
    import os
    from l402kit import L402Client
    from l402kit.wallets import AlbyWallet

    client = L402Client(
        wallet=AlbyWallet(os.environ["ALBY_TOKEN"]),
        budget_sats=1000,
    )

    r = client.get("https://api.example.com/premium")
    print(r.json())
    ```
  </Tab>
</Tabs>

***

## Ce qui se passe étape par étape

1. `client.fetch` / `client.get` envoie la requête sans en-tête d'authentification
2. Si l'API renvoie `402`, le client lit l'`invoice` et le `macaroon` depuis le corps de la réponse (ou l'en-tête `WWW-Authenticate`)
3. Il vérifie votre budget — lève une erreur `BudgetExceededError` si le prix dépasse la limite
4. Il appelle `wallet.payInvoice(bolt11)` et attend le preimage
5. Il relance la requête avec `Authorization: L402 <macaroon>:<preimage>`
6. Renvoie la `Response`/`httpx.Response` finale à votre code

Pas de 402 ? La réponse est transmise telle quelle.

***

## Rapport de dépenses

```typescript theme={null}
// TypeScript
const report = client.spendingReport();
// { total: 42, remaining: 958, byDomain: { "api.example.com": 42 }, transactions: [...] }
```

```python theme={null}
# Python
report = client.spending_report()
print(f"Spent {report.total} sats, {report.remaining} remaining")
```

***

## Prochaines étapes

<CardGroup cols={2}>
  <Card title="Portefeuilles" icon="wallet" href="/agent/wallets">
    Blink, Alby — configuration complète et options
  </Card>

  <Card title="Contrôle du budget" icon="shield-halved" href="/agent/budget">
    Limites par domaine, callbacks, rapports
  </Card>

  <Card title="Serveur MCP" icon="robot" href="/agent/mcp">
    Intégration Claude Desktop en 2 minutes
  </Card>

  <Card title="LangChain" icon="link" href="/agent/langchain">
    Outil clé en main pour les agents LangChain
  </Card>
</CardGroup>
