# Searching with TQL

> Find tickets with the Ticket Query Language: fields, operators, dates, sorting and examples

URL: https://docs.lemuai.com/tickets-api/tql

TQL (Ticket Query Language) is how you search tickets. It is the same language the LemuTickets app uses for filters, queues and saved views, so a filter you build in the app works in the API too.

Send a query as the `q` parameter of [List and search tickets](https://docs.lemuai.com/tickets-api/tickets/list):
```bash
curl -G https://api.tickets.lemuai.com/v1/tickets \
  -H "Authorization: Bearer ltk_live_your_api_key" \
  --data-urlencode 'q=project = SUP AND status_category != done AND priority in (p1, p2) ORDER BY sla.next_due ASC'
```

## How a query is built

A query is one or more conditions joined with `AND` and `OR`, followed by an optional `ORDER BY`:
```
field operator value [AND|OR field operator value ...] [ORDER BY field [ASC|DESC], ...]
```

- `AND` binds tighter than `OR`. Use parentheses to group: `(priority = p1 OR sla.breached = true) AND project = SUP`.
- Two conditions side by side without a keyword are joined with `AND`.
- `NOT` in front of a condition or group negates it: `NOT labels in (spam)`.
- Keywords (`AND`, `OR`, `NOT`, `IN`, `IS`, `EMPTY`, `ORDER BY`, `ASC`, `DESC`) are not case sensitive. Field names are not case sensitive.
- Put values with spaces or special characters in double or single quotes: `status = "Waiting on customer"`.
- A quoted text or a word on its own is a full-text search of the title and description: `"refund not received"`. A ticket key on its own, like `SUP-1042`, finds that ticket.

## Operators

| Operator | Meaning | Example |
|---|---|---|
| `=` | Equals | `priority = p1` |
| `!=` | Does not equal (also matches tickets where the field is empty) | `status_category != done` |
| `>` `>=` `<` `<=` | Greater or less than (dates, numbers, priority) | `created >= -7d` |
| `~` | Contains (case insensitive) | `title ~ invoice` |
| `!~` | Does not contain | `title !~ test` |
| `IN (...)` | Any of the values | `priority in (p1, p2)` |
| `NOT IN (...)` | None of the values | `source not in (email, slack)` |
| `IS EMPTY` | Has no value | `assignee is empty` |
| `IS NOT EMPTY` | Has a value | `due is not empty` |

For `priority`, "greater" means more urgent: `priority >= p2` finds `p1` and `p2`.

## Fields

| Field | What it matches | Values |
|---|---|---|
| `project` | The project | Key (`SUP`), name or id |
| `key` | The ticket key | `SUP-1042` |
| `type` | The ticket type | Name (`Complaint`) or id |
| `status` | The exact status | Name (`"In progress"`) or id |
| `status_category` | The status group | `new`, `open`, `waiting`, `done` |
| `priority` | Priority | `p1`, `p2`, `p3`, `p4` |
| `severity` | Severity | Text |
| `source` | Where the ticket came from | `voice`, `whatsapp`, `web_chat`, `email`, `slack`, `teams`, `api`, `portal`, `web_form`, `manual`, `lemu_copilot`, `automation`, `journey` |
| `assignee` | The assigned person | `me()`, email, name or user id |
| `reporter` | Who created the ticket | `me()`, email, name or user id |
| `team` | The assigned team | Name or id |
| `requester` | The customer who raised it | Email, phone, name or requester id |
| `account` | The customer's account | Name or id |
| `account.tier` | The account's tier | For example `enterprise` |
| `requester.vip` | Requester is marked VIP | `true` or `false` |
| `labels` | Labels on the ticket | Label text. `labels in (refund, urgent)` matches tickets with any of them |
| `title` | The title | Text (use `~` to search inside it) |
| `description` | The description | Text |
| `text` | Full-text search of title and description | Text |
| `created` | When it was created | Date |
| `updated` | Last change | Date |
| `resolved` | When it was resolved | Date |
| `closed` | When it was closed | Date |
| `due` | The due date | Date |
| `first_response` | First public reply | Date |
| `status_changed` | Last status change | Date |
| `sla.next_due` | The next SLA deadline | Date |
| `sla.breached` | An SLA target was missed | `true` or `false` |
| `escalation_level` | How many times it was escalated | Number |
| `reopened` | How many times it was reopened | Number |
| `csat` | The customer's satisfaction score | Number |
| `time_spent` | Logged work, in minutes | Number |
| `restricted` | Visible only to some people | `true` or `false` |
| `parent` | The parent ticket | Ticket key |
| `watcher` | A user watching the ticket | User id |
| `external_id` | Your own id for the ticket | Text |
| `cf.<key>` | A custom field | Depends on the field. Numbers can use `>` and `<` |
| `ai.<key>` | A value the AI set during triage, such as `ai.category` | Text |

`status_category` is usually better than `status` in integrations: each project names its statuses differently, but every status belongs to one of the four categories.

## Dates

Date fields accept:

- **Relative times** from now: `-30m`, `-12h`, `-7d`, `-2w` for the past, `+1d` for the future. `created >= -7d` is "created in the last 7 days".
- **ISO dates and times**: `2026-11-01` or `2026-11-01T09:00:00+05:30`. Times without a zone are read as UTC.
- **Functions**: `now()`, `startOfDay()`, `endOfDay()`, `startOfWeek()` (Monday), `startOfMonth()`. These use UTC.

## Functions

| Function | Value |
|---|---|
| `me()` or `currentUser()` | The calling user. For an API key, the key's own user |
| `now()` | The current time |
| `startOfDay()`, `endOfDay()` | Start or end of today (UTC) |
| `startOfWeek()` | Monday 00:00 of this week (UTC) |
| `startOfMonth()` | The first of this month, 00:00 (UTC) |

## Sorting

Add `ORDER BY` at the end with one or more fields, each `ASC` (default) or `DESC`. Empty values sort last. Without `ORDER BY`, the most recently updated tickets come first.

You can sort by `created`, `updated`, `resolved`, `due`, `sla.next_due`, `key` (or `number`), `title`, `priority`, `status` (or `status_category`), `escalation_level`, `csat` and `status_changed`.

## Examples

| You want | Query |
|---|---|
| Open tickets in one project, most urgent first | `project = SUP AND status_category != done ORDER BY priority ASC, created ASC` |
| Breached or about to breach today | `sla.breached = true OR sla.next_due <= endOfDay()` |
| Unassigned high-priority tickets | `assignee is empty AND priority >= p2 AND status_category in (new, open)` |
| One customer's tickets this month | `requester = "priya@example.com" AND created >= startOfMonth()` |
| Enterprise accounts waiting on you | `account.tier = enterprise AND status_category = open` |
| Complaints from WhatsApp last week | `type = Complaint AND source = whatsapp AND created >= -1w` |
| A custom field value | `cf.order_id = "58213"` |
| Ticket synced from your ERP | `external_id = "ERP-CMP-58213"` |
| Free text | `"refund not received" AND project = SUP` |

## Checking a query

Send a query to `POST /v1/tql/validate` to check it before you save it in your system:
```bash
curl -X POST https://api.tickets.lemuai.com/v1/tql/validate \
  -H "Authorization: Bearer ltk_live_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{ "q": "project = SUP AND priorty = p1" }'
```
```json
{
  "valid": false,
  "message": "Unknown field priorty",
  "data": { "field": "priorty" },
  "fields": ["project", "key", "type", "status", "..."]
}
```

A valid query returns `"valid": true` and the same `fields` list. When a query in [List and search tickets](https://docs.lemuai.com/tickets-api/tickets/list) is invalid, the request fails with `422` and a `tql_*` error code.
