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

# x402 Payments

> Enable autonomous agent payments with spending policies

# x402: The Agentic Economy

Agntor handles the `402 Payment Required` handshake automatically, enabling your agents to make verified payments without human intervention.

## How It Works

<Steps>
  <Step title="Request">
    Your agent hits a paid API endpoint.
  </Step>

  <Step title="Challenge">
    The server returns an x402 header with payment requirements.
  </Step>

  <Step title="Audit">
    Agntor checks if the transaction fits the agent's **Spend Policy**.
  </Step>

  <Step title="Settlement">
    If safe, Agntor signs the attestation and releases the payment.
  </Step>
</Steps>

## Setting a Spend Policy

Define spending limits and restrictions for your agent:

```typescript theme={null}
import { AgntorClient } from '@agntor/sdk';

const agntor = new AgntorClient({
  apiKey: process.env.AGNTOR_API_KEY
});

await agntor.payments.setPolicy({
  // Daily spending limit
  dailyLimit: "0.01 ETH",
  
  // Approved domains for payments
  allowedDomains: [
    "*.cloud-compute.com",
    "api.search.com",
    "storage.provider.io"
  ],
  
  // Human approval threshold
  requireHumanApprovalAbove: "0.05 ETH",
  
  // Per-transaction limit
  maxTransactionAmount: "0.005 ETH"
});
```

## Payment Flow Example

```typescript theme={null}
import { AgntorShield } from '@agntor/core';

const shield = new AgntorShield({
  identity: 'eip8004:0x...',
  policy: 'enterprise-standard',
  maxSpend: '0.05_ETH_x402'
});

// Agent makes a request to a paid API
const result = await shield.execute(async (ctx) => {
  // Agntor automatically handles x402 negotiation
  const response = await fetch('https://api.compute-provider.com/gpu', {
    headers: {
      'X-Agntor-Identity': ctx.identity
    }
  });
  
  return response.json();
});
```

## Payment Events

Monitor all payment activity:

```typescript theme={null}
agntor.payments.on('transaction', (event) => {
  console.log(`Amount: ${event.amount}`);
  console.log(`Recipient: ${event.recipient}`);
  console.log(`Status: ${event.status}`);
  console.log(`Attestation: ${event.attestationTx}`);
});

agntor.payments.on('blocked', (event) => {
  console.warn(`Payment blocked: ${event.reason}`);
  // Reasons: EXCEEDS_LIMIT, DOMAIN_NOT_ALLOWED, REQUIRES_APPROVAL
});
```

## Security Features

| Feature           | Description                                                   |
| ----------------- | ------------------------------------------------------------- |
| **Escrow**        | Funds held in smart contract until service delivery confirmed |
| **Attestation**   | Every payment linked to on-chain identity                     |
| **Rate Limiting** | Automatic throttling to prevent runaway spending              |
| **Audit Trail**   | Complete transaction history for compliance                   |

<Warning>
  Always set appropriate spending limits. Agents without limits may drain funds if compromised.
</Warning>

## Next Steps

<CardGroup cols={2}>
  <Card title="Compliance" icon="clipboard-check" href="/compliance">
    Learn about SOC2-level compliance for agents.
  </Card>

  <Card title="MCP Integration" icon="plug" href="/mcp-integration">
    Secure your agent's tool access.
  </Card>
</CardGroup>
