# Open houses this weekend

Source: https://help.ruuster.com/idx/recipe-open-houses

> An /open-houses page with every home in your market area that has an open house this weekend, rendered on your server and refreshed every 5 minutes.

A page at `/open-houses` with the homes in your market area that have an open house this weekend.
`openHousePeriod=this_weekend` asks for them; `today_or_tomorrow`, `next_7_days` and `any` are the
other windows. Each card shows its open house times in the listing's own time zone, and the page is
rendered on your server, so it is ready to share on Friday.

## What the visitor sees

<figure class="idx-shot"><img src="/idx-docs/screens/ours/listing-grid-1440.png" alt="Listing grid with the disclaimers under it, on a desktop (1440 px wide)" width="1440" height="900" loading="lazy" decoding="async"><figcaption>Homes with an open house this weekend.</figcaption></figure>

<figure class="idx-shot"><img src="/idx-docs/screens/ours/listing-grid-375.png" alt="Listing grid with the disclaimers under it, on a phone (375 px wide)" width="375" height="1403" loading="lazy" decoding="async"><figcaption>Homes with an open house this weekend.</figcaption></figure>

The markup is `static/html/listing-grid.html` in `@ruuster/idx-recipes`. The [HTML and CSS recipe](https://help.ruuster.com/idx/recipe-html-css) shows how to fill it and which stylesheet and script to load.

## What the page calls

| When | Request | Key | Cached |
|---|---|---|---|
| Each page view | `GET /v1/listings` with `openHousePeriod=this_weekend` and your market area | secret key, on your server | 5 minutes by Next.js |

cURL:

```bash
curl -s -G "https://idx.ruuster.com/v1/listings" \
  --data-urlencode 'where={"label":"Atlanta, GA","value":"Atlanta","source":"crm-system","data":{"state":"GA","city":"Atlanta"},"city":"Atlanta","state":"GA"}' \
  --data-urlencode status=active \
  --data-urlencode openHousePeriod=this_weekend \
  --data-urlencode limit=24 \
  --data-urlencode pageHref=/open-houses \
  -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({
  where: {
    label: "Atlanta, GA",
    value: "Atlanta",
    source: "crm-system",
    data: {
      state: "GA",
      city: "Atlanta",
    },
    city: "Atlanta",
    state: "GA",
  },
  status: "active",
  openHousePeriod: "this_weekend",
  limit: 24,
  pageHref: "/open-houses",
});
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={
        "where": "{\"label\":\"Atlanta, GA\",\"value\":\"Atlanta\",\"source\":\"crm-system\",\"data\":{\"state\":\"GA\",\"city\":\"Atlanta\"},\"city\":\"Atlanta\",\"state\":\"GA\"}",
        "status": "active",
        "openHousePeriod": "this_weekend",
        "limit": "24",
        "pageHref": "/open-houses",
    },
    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([
    'where' => '{"label":"Atlanta, GA","value":"Atlanta","source":"crm-system","data":{"state":"GA","city":"Atlanta"},"city":"Atlanta","state":"GA"}',
    'status' => 'active',
    'openHousePeriod' => 'this_weekend',
    'limit' => '24',
    'pageHref' => '/open-houses',
], '', '&', 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']);
```

## Steps

1. Add `lib/idx.ts`, `lib/site.ts` and `components/results.tsx` from the
   [recipes overview](https://help.ruuster.com/idx/recipes), and `lib/area-query.ts` and
   `lib/results.ts` from the [search page recipe](https://help.ruuster.com/idx/recipe-search-page).
2. In `content/areas.ts`, make your whole market (your city or county row from `/v1/suggest`) the
   last area: `MARKET_AREA` is that entry.
3. Create `app/open-houses/page.tsx` with the code below.
4. Open `http://localhost:3000/open-houses`.

## The code

<p class="code-title"><code>app/open-houses/page.tsx</code></p>

```tsx
// /open-houses: homes with an open house this weekend in your market area, rendered on the server.
// Each card shows its open house times in the listing's own time zone.
import { notFound } from "next/navigation";
import { Results } from "@/components/results";
import { MARKET_AREA } from "@/content/areas";
import { idx } from "@/lib/idx";
import { PAGE_SIZE, visitorQuery, type CardQuery } from "@/lib/area-query";
import { failureAction, resultsMetadata } from "@/lib/results";

export const revalidate = 300;

type Props = { searchParams: Record<string, string | string[] | undefined> };

function query(searchParams: Props["searchParams"]): CardQuery {
  return { ...visitorQuery(searchParams), where: MARKET_AREA.where, status: "active", openHousePeriod: "this_weekend", limit: PAGE_SIZE, pageHref: "/open-houses" };
}

export async function generateMetadata({ searchParams }: Props) {
  const r = await idx.search(query(searchParams));
  return r.ok ? resultsMetadata(r.data.head, `Open houses this weekend in ${MARKET_AREA.name}`) : {};
}

export default async function OpenHouses({ searchParams }: Props) {
  const r = await idx.search(query(searchParams));
  if (!r.ok) {
    if (failureAction(r) === "not_found") notFound();
    throw new Error(`Listings API ${r.status} ${r.error.code}`);
  }
  return (
    <main>
      <h1>{`Open houses this weekend in ${MARKET_AREA.name}`}</h1>
      <Results r={r} basePath="/open-houses" searchParams={searchParams} listName="Open houses this weekend" emptyText="No open houses are scheduled for this weekend yet." />
    </main>
  );
}
```

## Forms and your CRM

The page carries no forms. Each card leads to its listing page, where your tour form is
([Registration, tour and contact forms](https://help.ruuster.com/idx/recipe-forms)).

## Rules this page follows

**MUST**

- Show each open house in the listing's own time zone, as the cards do.
- Re-render at least every 5 minutes, so a canceled open house leaves the page quickly.
- Show every board's disclaimer under the list.

**NEVER**

- Add open houses by hand that the API did not answer.
- Describe a home by who it suits ([Fair housing](https://help.ruuster.com/idx/fair-housing)).

## Test it

```sh
curl -s http://localhost:3000/open-houses | grep -o "<h1>[^<]*</h1>"
```

The heading names your market area. View source and find each card's open house time. With no open
houses scheduled, the page says so instead of showing an empty grid.

## Build it with AI

```text
Read RUUSTER_IDX.md first and follow its MUST and NEVER rules. Ask me if it doesn't cover something.

Build an "Open houses this weekend" page at /open-houses that follows <https://help.ruuster.com/idx/recipe-open-houses>. Call GET /v1/listings with openHousePeriod=this_weekend and my market area's where object on the server with my secret key, and revalidate the page every 5 minutes. Show each open house time in the listing's own time zone, and keep the MLS disclaimers under the results.

Use these environment variables: IDX_SECRET_KEY (server only), SITE_ORIGIN and SITE_NAME. Never paste a secret key into a chat, and never put IDX_SECRET_KEY in browser code, a URL or a log. No key goes in any page: my pages call my own /api/idx route, made with createListingsRoute from @ruuster/idx/route, which adds my secret key on the server (on a site with no server, the widgets call the API with no key from my approved website address).

Fair housing: describe homes, places and amenities only. Never describe the people who live in an area or who a home or area suits, and never rank schools as a reason to buy.

When you are done, show me what the page says when no open houses are scheduled.
```

On a site without React, such as PHP, Python, Ruby on Rails, Go or plain HTML, [Build without React: HTML, CSS and JavaScript](https://help.ruuster.com/idx/recipe-html-css) gives you the same markup, the stylesheet and the script for every widget.
