Menu · Recipes
Recipes

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.

View as Markdownopenapi.json

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

Live preview
The location box the search bar is built from.
<!--
  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>

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"

Steps

  1. In .env.local, set IDX_SECRET_KEY (your secret key). No other key is needed.
  2. 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.
  3. Put the SearchBox on your homepage with action="/search" and baseUrl="/api/idx", as below.
  4. Build the search page, which reads the where object from the URL.
  5. 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 where object 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 where object 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

Prompt
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.