React and Next.js contact form integration
Keep visitors on your page and handle accepted, quarantined, and failed submissions explicitly.
Submit with fetch
This React example also works in a Next.js client component. Replace the endpoint with the URL copied from Forms & APIs. Public browser forms do not need a secret API key.
"use client";
import { useState, type FormEvent } from "react";
export function ContactForm() {
const [sending, setSending] = useState(false);
const [feedback, setFeedback] = useState("");
async function submit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
if (sending) return;
const form = event.currentTarget;
const values = new FormData(form);
setSending(true);
setFeedback("");
try {
const response = await fetch("https://server.infraly.online/api/forms-and-api/f/YOUR_FORM_SLUG", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(Object.fromEntries(values)),
signal: AbortSignal.timeout(15000),
});
const result = await response.json();
if (response.status === 202 && result.status === "quarantined") {
setFeedback("Your message was received for review.");
form.reset();
} else if (response.ok && result.status === "success") {
setFeedback("Thanks! Your message has been sent.");
form.reset();
} else {
throw new Error(result.message || result.error || "Please try again.");
}
} catch (error) {
setFeedback(error instanceof Error ? error.message : "Please try again.");
} finally {
setSending(false);
}
}
return (
<form onSubmit={submit}>
<label>Name <input name="name" required /></label>
<label>Email <input name="email" type="email" required /></label>
<label>Message <textarea name="message" required /></label>
<input name="_gotcha" hidden tabIndex={-1} autoComplete="off" />
<button disabled={sending}>{sending ? "Sending..." : "Send"}</button>
<p role="status">{feedback}</p>
</form>
);
}Handle the response status
HTTP 202 is a successful HTTP response, so response.ok is true. It means the submission was quarantined, not added to Leads. Check the status field as well as response.ok.
JSON requests receive JSON even if the form has a Redirect URL. Navigate to a thank-you page in your application after you confirm the result.
Use another frontend
The endpoint is framework-independent. Use the same POST URL, headers, fields, and response handling from Vue, Svelte, Angular, or plain JavaScript. No Infraly frontend SDK is required.
Check validation and production origins
Use this component in a React page or import it into a Next.js App Router page. Client-side required fields help visitors. Infraly does not enforce the full configured field schema at ingestion; add validation in a server integration you control when strict checks are needed. Replace the sample endpoint with the full URL copied from Forms & APIs. Keep workspace API keys out of NEXT_PUBLIC variables and client components.
Test an accepted message, a filled honeypot, and a failed request. Confirm the feedback changes and the Send button is re-enabled. Before publishing, register the exact Production origin and switch to a Production endpoint. Inspect Leads or Spam Shield after a timeout before manually retrying.
Infraly documentation