Next.js
An App Router page whose client component posts JSON directly from the browser, plus an optional pass-through route for sites that want to keep a server URL.
Sample: samples/nextjs (app/contact/page.tsx, app/contact/ContactForm.tsx, optional app/api/enquiry/route.ts, README). Next.js 14 or 15, React 18 or 19.
The snippet
// app/contact/page.tsx (server component)
import ContactForm from './ContactForm';
export default function ContactPage() {
return (
<ContactForm
formId={process.env.NEXT_PUBLIC_CONTACTUSIQ_FORM_ID ?? 'au_xxxxxxx'}
turnstileSiteKey={process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY}
/>
);
}ContactForm.tsx is a 'use client' component: idle, sending, sent and error states; next/script loads Turnstile with ?render=explicit and the widget is rendered into a ref once the script reports onLoad; the honeypot is an off-screen text input; the submit fetches https://api.contactusiq.com/s/{formId} with Content-Type: application/json and Accept: application/json; 200 shows the thank-you with the id, 400 the server's error, 429 the Retry-After seconds. Turnstile is reset after every attempt.
Because the browser posts directly, the page works with static export and edge caching and involves no API route, server action or secret.
Steps
- Copy
app/contact/page.tsxandapp/contact/ContactForm.tsxinto yourapp/folder. - Set
NEXT_PUBLIC_CONTACTUSIQ_FORM_ID=au_xxxxxxxandNEXT_PUBLIC_TURNSTILE_SITE_KEY=0x4AAAAAAA_PLACEHOLDERin.env.local, or hard-code them. Leave the Turnstile key unset to submit without Turnstile. - Deploy and visit
/contacton the verified domain.
Optional: keep a server route
Sites that already post to their own URL (an existing /api/enquiry route, for example) can keep it as a pass-through. The sample's route.ts is an edge route that forwards the body and content type to the intake URL and returns the upstream status, body and Retry-After unchanged:
// app/api/enquiry/route.ts
import { NextRequest, NextResponse } from 'next/server';
export const runtime = 'edge';
export async function POST(request: NextRequest) {
const formId = process.env.CONTACTUSIQ_FORM_ID ?? 'au_xxxxxxx';
const upstream = await fetch(`https://api.contactusiq.com/s/${encodeURIComponent(formId)}`, {
method: 'POST',
headers: {
'Content-Type': request.headers.get('content-type') ?? 'application/json',
Accept: 'application/json',
Origin: request.headers.get('origin') ?? new URL(request.url).origin,
'X-Forwarded-For': request.headers.get('x-forwarded-for') ?? '',
'User-Agent': request.headers.get('user-agent') ?? '',
},
body: request.body,
...({ duplex: 'half' } as Record<string, string>),
});
return new NextResponse(upstream.body, {
status: upstream.status,
headers: {
'Content-Type': upstream.headers.get('content-type') ?? 'application/json',
...(upstream.headers.get('retry-after') ? { 'Retry-After': upstream.headers.get('retry-after')! } : {}),
},
});
}Then point the component at /api/enquiry instead of the intake URL. Same-origin requests need no CORS preflight, which makes local development easier. Note that the intake API takes the client IP from its own trusted edge header, never from user-supplied headers, so behind a proxy the per-IP rate limit applies to your server's IP; the direct browser flow is preferred in production.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
| CORS error in the browser | The page origin is not verified. Verify the domain, allow any origin on the form for development, or use the proxy route locally. |
200 but nothing in the inbox | Silent origin or spam rejection. With the proxy, confirm Origin is forwarded and is a verified domain. |
| Turnstile does not appear | Check the site key and that the domain is listed on the Turnstile widget in Cloudflare. |