Menu · Recipes
Recipes

Manage alerts and unsubscribe pages

The manage-alerts and unsubscribe pages every alert links to, on your own site: read the link's token, list the buyer's saved searches, and let them change how often, pause, delete or stop everything.

View as Markdownopenapi.json

Every alert links to two pages on your site, with the buyer's token in the address: your manage-alerts page and your unsubscribe page. Set their addresses once in your alert settings (manageUrl and unsubscribeUrl). The one-click unsubscribe in each email works even without these pages.

What the buyer sees

The manage page lists each saved search with how often it sends, a Pause or Resume button and a Delete button, and a "Stop all listing alerts" button under the list. The unsubscribe page says the search (or every alert) is stopped. An old link shows a short notice and changes nothing.

The manage page

The page reads the token on your server with idx.alerts.link(token), which answers the buyer and their searches, then renders ManageAlerts:

packages/ruuster-idx-recipes/snippets/alerts-manage-page.tsx

// app/alerts/manage/page.tsx: the manage-alerts page every alert links to (?token=...). The server
// reads the token with the Listings API, then ManageAlerts lets the buyer change each search.
import { createClient } from "@ruuster/idx";
import { AlertsUnsubscribed, ManageAlerts } from "@ruuster/idx-recipes";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });

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

export default async function ManageAlertsPage({ searchParams }: PageProps) {
  const token = typeof searchParams.token === "string" ? searchParams.token : "";
  const r = token ? await idx.alerts.link(token) : null;
  if (!r || !r.ok) return <AlertsUnsubscribed scope="invalid" />;
  return <ManageAlerts action="/api/alerts/manage" token={token} buyer={r.data.buyer} searches={r.data.searches} />;
}

The manage route

ManageAlerts posts each change to your route. The route checks the token again and changes only that buyer's own searches:

packages/ruuster-idx-recipes/snippets/alerts-manage-route.ts

// app/api/alerts/manage/route.ts: ManageAlerts posts here. Every change is checked against the
// link's token first, so a buyer can change only their own searches.
import { createClient, type AlertFrequency } from "@ruuster/idx";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });
const FREQUENCIES: readonly AlertFrequency[] = ["instant", "daily", "weekly"];

export async function POST(req: Request): Promise<Response> {
  let body: { token?: unknown; op?: unknown; searchId?: unknown; frequency?: unknown; status?: unknown };
  try {
    body = await req.json();
  } catch {
    return Response.json({ ok: false }, { status: 400 });
  }
  if (typeof body.token !== "string") return Response.json({ ok: false }, { status: 400 });
  const link = await idx.alerts.link(body.token);
  if (!link.ok) return Response.json({ ok: false }, { status: 410 });
  const { buyer, searches } = link.data;

  if (body.op === "unsubscribe_all") {
    const r = await idx.alerts.buyers.unsubscribe(buyer.id);
    return Response.json({ ok: r.ok }, { status: r.ok ? 200 : 502 });
  }
  const search = searches.find((s) => s.id === body.searchId);
  if (!search) return Response.json({ ok: false }, { status: 404 });
  if (body.op === "delete") {
    const r = await idx.alerts.searches.delete(search.id);
    return Response.json({ ok: r.ok }, { status: r.ok ? 200 : 502 });
  }
  if (body.op === "update") {
    const frequency = FREQUENCIES.includes(body.frequency as AlertFrequency) ? (body.frequency as AlertFrequency) : undefined;
    const status = body.status === "active" || body.status === "paused" ? body.status : undefined;
    const r = await idx.alerts.searches.update(search.id, { ...(frequency ? { frequency } : {}), ...(status ? { status } : {}) });
    return Response.json({ ok: r.ok }, { status: r.ok ? 200 : 502 });
  }
  return Response.json({ ok: false }, { status: 400 });
}

The unsubscribe page

packages/ruuster-idx-recipes/snippets/alerts-unsubscribe-page.tsx

// app/alerts/unsubscribe/page.tsx: the unsubscribe link in every alert (?token=...). One search's
// link stops that search; a digest's link stops every alert.
import { createClient } from "@ruuster/idx";
import { AlertsUnsubscribed } from "@ruuster/idx-recipes";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });

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

export default async function UnsubscribePage({ searchParams }: PageProps) {
  const token = typeof searchParams.token === "string" ? searchParams.token : "";
  const link = token ? await idx.alerts.link(token) : null;
  if (!link || !link.ok) return <AlertsUnsubscribed scope="invalid" />;
  await idx.alerts.unsubscribe(token);
  return <AlertsUnsubscribed scope={link.data.search ? "search" : "all"} />;
}

Rules