# @xapi-fi/sdk

Official SDK for xAPI-Fi - Monetize any API endpoint with x402 micropayments.

## Installation

```bash
npm install @xapi-fi/sdk
```

## Quick Start

### Server-Side (Express/Next.js)

```typescript
import XAPIFi from '@xapi-fi/sdk/server';

const xapi = new XAPIFi({
  apiKey: process.env.XAPI_FI_KEY
});

// Protect your endpoint
app.post('/api/generate',
  xapi.protect({ price: 0.001 }),
  async (req, res) => {
    const result = await generateContent(req.body);
    res.json(result);
  }
);
```

### Client-Side

```typescript
import { XAPIFiClient } from '@xapi-fi/sdk/client';

const client = new XAPIFiClient();

// Make a request to a protected endpoint
const response = await client.fetch('/api/generate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ prompt: 'Hello world' })
});
```

## Configuration

### Server SDK Options

```typescript
interface XAPIFiConfig {
  apiKey: string;                      // Required: Your xAPI-Fi API key
  network?: 'solana' | 'solana-devnet'; // Optional: Network to use
  facilitatorUrl?: string;              // Optional: Custom facilitator URL
  apiBaseUrl?: string;                  // Optional: Custom API base URL
}
```

### Protection Options

```typescript
interface ProtectOptions {
  price: number;                        // Required: Price in USD
  description?: string;                 // Optional: Endpoint description
  maxTimeout?: number;                  // Optional: Payment verification timeout
  customRules?: (req: any) => Promise<boolean>; // Optional: Custom validation
}
```

## Features

- **One-Line Integration**: Add payment gates with a single middleware
- **Framework Agnostic**: Works with Express, Next.js, and more
- **Type Safe**: Full TypeScript support
- **x402 Protocol**: Built on the x402 micropayment standard
- **Zero Gas Fees**: Users don't pay blockchain gas fees

## Documentation

Full documentation available at [docs.xapi-fi.com](https://docs.xapi-fi.com)

## License

MIT
