# Build a podcast search

> A search box with filters on your own site, backed by the API.

Source: https://www.podcastlookup.com/documentation/guides/search-ui

Your key must stay secret, so the browser talks to **your** server, which calls Podcast Lookup. Here with a Next.js route handler; any backend works the same way.

```txt
app/
  api/
    podcasts/
      route.ts
  page.tsx
```

### Proxy the search

Forward only the parameters you allow, and cache results so repeated searches don't cost credits:

```ts title="app/api/podcasts/route.ts"
const ALLOWED = ["q", "lang", "category", "sort", "page"]

export async function GET(request: Request) {
  const incoming = new URL(request.url).searchParams
  const params = new URLSearchParams()
  for (const [key, value] of incoming) if (ALLOWED.includes(key)) params.append(key, value)

  const response = await fetch(`https://www.podcastlookup.com/api/v1/podcasts?${params}`, {
    headers: { Authorization: `Bearer ${process.env.PODCAST_LOOKUP_API_KEY}` },
    next: { revalidate: 3600 }, // one credit per distinct search per hour
  })
  return new Response(response.body, { status: response.status, headers: { "Content-Type": "application/json" } })
}
```

### Fill the filters

Call [`/facets`](https://www.podcastlookup.com/documentation/rest-api/list-filters.md) once at build time or once a day, and render `languages` and `categories` as select menus.

### Search as the user types

Debounce input (300 ms or so) so each keystroke doesn't become a credit:

```tsx title="app/page.tsx"
"use client"

import { useEffect, useState } from "react"

export default function Page() {
  const [q, setQ] = useState("")
  const [results, setResults] = useState<any[]>([])

  useEffect(() => {
    if (q.length < 2) return setResults([])
    const timer = setTimeout(async () => {
      const response = await fetch(`/api/podcasts?q=${encodeURIComponent(q)}`)
      setResults((await response.json()).results ?? [])
    }, 300)
    return () => clearTimeout(timer)
  }, [q])

  return (
    <>
      <input value={q} onChange={(event) => setQ(event.target.value)} placeholder="Search podcasts" />
      <ul>
        {results.map((podcast) => (
          <li key={podcast.id}>
            <img src={podcast.image_url} alt="" width={48} height={48} /> {podcast.title}, {podcast.author}
          </li>
        ))}
      </ul>
    </>
  )
}
```

> **Note: Protect your credits**
>
> A public search box can be scripted by anyone. Add a rate limit per visitor on your proxy route, and cache aggressively.