Skip to Content
FrameworksNext.js

Next.js

For the App Router. Pages Router works the same with an API route instead of a server action.

Add your keys

.env.local
NEXT_PUBLIC_WAITLISTLY_PUBLIC_KEY=pk_your_public_key WAITLISTLY_SECRET_KEY=sk_your_secret_key

Collect from your waitlist form

app/waitlist-form.tsx
'use client'; import { useState } from 'react'; export function WaitlistForm() { const [result, setResult] = useState<{ status: string; position?: number; referral_link?: string } | null>(null); async function onSubmit(e: React.FormEvent<HTMLFormElement>) { e.preventDefault(); const email = new FormData(e.currentTarget).get('email'); const res = await fetch('https://waitlistly.live/api/v1/contacts', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Waitlistly-Key': process.env.NEXT_PUBLIC_WAITLISTLY_PUBLIC_KEY! }, body: JSON.stringify({ email, ref: new URLSearchParams(location.search).get('ref') }), }); setResult(await res.json()); } if (result?.status === 'joined') return <p>You're #{result.position} in line. Share {result.referral_link} to move up.</p>; return ( <form onSubmit={onSubmit}> <input name="email" type="email" required placeholder="you@company.com" /> <button>Join the waitlist</button> </form> ); }

Add a small helper for the secret calls

lib/waitlistly.ts
import 'server-only'; async function call(path: string, body: unknown) { const res = await fetch(`https://waitlistly.live/api/v1/${path}`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${process.env.WAITLISTLY_SECRET_KEY}` }, body: JSON.stringify(body), }); return res.json(); } export const verifyInvite = (code: string) => call('invites/verify', { code }); export const reportActivation = (code: string) => call('activations', { code });

Gate and activate in your sign-up

app/signup/actions.ts
'use server'; import { verifyInvite, reportActivation } from '@/lib/waitlistly'; export async function signUp(formData: FormData) { const code = String(formData.get('invite_code') || ''); const invite = await verifyInvite(code); if (!invite.valid) return { error: 'This invite is not valid or has expired.' }; // …create the account with your auth library… await reportActivation(code); }

On the sign-up page, prefill the invited email and pass the code in a hidden input:

app/signup/page.tsx
'use client'; import { useEffect, useState } from 'react'; import { signUp } from './actions'; export default function SignUp() { const [invite, setInvite] = useState<{ code: string; email: string | null; valid: boolean } | null>(null); useEffect(() => { const code = new URLSearchParams(location.search).get('invite_code'); if (!code) return; fetch('https://waitlistly.live/api/v1/invites/lookup', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Waitlistly-Key': process.env.NEXT_PUBLIC_WAITLISTLY_PUBLIC_KEY! }, body: JSON.stringify({ code }), }) .then((r) => r.json()) .then((r) => setInvite({ code, email: r.email ?? null, valid: r.valid })); }, []); if (invite && !invite.valid) return <p>This invite is not valid or has expired.</p>; return ( <form action={signUp}> <input name="email" type="email" defaultValue={invite?.email ?? ''} key={invite?.email ?? 'empty'} required /> <input name="invite_code" type="hidden" value={invite?.code ?? ''} readOnly /> <input name="password" type="password" required /> <button type="submit">Create account</button> </form> ); }
Last updated on