Skip to content

Next.js quickstart

Paste one client component into an existing Next.js App Router app, and you have a waitlist.

If you don’t already have an App Router project, scaffold one, then add the SDK:

quickstarts/nextjs/install.txt
npx create-next-app@16 my-app --typescript --app
npm install @waitlo/sdk

The form is a "use client" component built on @waitlo/sdk/browser: it solves this waitlist’s challenge as soon as the page loads, re-arms it after every attempt, and retries once if it happens to expire before you click submit. Save it as app/waitlist-form.tsx:

waitlist-form.tsx
"use client";
import { useEffect, useRef, useState } from "react";
import type { FormEvent } from "react";
import { ConnectionError, Waitlo, WaitloApiError, solveChallenge } from "@waitlo/sdk/browser";
const waitlo = new Waitlo({ publishableKey: "pk_test_…" });
const waitlistId = "wl_…";
type Solved = Awaited<ReturnType<typeof solveChallenge>>;
const OUTCOME_COPY = {
joined: "You're on the list.",
pending_confirmation: "Almost done: check your inbox to confirm your email address.",
duplicate: "You're already on the list.",
invalid_email: "That doesn't look like a valid email address.",
rate_limited: "Too many attempts. Please try again in a moment.",
challenge_failed: "We couldn't verify your submission. Please try again.",
submission_limit_reached: "This isn't accepting submissions right now. Try again later.",
connection_error: "Couldn't reach the server. Check your connection and try again.",
generic_error: "Something went wrong. Please try again.",
};
function mapError(error: unknown): keyof typeof OUTCOME_COPY {
if (error instanceof WaitloApiError) {
if (error.is("duplicate_entry")) return error.entry.pending_confirmation ? "pending_confirmation" : "duplicate";
if (error.is("parameter_invalid") && error.param === "email") return "invalid_email";
if (error.is("rate_limited")) return "rate_limited";
if (error.is("challenge_missing") || error.is("challenge_invalid") || error.is("challenge_expired")) return "challenge_failed";
if (error.is("submission_limit_reached")) return "submission_limit_reached";
} else if (error instanceof ConnectionError) {
return "connection_error";
}
console.error(error);
return "generic_error";
}
export function WaitlistForm() {
const [email, setEmail] = useState("");
const [submitting, setSubmitting] = useState(false);
const [outcome, setOutcome] = useState<keyof typeof OUTCOME_COPY | null>(null);
const solvedRef = useRef<Promise<{ challenge: Parameters<typeof solveChallenge>[0]; solution: Solved }> | null>(null);
function rearmChallenge() {
// A challenge is spent per address (ADR 0037, ADR 0180): re-armed after every attempt, so the next click only waits on the network.
solvedRef.current = waitlo.waitlists.challenge(waitlistId).then(async (challenge) => ({ challenge, solution: await solveChallenge(challenge) }));
}
useEffect(() => {
rearmChallenge();
}, []);
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setSubmitting(true);
const body = { email, waitlo_website: "" };
async function attempt(): Promise<keyof typeof OUTCOME_COPY> {
const { challenge, solution } = await solvedRef.current!;
const altcha = btoa(JSON.stringify({ challenge, solution }));
const entry = await waitlo.waitlists.join(waitlistId, { ...body, altcha });
return entry.pending_confirmation ? "pending_confirmation" : "joined";
}
try {
setOutcome(await attempt());
} catch (error) {
if (error instanceof WaitloApiError && error.is("challenge_expired")) {
rearmChallenge();
try {
setOutcome(await attempt());
} catch (retryError) {
setOutcome(mapError(retryError));
}
} else {
setOutcome(mapError(error));
}
}
rearmChallenge();
setSubmitting(false);
}
return (
<form onSubmit={handleSubmit}>
<input type="text" name="waitlo_website" value="" tabIndex={-1} autoComplete="off" aria-hidden="true" style={{ position: "absolute", left: -9999 }} readOnly />
<label htmlFor="email">Email</label>
<input id="email" type="email" required value={email} onChange={(event) => setEmail(event.target.value)} />
<button type="submit" disabled={submitting}>Join the waitlist</button>
<p role="status">{outcome ? OUTCOME_COPY[outcome] : ""}</p>
</form>
);
}

Then render it from a page next to it:

quickstarts/nextjs/page.tsx
// Paste this next to the form you saved as `app/waitlist-form.tsx`
// (ADR 0179): the form itself is rendered above it on the page, from the
// same `renderSnippet` call `tests/browser/quickstart-nextjs.test.ts` drives
// in Chromium.
import { WaitlistForm } from "./waitlist-form";
export default function Page() {
return (
<main>
<h1>Join the waitlist</h1>
<WaitlistForm />
</main>
);
}

Run the app and open the page. Test mode accepts requests from localhost on any port, so this runs with nothing else configured: joining shows “You’re on the list.”, joining again shows “You’re already on the list.”, and anything the server or your connection can’t answer shows one plain line instead of a stack trace.

Swap the pk_test_ key for this waitlist’s pk_live_ key, add the page’s real origin to the project’s allowed origins, and drop baseUrl from the options object above – the client already defaults to the live API.

This form joins with no consent wording, so none of its entries can ever receive an Update. Read Consent and Updates under CASL before you turn that on.