> For a complete page index, fetch https://developer.close.com/llms.txt

# Advanced Filtering

The Advanced Filtering API finds objects matching a structured query sent to
[Search objects with filters](/api/resources/advanced-filtering/search)
(`POST /api/v1/data/search/`). It's the API behind the filters on the Leads,
Contacts and Opportunities pages in Close.

This page is a short tutorial. The
[endpoint reference](/api/resources/advanced-filtering/search) explains how to
build queries and documents every parameter, query type and condition, as well
as dates, results and pagination.
[Advanced Filtering Fields](/api/resources/advanced-filtering-fields) lists the
fields you can filter by for each object type.

### Tutorial

Let's find all Contacts with "CEO" in their title. The request body looks like
this:

```json
{
  "query": {
    "type": "and",
    "queries": [
      {
        "type": "object_type",
        "object_type": "contact"
      },
      {
        "type": "field_condition",
        "field": {
          "type": "regular_field",
          "object_type": "contact",
          "field_name": "title"
        },
        "condition": {
          "type": "text",
          "mode": "full_words",
          "value": "CEO"
        }
      }
    ]
  },
  "_fields": {
    "contact": ["id", "name", "title"]
  }
}
```

The `and` query combines an `object_type` query, which asks for Contacts, with
a `field_condition` query, which asks for Contacts whose `title` contains the
word "CEO". `_fields` asks for the Contacts' `id`, `name` and `title`.

Here's an example response:

```json
{
  "data": [
    {
      "__object_type": "contact",
      "id": "cont_PtENYt1P6sRWkcnqoGnIj6hcZammGPdVbTXJWqdTvpV",
      "name": "Lucille Bluth",
      "title": "CEO"
    },
    {
      "__object_type": "contact",
      "id": "cont_HjTcJFiNli2AKf5fioygQDVSpA9nJILI9SkKv3nBk0A",
      "name": "Steli Efti",
      "title": "CEO & Co-Founder"
    }
  ],
  "cursor": null
}
```

### Filtering by related objects and custom fields

To filter by fields of another object, use a `has_related` query. For example,
here are CEOs with at least one email address, at Leads whose Industry custom
field is "Software as a service":

```json
{
  "query": {
    "type": "and",
    "queries": [
      {
        "type": "object_type",
        "object_type": "contact"
      },
      {
        "type": "field_condition",
        "field": {
          "type": "regular_field",
          "object_type": "contact",
          "field_name": "emails_count"
        },
        "condition": {
          "type": "number_range",
          "gte": 1
        }
      },
      {
        "type": "field_condition",
        "field": {
          "type": "regular_field",
          "object_type": "contact",
          "field_name": "title"
        },
        "condition": {
          "type": "text",
          "mode": "full_words",
          "value": "CEO"
        }
      },
      {
        "type": "has_related",
        "this_object_type": "contact",
        "related_object_type": "lead",
        "related_query": {
          "type": "field_condition",
          "field": {
            "type": "custom_field",
            "custom_field_id": "cf_B57sofuEB7OBneH86SJctTr7hfiAiQcgkGRbAQfha0Z"
          },
          "condition": {
            "type": "term",
            "values": ["Software as a service"]
          }
        }
      }
    ]
  }
}
```

This query introduces a few more concepts:

- A `number_range` condition for a number field. `gte: 1` means greater than or
  equal to 1.
- A `custom_field`, which references a custom field by its ID. Find custom
  field IDs with the custom field endpoints, e.g.
  [List Lead Custom Fields](https://developer.close.com/api/resources/custom-fields/custom-fields-lead/list).
- A `term` condition, which matches one of the choices of a choice custom field.
- A `has_related` query, which matches Contacts whose Lead matches the
  `related_query`. Fields in the `related_query` belong to the related object
  type. Use it the other way around too, e.g. to find Leads with a Contact who
  has a title, or Leads with a call in the last week (`activity.call`).

### Visual Query Builder

Instead of writing the JSON query by hand, you can build your query visually on
the [Leads Page](https://app.close.com/leads/) or
[Contacts page](https://app.close.com/contacts/) in Close, and then copy the
JSON query by clicking the triple-dot menu on the top right and selecting
"Copy Filters".

![Advanced Filtering](/_fern-files/close.docs.buildwithfern.com/1e53f0c1bf29f5de058f69b9a2d0036f84a5f9eea01670427af10c905befa90d/docs/pages/resources/assets/copy_filters.gif)

### Query types

| Type | Matches |
| --- | --- |
| `and`, `or` | Objects matching all, or at least one, of the given `queries`. |
| `object_type` | Objects of a type, e.g. `lead`, `contact` or `activity.call`. |
| `field_condition` | Objects whose field value satisfies a condition. |
| `has_related` | Objects with a related object matching a query. |
| `text` | Objects containing the given text in any text field, like the search bar in Close. |
| `id` | A single object by its ID. |
| `saved_search`, `any_of_saved_search` | Objects matching one or more Smart Views. |
| `in_status` | Leads or Opportunities that were in any of the given statuses in a period. |
| `match_all` | Every object. |

### Field conditions

Which condition to use depends on the field's value type, which
[Advanced Filtering Fields](/api/resources/advanced-filtering-fields) lists for
every field.

| Value type | Conditions |
| --- | --- |
| text, email address, phone number, URL | `text`, `multiple_text` |
| one of a fixed set of values, choice custom fields | `term` |
| IDs of other objects (users, statuses, ...) | `reference`, and `current_user` for the requesting user |
| numbers | `number_range`, `number`, `multiple_numbers` |
| durations | `duration_range` |
| dates and datetimes | `moment_range`, `predefined_relative_date_range`, `fixed_date_range`, `exact_moment` |
| booleans | `boolean` |
| timezones | `timezone_name`, `local_time` |
| any field | `exists` |