React
Works in any React app (Vite, Next.js, Remix). There is no SDK: the component calls the endpoint with fetch and asks for JSON so it can show the result.
ContactForm.jsx
import { useState } from "react";
export function ContactForm() {
const [status, setStatus] = useState("idle"); // idle | sending | sent | error
const [message, setMessage] = useState("");
async function onSubmit(event) {
event.preventDefault();
setStatus("sending");
const fields = Object.fromEntries(new FormData(event.currentTarget));
try {
const res = await fetch("https://api.formtura.app/v1/submit/YOUR_FORM_ID", {
method: "POST",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body: JSON.stringify(fields),
});
const data = await res.json().catch(() => ({}));
setStatus(res.ok ? "sent" : "error");
setMessage(res.ok ? data.message : data.error ?? "Something went wrong.");
} catch {
setStatus("error");
setMessage("Couldn't reach the server. Please try again.");
}
}
if (status === "sent") return <p>{message}</p>;
return (
<form onSubmit={onSubmit}>
<label>
Name
<input type="text" name="name" required />
</label>
<label>
Email
<input type="email" name="email" required />
</label>
<label>
Message
<textarea name="message" required />
</label>
{/* Spam trap: hidden from people, filled in by bots. Keep it empty. */}
<input type="text" name="_gotcha" tabIndex={-1} autoComplete="off"
style={{ position: "absolute", left: -9999 }} aria-hidden="true" />
<button type="submit" disabled={status === "sending"}>
{status === "sending" ? "Sending…" : "Send"}
</button>
{status === "error" && <p role="alert">{message}</p>}
</form>
);
}Notes
- Send
Accept: application/json. You then get{ "success": true, "message": … }instead of an HTML page. - On errors, show
data.errorto the visitor and usedata.codein your own logic. See Errors. - In Next.js, mark the component
"use client". The endpoint is public, so it is safe to keep in client code.
Never put your project's secret key in React code. Use the public endpoint from the browser, and the secret key only on a server.