> ## Documentation Index
> Fetch the complete documentation index at: https://docs.delegare.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Typescript SDK

> The official @delegare/sdk for Node.js and Browser.

The Delegare SDK provides a simple, type-safe interface for interacting with the Delegare protocol.

## Installation

```bash theme={null}
pnpm add @delegare/sdk
# or
npm install @delegare/sdk
```

## Initialization

Initialize the client with your Merchant credentials.

```typescript theme={null}
import { Delegare } from '@delegare/sdk';

const delegare = new Delegare({
  merchantId: 'm_123...',
  apiKey: process.env.DELEGARE_API_KEY,
  // Use sandbox URL for testing
  baseUrl: 'https://api.sandbox.delegare.dev/v1' 
});
```

## Core Methods

### Create Setup Session

Generates a URL where your user can authorize a spending mandate.

```typescript theme={null}
const session = await delegare.createSetupSession({
  maxPerTxCents: 1000,         // $10.00
  maxMonthlySpendCents: 5000,   // $50.00
  rail: 'both',                 // allow fiat and crypto
  redirectUrl: 'https://your-app.com/callback'
});

console.log(session.setupUrl);
```

### Charge Mandate

Executes a payment using an `intentMandate` (SD-JWT-VC) provided by an agent.

```typescript theme={null}
const receipt = await delegare.charge({
  intentMandate: 'eyJhbGci...',
  amountCents: 1500,
  currency: 'usd',
  description: 'AI Agent Purchase',
  idempotencyKey: 'order_789'
});

if (receipt.status === 'completed') {
  console.log('Payment successful!', receipt.receiptId);
}
```

### Check Balance

Query the remaining monthly limit for a specific mandate.

```typescript theme={null}
const balance = await delegare.getMandateBalance('mandate_id_here');
console.log(`Remaining: $${balance.remainingMonthlyCents / 100}`);
```

### Fetch (x402 Auto-Payment)

A drop-in replacement for the native `fetch` API that automatically intercepts and handles `x402` (HTTP 402 Payment Required) challenges.

If the requested endpoint returns a `402` status with `X-PAYMENT` requirements, the SDK will:

1. Verify the price is within the agent's pre-authorized spending mandate.
2. Sign the required transaction automatically under the hood (zero popups).
3. Append the signed `X-Payment` header and seamlessly retry the request.

```typescript theme={null}
// Works exactly like a normal fetch, but passes the mandate as the 3rd argument
const response = await delegare.fetch(
  'https://api.example.com/premium-data', 
  { method: 'GET' }, 
  intentMandate // Your agent's SD-JWT-VC spending token
);

const data = await response.json();
```
