# Count Contacts

POST https://services.leadconnectorhq.com/contacts/search/count

Count the contacts that match a search, without returning them. Takes the same `query` and `filters` as [Search Contacts](./search-contacts-advanced). Large counts can take up to 35 seconds. If the count does not finish in time, the response is `{ "count": null }`; retry the request.

## Authentication

Send `Authorization: Bearer <token>` using:

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

## Parameters

### Header Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `Version` | string | Yes | API Version. Send `v3`. |

## OpenAPI Specification

```json
{
  "openapi": "3.0.0",
  "info": {
    "title": "LeadConnector API",
    "version": "v3"
  },
  "servers": [
    {
      "url": "https://services.leadconnectorhq.com"
    }
  ],
  "paths": {
    "/contacts/search/count": {
      "post": {
        "summary": "Count Contacts",
        "operationId": "countContacts",
        "description": "Count the contacts that match a search, without returning them. Takes the same `query` and `filters` as [Search Contacts](./search-contacts-advanced). Large counts can take up to 35 seconds. If the count does not finish in time, the response is `{ \"count\": null }`; retry the request.",
        "parameters": [
          {
            "name": "Version",
            "in": "header",
            "description": "API Version. Send `v3`.",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "v3"
              ]
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CountContactsDto"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SearchCountSuccessResponseDto"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BadRequestDTO"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnauthorizedDTO"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden - the token does not have access to this location",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ForbiddenDTO"
                }
              }
            }
          },
          "422": {
            "description": "Unprocessable Entity - locationId is missing",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnprocessableDTO"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearer": [
              "contacts.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"
      },
      "Agency-Access-Only": {
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "name": "Authorization",
        "in": "header",
        "description": "Use the Access Token generated with user type as Agency.",
        "type": "http"
      }
    },
    "schemas": {
      "CountContactsDto": {
        "type": "object",
        "required": [
          "locationId"
        ],
        "properties": {
          "locationId": {
            "type": "string",
            "description": "Location Id",
            "example": "ve9EPM428h8vShlRW1KT"
          },
          "query": {
            "type": "string",
            "description": "Free-text search, the same as `query` on Search Contacts",
            "example": "john"
          },
          "filters": {
            "type": "array",
            "description": "Filters, or groups of filters, the same as `filters` on Search Contacts. Top-level items are combined with AND.",
            "items": {
              "type": "object",
              "properties": {
                "field": {
                  "type": "string",
                  "description": "Field to filter on (filter item)",
                  "example": "tags"
                },
                "operator": {
                  "type": "string",
                  "description": "Operator (filter item), e.g. `eq`, `not_eq`, `contains`, `not_contains`, `exists`, `not_exists`, `range`, `gt`, `gte`, `lt`, `lte`. See the Filters reference for the operators each field supports.",
                  "example": "eq"
                },
                "value": {
                  "description": "Value to compare with (filter item). Not sent for `exists` / `not_exists`; `{ gte, lte, gt, lt }` for `range`.",
                  "oneOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "number"
                    },
                    {
                      "type": "boolean"
                    },
                    {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    {
                      "type": "object"
                    }
                  ],
                  "example": "vip"
                },
                "group": {
                  "type": "string",
                  "enum": [
                    "AND",
                    "OR"
                  ],
                  "description": "How the nested filters are combined (group item)",
                  "example": "OR"
                },
                "filters": {
                  "type": "array",
                  "description": "Nested filters or groups, in the same form (group item)",
                  "items": {
                    "type": "object"
                  },
                  "example": [
                    {
                      "field": "type",
                      "operator": "eq",
                      "value": "lead"
                    }
                  ]
                }
              }
            },
            "example": [
              {
                "field": "tags",
                "operator": "eq",
                "value": "vip"
              },
              {
                "group": "OR",
                "filters": [
                  {
                    "field": "type",
                    "operator": "eq",
                    "value": "lead"
                  },
                  {
                    "field": "type",
                    "operator": "eq",
                    "value": "customer"
                  }
                ]
              }
            ]
          }
        }
      },
      "SearchCountSuccessResponseDto": {
        "type": "object",
        "properties": {
          "count": {
            "type": "number",
            "description": "Number of contacts that match the request. `null` when the count did not finish in time; retry.",
            "nullable": true,
            "example": 120
          }
        },
        "required": [
          "count"
        ],
        "title": "SearchCountSuccessResponseDto"
      },
      "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"
      },
      "ForbiddenDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 403
          },
          "message": {
            "type": "string",
            "example": "You do not have permission to access this resource"
          },
          "error": {
            "type": "string",
            "example": "Forbidden"
          }
        },
        "title": "ForbiddenDTO"
      },
      "UnprocessableDTO": {
        "type": "object",
        "properties": {
          "statusCode": {
            "type": "number",
            "example": 422
          },
          "message": {
            "example": [
              "Unprocessable Entity"
            ],
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "error": {
            "type": "string",
            "example": "Unprocessable Entity"
          }
        },
        "title": "UnprocessableDTO"
      }
    }
  }
}
```

## Code Examples

**cURL**

```bash
curl -L -X POST 'https://services.leadconnectorhq.com/contacts/search/count' \
  -H 'Authorization: Bearer <TOKEN>' \
  -H 'Version: v3' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "locationId": "ve9EPM428h8vShlRW1KT",
  "query": "john",
  "filters": [
    {
      "field": "tags",
      "operator": "eq",
      "value": "vip"
    },
    {
      "group": "OR",
      "filters": [
        {
          "field": "type",
          "operator": "eq",
          "value": "lead"
        },
        {
          "field": "type",
          "operator": "eq",
          "value": "customer"
        }
      ]
    }
  ]
}'
```

**Node.js**

```javascript
const options = {
  method: 'POST',
  headers: {
    Authorization: 'Bearer <TOKEN>',
    Version: 'v3',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
      "locationId": "ve9EPM428h8vShlRW1KT",
      "query": "john",
      "filters": [
        {
          "field": "tags",
          "operator": "eq",
          "value": "vip"
        },
        {
          "group": "OR",
          "filters": [
            {
              "field": "type",
              "operator": "eq",
              "value": "lead"
            },
            {
              "field": "type",
              "operator": "eq",
              "value": "customer"
            }
          ]
        }
      ]
  })
};

try {
  const response = await fetch('https://services.leadconnectorhq.com/contacts/search/count', options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

**Python**

```python
import requests

url = "https://services.leadconnectorhq.com/contacts/search/count"

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

payload = {
    "locationId": "ve9EPM428h8vShlRW1KT",
    "query": "john",
    "filters": [
        {
            "field": "tags",
            "operator": "eq",
            "value": "vip"
        },
        {
            "group": "OR",
            "filters": [
                {
                    "field": "type",
                    "operator": "eq",
                    "value": "lead"
                },
                {
                    "field": "type",
                    "operator": "eq",
                    "value": "customer"
                }
            ]
        }
    ]
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
```

**PHP**

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

curl_setopt_array($curl, [
  CURLOPT_URL => 'https://services.leadconnectorhq.com/contacts/search/count',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer <TOKEN>',
    'Version: v3',
    'Content-Type: application/json',
  ],
  CURLOPT_POSTFIELDS => '{
  "locationId": "ve9EPM428h8vShlRW1KT",
  "query": "john",
  "filters": [
    {
      "field": "tags",
      "operator": "eq",
      "value": "vip"
    },
    {
      "group": "OR",
      "filters": [
        {
          "field": "type",
          "operator": "eq",
          "value": "lead"
        },
        {
          "field": "type",
          "operator": "eq",
          "value": "customer"
        }
      ]
    }
  ]
}',
]);

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

**Java**

```java
HttpResponse<String> response = Unirest
  .post("https://services.leadconnectorhq.com/contacts/search/count")
  .header("Authorization", "Bearer <TOKEN>")
  .header("Version", "v3")
  .header("Content-Type", "application/json")
  .body("{\"locationId\":\"ve9EPM428h8vShlRW1KT\",\"query\":\"john\",\"filters\":[{\"field\":\"tags\",\"operator\":\"eq\",\"value\":\"vip\"},{\"group\":\"OR\",\"filters\":[{\"field\":\"type\",\"operator\":\"eq\",\"value\":\"lead\"},{\"field\":\"type\",\"operator\":\"eq\",\"value\":\"customer\"}]}]}")
  .asString();
```

**Go**

```go
package main

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

func main() {
  url := "https://services.leadconnectorhq.com/contacts/search/count"
  payload := strings.NewReader(`{
  "locationId": "ve9EPM428h8vShlRW1KT",
  "query": "john",
  "filters": [
    {
      "field": "tags",
      "operator": "eq",
      "value": "vip"
    },
    {
      "group": "OR",
      "filters": [
        {
          "field": "type",
          "operator": "eq",
          "value": "lead"
        },
        {
          "field": "type",
          "operator": "eq",
          "value": "customer"
        }
      ]
    }
  ]
}`)
  req, _ := http.NewRequest("POST", url, payload)
  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/contacts/search/count")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer <TOKEN>"
request["Version"] = "v3"
request["Content-Type"] = "application/json"
request.body = '{
  "locationId": "ve9EPM428h8vShlRW1KT",
  "query": "john",
  "filters": [
    {
      "field": "tags",
      "operator": "eq",
      "value": "vip"
    },
    {
      "group": "OR",
      "filters": [
        {
          "field": "type",
          "operator": "eq",
          "value": "lead"
        },
        {
          "field": "type",
          "operator": "eq",
          "value": "customer"
        }
      ]
    }
  ]
}'

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

**PowerShell**

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

$body = '{
  "locationId": "ve9EPM428h8vShlRW1KT",
  "query": "john",
  "filters": [
    {
      "field": "tags",
      "operator": "eq",
      "value": "vip"
    },
    {
      "group": "OR",
      "filters": [
        {
          "field": "type",
          "operator": "eq",
          "value": "lead"
        },
        {
          "field": "type",
          "operator": "eq",
          "value": "customer"
        }
      ]
    }
  ]
}'

$response = Invoke-RestMethod -Uri 'https://services.leadconnectorhq.com/contacts/search/count' -Method 'POST' -Headers $headers -Body $body -ContentType 'application/json'
$response | ConvertTo-Json
```
