# Sold listings and sold windows

Source: https://help.ruuster.com/idx/guide-sold

> Search recently sold homes on the boards that show sold data, within each board's sold window, and build sold pages that stay correct when a board's rules differ.

Some boards let you show sold listings for a period after the sale, and some don't show sold listings
at all. The API applies each board's rule, so you never show a sale a board doesn't allow.

## Check first

`GET /v1/meta` answers `data.capabilities.sold`: one row per board with `mlsId`, `allowed` and
`windowDays` (how many days after the sale a listing may be shown). Build a sold page only when at
least one of your boards allows sold data; otherwise link to the area's listings for sale instead.

To name those boards, match each row's `mlsId` with the `id` of a board in `data.mls` from the same
answer, which holds its `name`:

```ts
import { createClient } from "@ruuster/idx";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });
const m = await idx.meta();
if (m.ok) {
  const names = new Map(m.data.mls.map((b) => [b.id, b.name]));
  for (const row of m.data.capabilities.sold) {
    console.log(names.get(row.mlsId) ?? row.mlsId, row.allowed ? `${row.windowDays} days` : "no sold data");
  }
}
```

A sold page needs no list of which boards cover which area. A search with `status=closed` alone
answers from those of your boards that show sold data. When none of your boards does, it answers 422
`sold_not_permitted`; when the area's own boards show no sold data, it answers no listings. The
Next.js example answers the first with a 404 and shows its empty-page sentence for the second.

## Searching sold homes

Send `status=closed` with `soldPeriod`, which is how far back to look: `7d`, `1m`, `3m`, `6m` or `12m`.
Sort with `close_date_desc` for the most recent sales first.

cURL:

```bash
curl -s -G "https://idx.ruuster.com/v1/listings" \
  --data-urlencode zip=30308 \
  --data-urlencode status=closed \
  --data-urlencode soldPeriod=6m \
  --data-urlencode sort=close_date_desc \
  --data-urlencode limit=24 \
  -H "Authorization: Bearer $IDX_SECRET_KEY"
```

TypeScript:

```typescript
import { createClient } from "@ruuster/idx";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });

const r = await idx.search({
  zip: "30308",
  status: "closed",
  soldPeriod: "6m",
  sort: "close_date_desc",
  limit: 24,
});
if (!r.ok) throw new Error(`${r.error.code}: ${r.error.message}`);
console.log(r.data);
```

Python:

```python
import os

import requests

res = requests.get(
    "https://idx.ruuster.com/v1/listings",
    params={
        "zip": "30308",
        "status": "closed",
        "soldPeriod": "6m",
        "sort": "close_date_desc",
        "limit": "24",
    },
    headers={"Authorization": "Bearer " + os.environ["IDX_SECRET_KEY"]},
    timeout=30,
)
body = res.json()
if not body["ok"]:
    raise RuntimeError(body["error"]["code"] + ": " + body["error"]["message"])
print(body["data"])
```

PHP:

```php
<?php

$query = http_build_query([
    'zip' => '30308',
    'status' => 'closed',
    'soldPeriod' => '6m',
    'sort' => 'close_date_desc',
    'limit' => '24',
], '', '&', PHP_QUERY_RFC3986);
$ch = curl_init('https://idx.ruuster.com/v1/listings' . '?' . $query);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('IDX_SECRET_KEY')],
    CURLOPT_TIMEOUT => 30,
]);
$body = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!$body['ok']) {
    throw new RuntimeException($body['error']['code'] . ': ' . $body['error']['message']);
}
print_r($body['data']);
```

- `soldPeriod` never reaches further back than a board's own window: a board that shows 90 days of
  sales answers 90 days even when you ask for 12 months.
- Show `closePrice` as a closed listing's sold price (the recipe components do). `closeDate` is the
  sale date: format it with `fmt.date`, which keeps a date sent as midnight UTC on its own day.
  `listPrice` is the original asking price when the board sends it, and `price` is normally the sold
  price as well.
- `status=closed` without `soldPeriod` answers 400 `sold_period_required`.
- A sold search covers only the boards that show sold data. When a search mixes `closed` with other
  statuses and its boards disagree, it answers 422 `sold_not_permitted`: narrow it with `filterByMls`
  to the boards that show sold data, or drop `closed`.

## Sold listing pages

A sold listing's own page stays live and shows "Sold" until its board's sold window ends. After that,
`GET /v1/listings/{listingId}` answers 410 `listing_removed`, and the page answers 410 too
([Off-market and removed listings](https://help.ruuster.com/idx/guide-removed-listings)).

## Writing sold pages

- Title a sold page with what it shows, such as "Recently sold homes in Midtown". The API's
  `head.title` does this for you when the search is sold only.
- Say these are recent sales in the area, not your brokerage's sales, unless you filtered to your own
  listings.
- Print the date the data was last updated and every board's disclaimer, as on any results page.
- [Sold and status rules](https://help.ruuster.com/idx/compliance-sold-status) has the rules in full.

Market numbers for sales (median sold price, days on market) follow the same rule:
[Market stats by area](https://help.ruuster.com/idx/guide-market-stats).
