# Export messages by location ID

GET https://services.leadconnectorhq.com/conversations/messages/export

Export messages for a specific location with cursor-based pagination support.

**Channel Filtering Behavior:**
- **When channel is omitted:** Returns all non-email message types, including messages that don't belong to any specific channel.
- **When channel=Email:** Returns email messages only.
- **When channel is specified (SMS, Call, WhatsApp, etc.):** Returns messages for that specific channel.

**Limitations:**
- Group Chat and SMS Review Request message types are not supported.
- Cursor validity is 2 minutes from the last request made.

## Authentication

Send `Authorization: Bearer <token>` using:

- **Sub-Account token: OAuth access token or Private Integration Token.** Scopes: `conversations/message.readonly`

## Parameters

### Header Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `Version` | string | Yes | API Version |

### Query Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `locationId` | string | Yes | Location ID to filter messages by |
| `channel` | string | No | Filter by message channel. Optional - when not provided, all non-email message types will be returned including activity messages (opportunity updates, appointments, etc.). To fetch email messages, you must explicitly set channel=Email. |
| `limit` | number | No | Number of messages to return per page |
| `cursor` | string | No | Cursor for pagination. Pass the nextCursor from previous response to get next page. |
| `sortBy` | string | No | Field to sort by |
| `sortOrder` | string | No | Sort order |
| `conversationId` | string | No | Filter messages by conversation ID |
| `contactId` | string | No | Filter messages by contact ID |
| `startDate` | string | No | Start date to filter messages by |
| `endDate` | string | No | End date to filter messages by |

## OpenAPI Specification

```json
{
  "openapi": "3.0.0",
  "info": {
    "title": "LeadConnector API",
    "version": "v3"
  },
  "servers": [
    {
      "url": "https://services.leadconnectorhq.com"
    }
  ],
  "paths": {
    "/conversations/messages/export": {
      "get": {
        "summary": "Export messages by location ID",
        "operationId": "exportMessagesByLocationId",
        "description": "Export messages for a specific location with cursor-based pagination support.\n\n**Channel Filtering Behavior:**\n- **When channel is omitted:** Returns all non-email message types, including messages that don't belong to any specific channel.\n- **When channel=Email:** Returns email messages only.\n- **When channel is specified (SMS, Call, WhatsApp, etc.):** Returns messages for that specific channel.\n\n**Limitations:**\n- Group Chat and SMS Review Request message types are not supported.\n- Cursor validity is 2 minutes from the last request made.",
        "parameters": [
          {
            "name": "Version",
            "in": "header",
            "description": "API Version",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "v3"
              ]
            }
          },
          {
            "name": "locationId",
            "required": true,
            "in": "query",
            "description": "Location ID to filter messages by",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "channel",
            "required": false,
            "in": "query",
            "description": "Filter by message channel. Optional - when not provided, all non-email message types will be returned including activity messages (opportunity updates, appointments, etc.). To fetch email messages, you must explicitly set channel=Email.",
            "schema": {
              "enum": [
                "Call",
                "SMS",
                "Email",
                "WhatsApp",
                "Instagram",
                "Facebook"
              ],
              "type": "string"
            }
          },
          {
            "name": "limit",
            "required": false,
            "in": "query",
            "description": "Number of messages to return per page",
            "schema": {
              "default": 100,
              "minimum": 10,
              "maximum": 1000,
              "type": "number"
            }
          },
          {
            "name": "cursor",
            "required": false,
            "in": "query",
            "description": "Cursor for pagination. Pass the nextCursor from previous response to get next page.",
            "schema": {
              "example": "a748514c-f49e-4fa8-9954-b53afc78d81d",
              "type": "string"
            }
          },
          {
            "name": "sortBy",
            "required": false,
            "in": "query",
            "description": "Field to sort by",
            "schema": {
              "default": "createdAt",
              "enum": [
                "createdAt",
                "updatedAt"
              ],
              "type": "string"
            }
          },
          {
            "name": "sortOrder",
            "required": false,
            "in": "query",
            "description": "Sort order",
            "schema": {
              "default": "desc",
              "enum": [
                "asc",
                "desc"
              ],
              "type": "string"
            }
          },
          {
            "name": "conversationId",
            "required": false,
            "in": "query",
            "description": "Filter messages by conversation ID",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "contactId",
            "required": false,
            "in": "query",
            "description": "Filter messages by contact ID",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "startDate",
            "required": false,
            "in": "query",
            "description": "Start date to filter messages by",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "endDate",
            "required": false,
            "in": "query",
            "description": "End date to filter messages by",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of messages for the location with pagination details.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExportMessagesResponseDto"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BadRequestDTO"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnauthorizedDTO"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearer": [
              "conversations/message.readonly"
            ]
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Sub-Account (OR) Private Integration Token of Sub-Account.",
        "type": "http"
      },
      "Location-Access": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Sub-Account (OR) Private Integration Token of Sub-Account.",
        "type": "http"
      },
      "Location-Access-Only": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Sub-Account.",
        "type": "http"
      },
      "Agency-Access": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Agency (OR) Private Integration Token of Agency.",
        "type": "http"
      }
    },
    "schemas": {
      "ExportMessagesResponseDto": {
        "type": "object",
        "properties": {
          "messages": {
            "description": "Array of messages",
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "id": {
                  "type": "string",
                  "example": "ve9EPM428h8vShlRW1KT"
                },
                "altId": {
                  "type": "string",
                  "description": "Alternative identifier for the message",
                  "example": "msg_123456789"
                },
                "type": {
                  "type": "number",
                  "example": 1
                },
                "messageType": {
                  "type": "string",
                  "description": "Type of the message as a string",
                  "example": "SMS",
                  "enum": [
                    "TYPE_CALL",
                    "TYPE_SMS",
                    "TYPE_EMAIL",
                    "TYPE_SMS_REVIEW_REQUEST",
                    "TYPE_WEBCHAT",
                    "TYPE_SMS_NO_SHOW_REQUEST",
                    "TYPE_CAMPAIGN_SMS",
                    "TYPE_CAMPAIGN_CALL",
                    "TYPE_CAMPAIGN_EMAIL",
                    "TYPE_CAMPAIGN_VOICEMAIL",
                    "TYPE_FACEBOOK",
                    "TYPE_CAMPAIGN_FACEBOOK",
                    "TYPE_CAMPAIGN_MANUAL_CALL",
                    "TYPE_CAMPAIGN_MANUAL_SMS",
                    "TYPE_GMB",
                    "TYPE_CAMPAIGN_GMB",
                    "TYPE_REVIEW",
                    "TYPE_INSTAGRAM",
                    "TYPE_WHATSAPP",
                    "TYPE_CUSTOM_SMS",
                    "TYPE_CUSTOM_EMAIL",
                    "TYPE_CUSTOM_PROVIDER_SMS",
                    "TYPE_CUSTOM_PROVIDER_EMAIL",
                    "TYPE_IVR_CALL",
                    "TYPE_ACTIVITY_CONTACT",
                    "TYPE_ACTIVITY_INVOICE",
                    "TYPE_ACTIVITY_PAYMENT",
                    "TYPE_ACTIVITY_OPPORTUNITY",
                    "TYPE_LIVE_CHAT",
                    "TYPE_LIVE_CHAT_INFO_MESSAGE",
                    "TYPE_ACTIVITY_APPOINTMENT",
                    "TYPE_FACEBOOK_COMMENT",
                    "TYPE_INSTAGRAM_COMMENT",
                    "TYPE_CUSTOM_CALL",
                    "TYPE_INTERNAL_COMMENT",
                    "TYPE_ACTIVITY_EMPLOYEE_ACTION_LOG"
                  ]
                },
                "locationId": {
                  "type": "string",
                  "example": "ve9EPM428h8vShlRW1KT"
                },
                "contactId": {
                  "type": "string",
                  "example": "ve9EPM428h8vShlRW1KT"
                },
                "conversationId": {
                  "type": "string",
                  "example": "ve9EPM428h8vShlRW1KT"
                },
                "dateAdded": {
                  "type": "string",
                  "example": "2024-03-27T18:13:49.000Z"
                },
                "body": {
                  "type": "string",
                  "example": "Hi there"
                },
                "direction": {
                  "type": "string",
                  "enum": [
                    "inbound",
                    "outbound"
                  ]
                },
                "status": {
                  "type": "string",
                  "enum": [
                    "connected",
                    "delivered",
                    "failed",
                    "opened",
                    "pending",
                    "read",
                    "scheduled",
                    "sent",
                    "undelivered",
                    "clicked",
                    "opt_out"
                  ]
                },
                "contentType": {
                  "type": "string",
                  "example": "text/plain"
                },
                "attachments": {
                  "description": "An array of attachment URLs. Attachments will be empty for Call and Voicemails, type 1 and 10. Please use get call recording API to fetch call recording and voicemails.",
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "meta": {
                  "type": "object",
                  "properties": {
                    "callDuration": {
                      "type": "string",
                      "description": "Call duration in seconds",
                      "example": 120
                    },
                    "callStatus": {
                      "type": "string",
                      "description": "Call status - can be pending, completed, answered, busy, no-answer, failed, canceled, or voicemail",
                      "example": "completed",
                      "enum": [
                        "pending",
                        "completed",
                        "answered",
                        "busy",
                        "no-answer",
                        "failed",
                        "canceled",
                        "voicemail"
                      ]
                    },
                    "email": {
                      "type": "object",
                      "description": "meta will contain email, for message type 3 (email). messageIds is list of all email message ids under the message thread",
                      "example": {
                        "email": {
                          "messageIds": [
                            "ve9EPM428kjkvShlRW1KT",
                            "ve9EPs1028kjkvShlRW1KT"
                          ]
                        }
                      }
                    },
                    "ig": {
                      "type": "object",
                      "description": "meta will contain ig, for message type 18 (instagram).",
                      "example": {
                        "ig": {
                          "page_id": "1234567890",
                          "page_name": "Instagram Page"
                        }
                      }
                    },
                    "fb": {
                      "type": "object",
                      "description": "meta will contain fb, for message type 11 (facebook).",
                      "example": {
                        "fb": {
                          "page_id": "1234567890",
                          "page_name": "Facebook Page"
                        }
                      }
                    }
                  },
                  "title": "MessageMeta"
                },
                "source": {
                  "type": "string",
                  "enum": [
                    "workflow",
                    "bulk_actions",
                    "campaign",
                    "api",
                    "app"
                  ],
                  "description": "Message source"
                },
                "userId": {
                  "type": "string",
                  "description": "User Id",
                  "example": "ve9EPM428kjkvShlRW1KT"
                },
                "conversationProviderId": {
                  "type": "string",
                  "description": "Conversation Provider Id",
                  "example": "ve9EPM428kjkvShlRW1KT"
                },
                "chatWidgetId": {
                  "type": "string",
                  "description": "Chat Widget Id",
                  "example": "67b0cc8cf14b19d85ace7s35"
                },
                "from": {
                  "type": "string",
                  "description": "Sender identifier (phone/name). Not returned for email types.",
                  "example": "+14155551234"
                },
                "to": {
                  "type": "string",
                  "description": "Recipient identifier (phone/name). Not returned for email types.",
                  "example": "+14155555678"
                },
                "error": {
                  "type": "string",
                  "description": "Error message if message delivery failed"
                }
              },
              "required": [
                "id",
                "type",
                "messageType",
                "locationId",
                "contactId",
                "conversationId",
                "dateAdded",
                "direction",
                "contentType"
              ],
              "title": "GetMessageResponseDto"
            }
          },
          "nextCursor": {
            "type": "string",
            "description": "Cursor for fetching next page. Null if no more results.",
            "example": "a748514c-f49e-4fa8-9954-b53afc78d81d"
          },
          "total": {
            "type": "number",
            "description": "Total number of messages matching the query",
            "example": 1234
          }
        },
        "required": [
          "messages",
          "total"
        ],
        "title": "ExportMessagesResponseDto"
      },
      "BadRequestDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 400
          },
          "message": {
            "type": "string",
            "example": "Bad Request"
          }
        },
        "title": "BadRequestDTO"
      },
      "UnauthorizedDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 401
          },
          "message": {
            "type": "string",
            "example": "Invalid token: access token is invalid"
          },
          "error": {
            "type": "string",
            "example": "Unauthorized"
          }
        },
        "title": "UnauthorizedDTO"
      }
    }
  }
}
```

## Code Examples

**cURL**

```bash
curl -L -X GET 'https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>' \
  -H 'Authorization: Bearer <TOKEN>' \
  -H 'Version: v3'
```

**Node.js**

```javascript
const options = {
  method: 'GET',
  headers: {
    Authorization: 'Bearer <TOKEN>',
    Version: 'v3'
  }
};

try {
  const response = await fetch('https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>', options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

**Python**

```python
import requests

url = "https://services.leadconnectorhq.com/conversations/messages/export"

headers = {
    "Authorization": "Bearer <TOKEN>",
    "Version": "v3"
}

params = {
    "locationId": "<LOCATIONID>"
}

response = requests.get(url, params=params, headers=headers)
print(response.json())
```

**PHP**

```php
<?php
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => 'https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer <TOKEN>',
    'Version: v3',
  ],
]);

$response = curl_exec($curl);
curl_close($curl);
echo $response;
```

**Java**

```java
HttpResponse<String> response = Unirest
  .get("https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>")
  .header("Authorization", "Bearer <TOKEN>")
  .header("Version", "v3")
  .asString();
```

**Go**

```go
package main

import (
  "fmt"
  "net/http"
  "io"
)

func main() {
  url := "https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>"
  req, _ := http.NewRequest("GET", url, nil)
  req.Header.Add("Authorization", "Bearer <TOKEN>")
  req.Header.Add("Version", "v3")

  res, _ := http.DefaultClient.Do(req)
  defer res.Body.Close()
  body, _ := io.ReadAll(res.Body)
  fmt.Println(string(body))
}
```

**Ruby**

```ruby
require 'uri'
require 'net/http'

url = URI("https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Get.new(url)
request["Authorization"] = "Bearer <TOKEN>"
request["Version"] = "v3"

response = http.request(request)
puts response.read_body
```

**PowerShell**

```powershell
$headers = @{
  "Authorization" = "Bearer <TOKEN>"
  "Version" = "v3"
}

$response = Invoke-RestMethod -Uri 'https://services.leadconnectorhq.com/conversations/messages/export?locationId=<LOCATIONID>' -Method 'GET' -Headers $headers
$response | ConvertTo-Json
```
