> ## 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 — Inicio Rápido

> Permite que tu agente de IA pague APIs protegidas con L402 automáticamente. Compatible con billeteras Blink o Alby, control de presupuesto, MCP y LangChain.

## Qué hace esto

l402-kit incluye un **cliente** integrado que permite a cualquier agente de IA — o cualquier script — llamar a APIs protegidas con L402 sin necesidad de escribir el bucle de pago tú mismo.

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

Todo lo que ocurre en medio — análisis de la factura, llamada a la billetera, reintento — se gestiona automáticamente.

***

## Node.js / TypeScript

### 1. Instalar

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

### 2. Elige una billetera

<Tabs>
  <Tab title="Blink">
    Obtén tus credenciales en [dashboard.blink.sv](https://dashboard.blink.sv) → API Keys.

    ```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, // gasto máximo por sesión
    });

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

  <Tab title="Alby">
    Obtén tu token de acceso en [getalby.com](https://getalby.com) → Settings → Access Tokens.

    ```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. Instalar

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

### 2. Llama a la 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>

***

## Qué ocurre paso a paso

1. `client.fetch` / `client.get` envía la solicitud sin ninguna cabecera de autenticación
2. Si la API devuelve `402`, el cliente lee el `invoice` y el `macaroon` del cuerpo de la respuesta (o de la cabecera `WWW-Authenticate`)
3. Verifica tu presupuesto — lanza `BudgetExceededError` si el precio superaría el límite
4. Llama a `wallet.payInvoice(bolt11)` y espera el preimage
5. Reintenta la solicitud con `Authorization: L402 <macaroon>:<preimage>`
6. Devuelve la `Response`/`httpx.Response` final a tu código

¿Sin 402? La respuesta se devuelve tal cual, sin modificaciones.

***

## Informe de gastos

```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")
```

***

## Próximos pasos

<CardGroup cols={2}>
  <Card title="Billeteras" icon="wallet" href="/agent/wallets">
    Blink, Alby — configuración completa y opciones
  </Card>

  <Card title="Control de presupuesto" icon="shield-halved" href="/agent/budget">
    Límites por dominio, callbacks e informes
  </Card>

  <Card title="Servidor MCP" icon="robot" href="/agent/mcp">
    Integración con Claude Desktop en 2 minutos
  </Card>

  <Card title="LangChain" icon="link" href="/agent/langchain">
    Herramienta lista para usar con agentes LangChain
  </Card>
</CardGroup>
