> ## 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.

# Express Checkout Example

> A complete e-commerce mock using the Delegare SDK.

This example demonstrates how a merchant backend can accept `intentMandate` (SD-JWT-VC) tokens from an agent and process a payment via the Delegare SDK.

The source code is available in the `examples/express-checkout` directory of the Delegare repository.

## Overview

1. **Setup Buyer:** The merchant creates a setup session for the user.
2. **Authorization:** The user completes the setup via the Delegare UI, generating an Intent Mandate.
3. **Checkout:** The AI agent sends the Intent Mandate to the merchant's `/api/checkout` endpoint.
4. **Execution:** The merchant backend uses `@delegare/sdk` to charge the mandate.

## Merchant Backend (Node.js)

```javascript theme={null}
const { Delegare } = require('@delegare/sdk');
const express = require('express');

const delegare = new Delegare({
  merchantId: process.env.DELEGARE_MERCHANT_ID,
  apiKey: process.env.DELEGARE_API_KEY,
  baseUrl: 'https://api.sandbox.delegare.dev/v1'
});

const app = express();
app.use(express.json());

app.post('/api/checkout', async (req, res) => {
  const { intentMandate, item } = req.body;

  try {
    // 1. Charge the agent!
    const receipt = await delegare.charge({
      intentMandate,
      amountCents: 1500, // $15.00
      currency: 'usd',
      description: `Order: ${item}`,
      idempotencyKey: `order_${Date.now()}`
    });

    res.json({ success: true, receipt });
  } catch (error) {
    res.status(402).json({ error: error.message });
  }
});

app.listen(4000);
```

## Running the Demo locally

If you have the Delegare repository cloned, you can run the interactive demo:

```bash theme={null}
cd examples/express-checkout
pnpm install
# Follow instructions in README to set your .env
pnpm start
```

Then, in a separate terminal, run the buyer setup script:

```bash theme={null}
node setup-buyer.js
```

This script will give you a setup URL. Open it, complete the flow in Sandbox mode, and the script will automatically output a `curl` command you can use to test the checkout!

## Testing the x402 API Paywall

This example also demonstrates the `@delegare/x402` middleware, which automatically protects the `/api/premium-data` endpoint and charges AI agents \$0.05 per request.

> **⚠️ IMPORTANT: x402 requires Crypto (USDC)**
> The `x402` protocol settles natively on-chain. To test this flow, your "Buyer" account MUST have a **Crypto Wallet** connected.
>
> 1. Ensure you are testing from a separate "Buyer" account (not your Merchant account). Use an incognito window if necessary.
> 2. Ensure that Buyer account has connected a Coinbase Smart Wallet (or similar) loaded with testnet USDC and ETH on Base Sepolia.
> 3. When you run `node setup-buyer.js` to generate the mandate, **you must authorize the Crypto spending option**, which will require you to actively sign a transaction on the Sepolia blockchain in your wallet. (A Fiat-only mandate will fail the x402 check!)

You can test this flow locally using the provided `x402.js` script!

Simply add your *Crypto-authorized* mandate token to your `.env` file:

```env theme={null}
DELEGARE_MANDATE=eyJhbGci...
```

Then run the script:

```bash theme={null}
pnpm x402
```

The script uses `delegare.fetch()` to automatically detect the `402 Payment Required` challenge, authorize the \$0.05 USDC payment via the Delegare SDK, and retry the request to securely fetch the premium data!

> **💡 Note on Dashboard Tracking:**
> If you provide a raw 0x address in `MERCHANT_USDC_WALLET` inside your `.env` that is *not* connected to your Merchant profile, the payment will succeed on-chain, but the Delegare backend won't know it belongs to you! It will be recorded under a generic "Web3 Merchant" and won't appear in your dashboard. To see x402 transactions in your dashboard, make sure you connect that exact USDC wallet via the **Payment Methods** tab first.
