> ## Documentation Index
> Fetch the complete documentation index at: https://x402-stellar.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Monetizing an AI Model API

> Step-by-step tutorial: Paywalling an OpenAI-compatible endpoint with Stellar x402

## Scenario

You have an open-source LLM inference endpoint (e.g., running via vLLM, Ollama, or Python FastAPI) exposing:

```
POST /v1/chat/completions
```

You want to charge AI agents **0.01 USDC** per completed inference request without requiring user accounts or credit cards.

## Step 1: Upstream FastAPI Service

Assume your FastAPI service is listening on `http://127.0.0.1:8000`:

```python theme={null}
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class CompletionRequest(BaseModel):
    prompt: str

@app.post("/v1/chat/completions")
def complete(req: CompletionRequest):
    return {"reply": f"Inference response for: {req.prompt}"}
```

## Step 2: Configure the Proxy

Create `config.yaml`:

```yaml theme={null}
listen_addr: ":8080"
network: "stellar:testnet"

routes:
  - path: "/v1/chat/completions"
    upstream: "http://127.0.0.1:8000"
    asset: "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC"
    price: "0.01"
    recipient: "GCALKSGAZRJLSUEJT3M5W6LN4R7XQOLIRCOS6ZA6EDZVTZDBIIPPFKJ6"
```

Start the proxy:

```bash theme={null}
go run cmd/gateway/main.go --config config.yaml
```

## Step 3: Agent Client Request

An autonomous caller executes the query using `@stellar-x402/client`:

```typescript theme={null}
import { StellarX402Client } from '@stellar-x402/client';

const client = new StellarX402Client({
  payerSecretKey: process.env.AGENT_SECRET!,
  maxSpendPerCall: '0.05',
  network: 'stellar:testnet',
});

const res = await client.fetch('http://localhost:8080/v1/chat/completions', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ prompt: 'Generate quarterly earnings summary' }),
});

const data = await res.json();
console.log(data.reply);
```

The gateway manages the 402 challenge negotiation, the client signs the authorization, the request reaches your FastAPI server, and payment is verified on-chain.
