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

# Import Click History

> Import historical click records for previously sent messages.

Bulk import up to 1,000 historical click records per request. Each click is associated with an existing message and offer. Only one click per `message_id` is stored (MessageClick uses `message_id` as its primary key), so duplicate entries are automatically skipped.

Importing clicks also updates the associated ListContact's denormalized stats: `click_count` and `last_clicked_at`.

## Authentication

<ParamField header="X-Api-Key" type="string" required>
  Your Trackly SMS API key. Format: `trk_[32-char-alphanumeric]`.
</ParamField>

## Body Parameters

<ParamField body="records" type="array" required>
  An array of click records to import. Maximum of 1,000 records per request. Each record accepts the following fields:

  <Expandable title="Record fields">
    <ParamField body="message_id" type="string" required>
      The message ID this click is associated with. The message must already exist in the system.
    </ParamField>

    <ParamField body="offer_id" type="string" required>
      The offer ID that was clicked. The offer must already exist in the system.
    </ParamField>

    <ParamField body="phone_number" type="string" required>
      The contact's phone number in E.164 format.
    </ParamField>

    <ParamField body="send_timestamp" type="datetime">
      The original send timestamp of the associated message in ISO 8601 format.
    </ParamField>

    <ParamField body="timestamp" type="datetime" required>
      The click timestamp in ISO 8601 format.
    </ParamField>
  </Expandable>
</ParamField>

## Response Fields

<ResponseField name="success_count" type="integer">
  Number of click records successfully imported.
</ResponseField>

<ResponseField name="error_count" type="integer">
  Number of records that failed validation or processing.
</ResponseField>

<ResponseField name="duplicates_skipped" type="integer">
  Number of records skipped because a click already exists for that `message_id`.
</ResponseField>

<ResponseField name="errors" type="array">
  Array of error objects (maximum 100 returned). Each object contains:

  * `index` (integer) -- Position of the failed record in the input array.
  * `message_id` (string) -- The `message_id` of the failed record.
  * `code` (string) -- Machine-readable error code.
  * `error` (string) -- Human-readable error description.
</ResponseField>

<RequestExample>
  <CodeGroup>
    ```bash cURL theme={null}
    curl -X POST https://api.tracklysms.com/api/v2/history/clicks \
      -H "X-Api-Key: trk_your_api_key_here" \
      -H "Content-Type: application/json" \
      -d '{
        "records": [
          {
            "message_id": "a1b2c3d4",
            "offer_id": "offer_123",
            "phone_number": "+12025559876",
            "send_timestamp": "2025-11-15T14:30:00Z",
            "timestamp": "2025-11-15T14:35:22Z"
          },
          {
            "message_id": "e5f6g7h8",
            "offer_id": "offer_456",
            "phone_number": "+13105558888",
            "timestamp": "2025-11-15T15:10:45Z"
          }
        ]
      }'
    ```

    <Note>
      `message_id` is an opaque identifier — do not assume a fixed length or format. Store them as variable-length strings.
    </Note>

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

    response = requests.post(
        "https://api.tracklysms.com/api/v2/history/clicks",
        headers={
            "X-Api-Key": "trk_your_api_key_here",
            "Content-Type": "application/json",
        },
        json={
            "records": [
                {
                    "message_id": "a1b2c3d4",
                    "offer_id": "offer_123",
                    "phone_number": "+12025559876",
                    "send_timestamp": "2025-11-15T14:30:00Z",
                    "timestamp": "2025-11-15T14:35:22Z",
                },
                {
                    "message_id": "e5f6g7h8",
                    "offer_id": "offer_456",
                    "phone_number": "+13105558888",
                    "timestamp": "2025-11-15T15:10:45Z",
                },
            ],
        },
    )

    print(response.json())
    ```

    ```javascript Node.js theme={null}
    const response = await fetch("https://api.tracklysms.com/api/v2/history/clicks", {
      method: "POST",
      headers: {
        "X-Api-Key": "trk_your_api_key_here",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        records: [
          {
            message_id: "a1b2c3d4",
            offer_id: "offer_123",
            phone_number: "+12025559876",
            send_timestamp: "2025-11-15T14:30:00Z",
            timestamp: "2025-11-15T14:35:22Z",
          },
          {
            message_id: "e5f6g7h8",
            offer_id: "offer_456",
            phone_number: "+13105558888",
            timestamp: "2025-11-15T15:10:45Z",
          },
        ],
      }),
    });

    const data = await response.json();
    console.log(data);
    ```
  </CodeGroup>
</RequestExample>

<ResponseExample>
  ```json 201 - Success theme={null}
  {
    "success_count": 2,
    "error_count": 0,
    "duplicates_skipped": 0,
    "errors": []
  }
  ```

  ```json 201 - Partial Success theme={null}
  {
    "success_count": 1,
    "error_count": 0,
    "duplicates_skipped": 1,
    "errors": []
  }
  ```

  ```json 400 - Validation Error theme={null}
  {
    "error": "records array is required",
    "code": "missing_records"
  }
  ```

  ```json 413 - Payload Too Large theme={null}
  {
    "error": "Maximum 1000 records per request",
    "code": "too_many_records"
  }
  ```
</ResponseExample>

## Error Codes

Only two conditions reject the whole request. Every other error is reported **per record**: the request still returns `201`, and each failed record appears in the response's `errors[]` array with a `code` (see the Partial Success example above).

### Request errors (HTTP status)

| HTTP Status | Error Code         | Description                                                    |
| ----------- | ------------------ | -------------------------------------------------------------- |
| 400         | `missing_records`  | The `records` field is required and must be a non-empty array. |
| 413         | `too_many_records` | Exceeded the maximum of 1,000 records per request.             |

Authenticated requests can also fail with `401 invalid_credentials`, `403 account_suspended`, or `429 rate_limited` — see [Error Codes](/api-reference/v2/error-codes).

### Per-record errors (`errors[].code`, returned with HTTP 201)

| Error Code             | Description                                           |
| ---------------------- | ----------------------------------------------------- |
| `missing_message_id`   | A record is missing the `message_id` field.           |
| `missing_offer_id`     | A record is missing the `offer_id` field.             |
| `missing_phone_number` | A record is missing the `phone_number` field.         |
| `missing_timestamp`    | A record is missing the `timestamp` field.            |
| `message_not_found`    | No message exists with the given `message_id`.        |
| `offer_not_found`      | No offer exists with the given `offer_id`.            |
| `invalid_timestamp`    | The timestamp is not a valid ISO 8601 datetime.       |
| `save_error`           | An unexpected error occurred while saving the record. |

## Next Steps

<CardGroup cols={2}>
  <Card title="Reporting Overview" icon="chart-bar" href="/guides/reporting/overview">
    Analyze click data
  </Card>

  <Card title="Bulk Create Contacts" icon="users" href="/api-reference/v2/contacts/bulk-create">
    Import contacts alongside history
  </Card>
</CardGroup>
