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

# Request History

> Display a paginated log of AI requests with costs, models, and metadata for end users

## Problem

You want to show customers a log of their AI requests — model used, tokens consumed, cost, and status — with pagination and optional filtering.

## Solution

Use `requests.list()` with a `customer_id` filter and map the results into a display-friendly format.

```typescript theme={null}
import { Lava } from '@lavapayments/nodejs';

const lava = new Lava();

async function getRequestHistory(customerId: string, cursor?: string) {
  const response = await lava.requests.list({
    customer_id: customerId,
    limit: 20,
    cursor,
  });

  return {
    requests: response.data.map((req) => ({
      id: req.request_id,
      date: req.created_at,
      provider: req.provider,
      model: req.model,
      tokens: req.model_usage.total_tokens,
      cost: parseFloat(req.cost).toFixed(4),
      status: req.status,
    })),
    hasMore: response.has_more,
    nextCursor: response.next_cursor,
  };
}
```

### API Route with Pagination

```typescript theme={null}
app.get('/api/requests', async (req, res) => {
  const { cursor } = req.query;
  const history = await getRequestHistory(
    req.user.customerId,
    cursor as string | undefined
  );
  res.json(history);
});
```

### Filter by Feature

Use metadata headers when making AI requests, then filter on them later:

```typescript theme={null}
// When making requests — tag with metadata
headers['X-Lava-Metadata-Feature'] = 'chat';
headers['X-Lava-Metadata-Session'] = sessionId;

// When querying — filter by metadata
const chatRequests = await lava.requests.list({
  customer_id: customerId,
  metadata_filters: { feature: 'chat' },
  limit: 50,
});
```

### Export to CSV

```typescript theme={null}
async function exportToCSV(customerId: string) {
  const allRequests = [];
  let cursor: string | undefined;

  do {
    const response = await lava.requests.list({
      customer_id: customerId,
      limit: 100,
      cursor,
    });
    allRequests.push(...response.data);
    cursor = response.next_cursor;
  } while (cursor);

  const lines = [
    'Date,Provider,Model,Tokens,Cost',
    ...allRequests.map((r) =>
      [r.created_at, r.provider, r.model, r.model_usage.total_tokens, r.cost].join(',')
    ),
  ];

  return lines.join('\n');
}
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Usage Analytics" icon="chart-line" href="/cookbook/usage-analytics">
    Aggregate usage data for dashboards and billing reports
  </Card>

  <Card title="Forward Proxy" icon="arrow-right" href="/gateway/forward-proxy">
    Learn how to attach metadata headers to proxy requests
  </Card>
</CardGroup>
