Loading page
JavaScript integration
Use fetch when you want to keep the visitor on the page while a submission is sent, or when you need a loading state and inline errors.
POST JSON to the form endpoint with fetch. A 201 response contains success, leadId and a message. Send an Idempotency-Key header so retrying the same submission does not create a second lead.
async function sendLead(values) {
const response = await fetch("https://formivo.faizyab.me/api/f/YOUR_FORM_ID", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Idempotency-Key": crypto.randomUUID(),
},
body: JSON.stringify(values),
});
if (response.status === 201) {
const body = await response.json();
return { ok: true, leadId: body.leadId };
}
if (response.status === 429) {
return { ok: false, retryAfter: response.headers.get("Retry-After") };
}
if (response.status === 409) {
return { ok: false, message: "This form is not accepting submissions right now." };
}
return { ok: false, message: "Something went wrong. Please try again." };
}