# Gmail Search (`gmail.search`)

Searches the connected Gmail mailbox with Gmail search syntax and returns sender, subject, date and snippet per message.

Docs: https://primaryapi.com/docs/api-reference/gmail/search

- Endpoint: `POST https://api.primaryapi.com/v1/run/gmail.search`
- Category: Knowledge & Data · Platform: `gmail` · Kind: search · Status: live
- Price: 1 credit per call. Every call costs the same, including calls that were retried automatically.
- Billing unit: per successful call
- Reliability: routed to a healthy upstream and retried automatically when an attempt fails; live 30-day status: `GET https://api.primaryapi.com/v1/status/apis?sku=gmail.search`
- Execution: synchronous (answers 202 only if it outlives the wait window)

## Input

| Field | Type | Required | Default | Constraints | Description |
|---|---|---|---|---|---|
| `query` | string | yes |  | max 500 chars | Gmail search query, the same syntax as the Gmail search box, for example "from:alice@example.com newer_than:7d". |
| `labelIds` | string[] | no |  | max 10 items; items max 100 chars | Only messages that carry ALL of these label ids, for example ["INBOX", "UNREAD"]. |
| `includeSpamTrash` | boolean | no | false |  | Also search Spam and Trash (default false). |
| `limit` | integer | no | 10 | 1–25 | Maximum number of results to return (1-25, default 10). |
| `cursor` | string | no |  |  | Opaque pagination cursor: pass the previous response's nextCursor. |
| `connection` | string | no |  | max 200 chars | Which connected account to use when the workspace has several for this provider: the customId you gave it (exact), its connection id ("con_…"), its handle (login, email or @handle) or its name. Optional; prefer the customId or id, which never match two accounts. |

Unknown fields are rejected with `400 invalid_input`. The routing controls `preferLatencyUnderMs` and `maxCredits` may be sent in the same JSON body.

## Output

When `output.found` is `true`, `output.data` has this shape:

| Field | Type | Description |
|---|---|---|
| `messages` | object[] |  |
| `messages[].id` | string | Message id (read it with gmail.message) |
| `messages[].threadId` | string \| null |  |
| `messages[].from` | string \| null |  |
| `messages[].to` | string \| null |  |
| `messages[].subject` | string \| null |  |
| `messages[].date` | string \| null | The Date header as sent |
| `messages[].receivedAt` | string \| null | When Gmail received it (ISO-8601) |
| `messages[].snippet` | string \| null | Short part of the message text |
| `messages[].labelIds` | string[] |  |
| `messages[].sizeEstimate` | integer \| null | Bytes |
| `resultSizeEstimate` | integer \| null | Gmail’s estimate of all matching messages |
| `nextCursor` | string \| null |  |

When the thing you asked about does not exist (or a search matches nothing) the call still succeeds: `output.found` is `false`, `output.data` is `null` and `output.reason` says why (`not_found`, `private`, `no_results`, …). Misses are free unless the notes below say misses are billed.

Pagination: pass the previous response’s `nextCursor` as `cursor` to get the next page; `nextCursor: null` means there are no more results.

## Example request

```bash
curl -sS -X POST "https://api.primaryapi.com/v1/run/gmail.search" \
  -H "Authorization: Bearer $PRIMARYAPI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query":"subject:report newer_than:30d","limit":2}'
```

## Example response

_Values are placeholders that follow the output schema; `credits` is what this call would cost at the usual price._

```json
{
  "output": {
    "found": true,
    "data": {
      "messages": [
        {
          "id": "18c3a1f0e2b4d5f6",
          "threadId": "18c3a1f0e2b4d5f6",
          "from": "Alice Example <alice@example.com>",
          "to": "me@example.com",
          "subject": "October report",
          "date": "Mon, 02 Nov 2026 09:00:00 +0000",
          "receivedAt": "2026-11-02T09:00:00.000Z",
          "snippet": "Here are the numbers for October.",
          "labelIds": [
            "INBOX",
            "UNREAD"
          ],
          "sizeEstimate": 5120
        }
      ],
      "resultSizeEstimate": 2,
      "nextCursor": "eyJ0IjoiMDg3NjU0MzIxMDk4NzY1NDMyMTAifQ"
    }
  },
  "provider": "PrimaryAPI",
  "credits": 1,
  "items": 1,
  "requestId": "req_01JBXK6Y2N9V8T4R3Q2P1M0A7Z",
  "nextCursor": null
}
```

## Pricing and reliability

- Usual price: 1 credit per call
- Price ceiling: the usual price, even when a call is retried. A quote’s `maxCredits` is the most a given call can cost; the `maxCredits` routing control caps it per call
- Live 30-day status, success rate, uptime and latency: `GET https://api.primaryapi.com/v1/status/apis?sku=gmail.search`

## Errors to handle

`invalid_input`, `unauthorized`, `insufficient_balance`, `key_limit_reached`, `trial_cap_reached`, `forbidden`, `not_found`, `idempotency_conflict`, `rate_limited`, `all_sources_failed`, `service_unavailable`, `timeout`. See https://primaryapi.com/docs/errors.

## Notes

Runs as your connected Google account (read-only Gmail access). Message bodies are not included: read one with gmail.message. Run the call again to refresh them.

