# Add a buyer and a search in one call

Source: https://help.ruuster.com/idx/alerts-one-call

> Create a buyer, their consent record, their agent and their first saved search with one POST /v1/buyers call that is safe to repeat, and know which saved-search criteria are accepted.

Most sites create a buyer at the moment a visitor saves a search. `POST /v1/buyers` does all of it
in one call: the buyer, the consent record, the agent and the first saved search.

## The call

```js
const res = await fetch("https://idx.ruuster.com/v1/buyers", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.IDX_SECRET_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    email: "jamie@example.com",
    firstName: "Jamie",
    lastName: "Lee",
    externalId: "fub-123",
    timeZone: "America/Chicago",
    consent: {
      text: "Email me new listings that match this search. I can unsubscribe at any time.",
      source: "https://www.example-homes.com/search",
      ip: request.headers.get("x-forwarded-for"),
    },
    agent: { name: "Dana Reyes", email: "dana@example-homes.com", externalId: "crm-7" },
    search: {
      name: "Travis Heights, 3 beds",
      criteria: { where: [whereFromSuggest], bedsMin: "3", priceMax: "750000" },
      frequency: "instant",
    },
  }),
});
const { data } = await res.json();
// data = { buyer, created, agent, search, searchCreated }
```

The answer is 201 when anything was created and 200 when everything already existed.

## Safe to repeat

We look the buyer up by `externalId` first, then by email (capital letters don't matter). An existing
buyer keeps their consent record and gets the other fields you sent. A search with the same criteria
for the same buyer answers the search already stored. An agent with the same `externalId` is reused.
So a form that is submitted twice, or a retry after a timeout, never makes a second buyer or a
second search.

## Consent

Every new buyer needs a consent record: the words the buyer agreed to (`text`), when (`at`, default
now), where (`source`) and optionally their `ip`. Store the exact words your form showed. A buyer who
unsubscribes can only be turned back on with a fresh consent record
(`POST /v1/buyers/{buyerId}/resubscribe`).

## Criteria

A saved search's `criteria` are the `GET /v1/listings` parameters as a JSON object: a location
(`where` objects from `/v1/suggest` passed unchanged, `zip`, `city` with `state`, `bbox`, `polygon`
or a saved `shape`) plus any filters. `where`, `type`, `subtype`, `status` and `keywords` may be
lists. We check them against your boards' filters when you save the search, with the same messages
a search would give, so a saved search never fails later.

A saved search can't carry `sort`, `page`, `limit`, `cursor`, `view`, `format` or `modifiedSince`:
they change how results are shown, not which listings match.

## Limits

A buyer can have up to 25 saved searches. You can lower that in your alert settings
(`maxSearchesPerBuyer`). The 26th answers `422 limit_reached`.

## Adding a search later

For a buyer you already have, use `POST /v1/saved-searches` with `buyerId` (or `buyerExternalId`)
and `criteria`. See [Alerts endpoints](/idx/reference-alerts).
