Sidebar

Mobile navigation sidebar

Developer console

React

Integrate the Address Suggest API in a React app with a debounced input and dropdown results.

Installation

No extra dependencies. Use the built-in fetch and React hooks.

Example

import { useState, useEffect, useCallback } from "react";

function AddressSuggest() {
  const [query, setQuery] = useState("");
  const [items, setItems] = useState([]);
  const [loading, setLoading] = useState(false);
  const apiUrl = "https://api.datasaras.com";
  const apiKey = "YOUR_API_KEY"; // Use env var in production

  const fetchSuggestions = useCallback(async (keyword) => {
    if (!keyword.trim()) return [];
    const params = new URLSearchParams({ keyword: keyword.trim(), limit: "10" });
    const res = await fetch(
      `${apiUrl}/address-api-v1/au/address/suggest?${params}`,
      { headers: { "x-auth-key": apiKey } }
    );
    const json = await res.json();
    return res.ok ? json.items ?? [] : [];
  }, []);

  useEffect(() => {
    if (!query.trim()) {
      setItems([]);
      return;
    }
    setLoading(true);
    const timer = setTimeout(() => {
      fetchSuggestions(query).then((data) => {
        setItems(data);
        setLoading(false);
      });
    }, 300);
    return () => clearTimeout(timer);
  }, [query, fetchSuggestions]);

  return (
    <div>
      <input
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Type an address…"
      />
      {loading && <span>Searching…</span>}
      <ul>
        {items.map((item) => (
          <li key={item.id} onClick={() => setQuery(item.address_line_1)}>
            {item.address_line_1}
          </li>
        ))}
      </ul>
    </div>
  );
}

Try it

Live example using the same pattern (config loaded from /config.json):

Loading…

Notes

  • Debouncing — Delay API calls by ~300ms while typing to avoid excessive requests.
  • API key — Never expose your API key in client-side code for production. Use a backend proxy that adds the x-auth-key header.
  • EndpointGET http://localhost:8383/address-api-v1/au/address/suggest?keyword=…&limit=10