Homepage search bar
A homepage search bar that suggests cities, neighborhoods, ZIP codes and schools as the visitor types, then hands off to your server-rendered search page.
A search bar for your homepage. As the visitor types, it suggests places (cities, neighborhoods, ZIP
codes, school areas and the places in your boards' states), and picking one goes to your
search page with that place's where object in
the URL. The search page then renders the results on your server. The bar runs in the browser and
asks your own listings route for suggestions, so the page carries no key.
What the visitor sees
<!--
Location search box.
From the React component SearchBox. Generated by npm run idx:recipes-static from @ruuster/idx-recipes. Do not edit it by hand.
Fill each value from:
form[action]: your search page's path; a picked place goes to {action}?where={the row's where as JSON}
form[data-rw-base-url]: your own route made with @ruuster/idx/route, for example /api/idx (it adds your secret key on your server). On a site with no server, leave it out: the browser calls https://idx.ruuster.com/v1 with no key, from your approved website address
form[data-rw-groups]: optional: the suggest groups, comma separated, for example city,zip,neighborhood
form[data-rw-min-chars] and [data-rw-debounce-ms]: 3 and 300, or your own values
.rw-search__input[placeholder]: your placeholder, or the default shown here
id, for, aria-controls: any values that are unique on the page; keep the label's for, the input's aria-controls and the listbox's id matching
Every data-rw-* attribute is a hook for ruuster-idx.js; see ../HOOKS.md.
-->
<form class="rw-recipes rw-search" role="search" action="/search" method="get" data-rw-search="" data-rw-base-url="/api/idx" data-rw-min-chars="3" data-rw-debounce-ms="300">
<label class="rw-visually-hidden" for="search-box-1-input">Search by city, ZIP code, neighborhood or address</label>
<div class="rw-search__box">
<svg class="rw-search__icon" viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" focusable="false"><circle cx="11" cy="11" r="7" fill="none" stroke="currentColor" stroke-width="2"></circle><path d="M20 20l-4-4" stroke="currentColor" stroke-width="2" stroke-linecap="round"></path></svg>
<input id="search-box-1-input" class="rw-search__input" type="text" role="combobox" autoComplete="off" spellcheck="false" aria-autocomplete="list" aria-expanded="false" aria-controls="search-box-1-list" placeholder="City, address or ZIP code" data-rw-search-input="" value=""/>
</div>
<div id="search-box-1-list" role="listbox" aria-label="Search by city, ZIP code, neighborhood or address" class="rw-search__menu" hidden="" data-rw-search-list=""></div>
<p class="rw-visually-hidden" role="status" aria-live="polite" data-rw-search-status=""></p>
</form>import { SearchBox } from "@ruuster/idx-recipes";
<SearchBox {...props} />The bar suggests after three characters, waits 300 ms between keystrokes, and works with the keyboard and screen readers: the arrow keys move through the suggestions and Enter picks one.
What the page calls
| When | Request | Key | Cached |
|---|---|---|---|
| As the visitor types | GET /v1/suggest, through your listings route |
your secret key, added on your server | an hour at Ruuster's edge |
curl -s -G "https://idx.ruuster.com/v1/suggest" \
--data-urlencode q=Midt \
--data-urlencode limit=8 \
-H "Authorization: Bearer $IDX_SECRET_KEY"import { createClient } from "@ruuster/idx";
const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });
const r = await idx.suggest({
q: "Midt",
limit: 8,
});
if (!r.ok) throw new Error(`${r.error.code}: ${r.error.message}`);
console.log(r.data);import os
import requests
res = requests.get(
"https://idx.ruuster.com/v1/suggest",
params={
"q": "Midt",
"limit": "8",
},
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
$query = http_build_query([
'q' => 'Midt',
'limit' => '8',
], '', '&', PHP_QUERY_RFC3986);
$ch = curl_init('https://idx.ruuster.com/v1/suggest' . '?' . $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
- In
.env.local, setIDX_SECRET_KEY(your secret key). No other key is needed. - Add your listings route at
app/api/idx/[...path]/route.ts, as below. It adds your secret key on your server and passes on only what a page may ask for. - Put the
SearchBoxon your homepage withaction="/search"andbaseUrl="/api/idx", as below. - Build the search page, which reads the
whereobject from the URL. - Open
http://localhost:3000, type three letters of a place and pick it.
On a site builder with no server of its own (WordPress, Squarespace, Webflow, Wix), use the plain HTML search box with no key instead: No key in your pages explains how the Listings API recognizes your site's address.
The code
The listings route:
// app/api/idx/[...path]/route.ts
import { createListingsRoute } from "@ruuster/idx/route";
// Reads IDX_SECRET_KEY from the server environment. Pages never see it.
export const GET = createListingsRoute({ basePath: "/api/idx" });
The homepage:
// app/page.tsx: the search bar hands off to /search.
import { SearchBox } from "@ruuster/idx-recipes";
export const revalidate = 300;
export default function Home() {
return (
<main>
<h1>Find your next home</h1>
<SearchBox action="/search" baseUrl="/api/idx" placeholder="City, neighborhood, ZIP or school" />
</main>
);
}
Forms and your CRM
The search bar carries no form. Leads come from the forms on your listing pages (Registration, tour and contact forms).
Rules this page follows
MUST
- Point the search box at your listings route and keep the secret key on your server.
- Pass the picked row's
whereobject to the search page unchanged.
NEVER
- Put any key in the page: not in a
NEXT_PUBLIC_variable, a prop or any browser code. - Build a
whereobject from the visitor's text yourself.
Test it
Open your homepage, press Tab until the search box has focus, type "Mid", press the down arrow and
Enter. The browser goes to /search?where=… and the results are in view source. In the browser's
network panel, the suggest request goes to /api/idx/suggest and carries no key. View the page
source and search for idx_: nothing is found.
Build it with AI
Read RUUSTER_IDX.md first and follow its MUST and NEVER rules. Ask me if it doesn't cover something.
Add a search bar to the homepage of my site that hands off to my search page, following <https://help.ruuster.com/idx/recipe-search-bar>. Use the SearchBox component with baseUrl="/api/idx" and action="/search", and add the route app/api/idx/[...path]/route.ts with `export const GET = createListingsRoute({ basePath: "/api/idx" });` if it is not there yet. It suggests places after three characters, and picking one goes to /search with the place's where object in the URL. The search page renders that search on the server with my secret key.
Use this environment variable: IDX_SECRET_KEY (server only). Never paste a secret key into a chat. 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).
When you are done, tell me how to test it with the keyboard only.On a site without React, such as PHP, Python, Ruby on Rails, Go or plain HTML, Build without React: HTML, CSS and JavaScript gives you the same markup, the stylesheet and the script for every widget.