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

# Get Post Statistics

> Retrieve post statistics with filters

## Endpoint

```
POST https://api.ugc.inc/stats/posts
```

## Overview

Get post statistics including views, likes, comments, and shares.

**Behavior:**

* **If `startDate` and `endDate` are NOT provided:** Returns the latest stat per post (one record per post) - optimal for dashboards showing current state
* **If `startDate` or `endDate` is provided:** Returns all stats within the date range - use for historical analysis and trend tracking

Use this endpoint to track post performance over time.

## Request Body

<ParamField body="postIds" type="string[]">
  Array of post IDs to get stats for. Omit to get all posts.
</ParamField>

<ParamField body="startDate" type="string">
  Start date in ISO 8601 format (e.g., `2024-01-01T00:00:00Z`). Optional. If omitted (along with `endDate`), returns latest stat per post.
</ParamField>

<ParamField body="endDate" type="string">
  End date in ISO 8601 format (e.g., `2024-12-31T23:59:59Z`). Optional. If omitted (along with `startDate`), returns latest stat per post.
</ParamField>

## Response

<ResponseField name="data" type="PostStat[]">
  Array of post statistics objects

  <Expandable title="PostStat properties">
    <ResponseField name="id" type="string">
      Unique stat record identifier
    </ResponseField>

    <ResponseField name="post_id" type="string">
      Associated post ID
    </ResponseField>

    <ResponseField name="views" type="number | null">
      View count at the time of recording
    </ResponseField>

    <ResponseField name="likes" type="number | null">
      Like count at the time of recording
    </ResponseField>

    <ResponseField name="comments" type="number | null">
      Comment count at the time of recording
    </ResponseField>

    <ResponseField name="shares" type="number | null">
      Share count at the time of recording
    </ResponseField>

    <ResponseField name="created_at" type="string">
      ISO 8601 UTC timestamp (ends in Z) when the stat was recorded
    </ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.ugc.inc/stats/posts \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "postIds": ["post_123456", "post_789012"],
      "startDate": "2024-01-01T00:00:00Z",
      "endDate": "2024-12-31T23:59:59Z"
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://api.ugc.inc/stats/posts',
      headers={
          'Authorization': 'Bearer YOUR_API_KEY',
          'Content-Type': 'application/json'
      },
      json={
          'postIds': ['post_123456', 'post_789012'],
          'startDate': '2024-01-01T00:00:00Z',
          'endDate': '2024-12-31T23:59:59Z'
      }
  )

  data = response.json()

  if data['ok']:
      for stat in data['data']:
          print(f"Post {stat['post_id']}: {stat['views']} views, {stat['likes']} likes")
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.ugc.inc/stats/posts', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      postIds: ['post_123456', 'post_789012'],
      startDate: '2024-01-01T00:00:00Z',
      endDate: '2024-12-31T23:59:59Z'
    })
  });

  const data = await response.json();

  if (data.ok) {
    data.data.forEach(stat => {
      console.log(`Post ${stat.post_id}:`);
      console.log(`  Views: ${stat.views}`);
      console.log(`  Likes: ${stat.likes}`);
      console.log(`  Comments: ${stat.comments}`);
    });
  }
  ```

  ```typescript React theme={null}
  import { UGCClient } from 'ugcinc';

  const client = new UGCClient({
    apiKey: 'YOUR_API_KEY'
  });

  const response = await client.stats.getPostStats({
    postIds: ['post_123456', 'post_789012'],
    startDate: '2024-01-01T00:00:00Z',
    endDate: '2024-12-31T23:59:59Z'
  });

  if (response.ok) {
    response.data.forEach(stat => {
      console.log(`Post ${stat.post_id}: ${stat.views} views, ${stat.likes} likes`);
    });
  }
  ```
</RequestExample>

<ResponseExample>
  ```json Success Response theme={null}
  {
    "ok": true,
    "code": 200,
    "message": "Success",
    "data": [
      {
        "id": "pstat_abc123",
        "post_id": "post_123456",
        "views": 125000,
        "likes": 8950,
        "comments": 342,
        "shares": 567,
        "created_at": "2024-12-25T10:00:00Z"
      },
      {
        "id": "pstat_def456",
        "post_id": "post_123456",
        "views": 128500,
        "likes": 9120,
        "comments": 356,
        "shares": 589,
        "created_at": "2024-12-26T10:00:00Z"
      }
    ]
  }
  ```
</ResponseExample>
