@formforkio/js
v1.0.0
Published
Official Vanilla JavaScript & TypeScript SDK for FormFork — Universal form submission client, progressive HTML form enhancer, and Store-and-Forward failover.
Maintainers
Readme
@formforkio/js
Zero-dependency Vanilla JavaScript & TypeScript SDK for FormFork.
Designed for static sites (Astro, Eleventy, Hugo, Jekyll, Webflow, Shopify, plain HTML) and custom frontend architectures. Includes Triple-Layer Store-and-Forward Failover, cryptographic fallback tickets (X-FormFork-Ticket), automatic _ff_t bot-timing proof, and localStorage offline queueing.
Features
- Zero Dependencies & Tiny Footprint: Pure Fetch + FormData + AbortController implementation with full TypeScript definitions.
- Triple-Layer Store-and-Forward Resilience:
- Primary Server: Sends to
endpointHost(/f/{formId}or/api/v1/submit) with configurabletimeoutMs(6000msdefault). - Store-and-Forward Fallback Node: On
502/503/504or network timeout, seamlessly fails over to yourfallbackEndpoint(/storage-node-scripts/fallback-php/index.php). - Browser Offline Outbox: If the user has no internet connection at all, queues the payload in
localStorage(formfork_offline_outbox_v1).
- Primary Server: Sends to
- Offline Site & Form Verification: Supports both
embedToken(X-FormFork-Token: ff_pub_...) and stateless HMACfallbackTicket(X-FormFork-Ticket) so your fallback server can verify request legitimacy even while the primary FormFork server is offline. - Flexible Payloads: Pass an
HTMLFormElement, aFormDatainstance (including file uploads), or a plain JSON object.
Installation
# npm
npm install @formforkio/js
# pnpm
pnpm add @formforkio/js
# yarn
yarn add @formforkio/js
# bun
bun add @formforkio/jsQuick Start
1. Submitting a Plain Object or JSON Payload
import { createFormForkClient } from '@formforkio/js';
const formfork = createFormForkClient({
endpointHost: 'https://forms.yourdomain.com',
fallbackEndpoint: 'https://fallback.yourdomain.com/index.php',
embedToken: 'ff_pub_live_98a7bc65d4e3f210',
fallbackTicket: 'eyJmb3JtSWQiOiJmb3JtLWNvbnRhY3QiLCJkb21haW5zIjpbInlvdXJkb21haW4uY29tIl19.8f3a...',
timeoutMs: 5000,
});
const result = await formfork.submit('form-contact', {
name: 'Ada Lovelace',
email: '[email protected]',
message: 'Inquiring about enterprise self-hosted licensing.',
});
if (result.success) {
if (result.queuedViaFallback) {
console.log('Accepted by Store-and-Forward fallback node:', result.submissionId);
} else if (result.queuedOffline) {
console.log('Saved in browser offline outbox; will sync when online.');
} else {
console.log('Delivered live to primary server:', result.submissionId);
}
} else {
console.error('Submission rejected:', result.code, result.message);
}2. Binding Directly to an HTML <form> (With File Uploads)
When you pass an HTMLFormElement or FormData object, @formforkio/js automatically preserves multipart/form-data file inputs and appends the _ff_t mount timestamp:
import { FormForkClient } from '@formforkio/js';
const client = new FormForkClient({
endpointHost: 'https://forms.yourdomain.com',
fallbackEndpoint: 'https://fallback.yourdomain.com/index.php?formId=form-careers',
});
const formElement = document.querySelector<HTMLFormElement>('#careers-form')!;
const statusBanner = document.querySelector<HTMLElement>('#form-status')!;
formElement.addEventListener('submit', async (event) => {
event.preventDefault();
statusBanner.textContent = 'Uploading application...';
const res = await client.submit('form-careers', formElement);
if (res.success) {
statusBanner.textContent = res.message;
formElement.reset();
} else {
statusBanner.textContent = `Error: ${res.message}`;
}
});Zero-Setup Form Auto-Provisioning (embedToken)
If you configure an embedToken (ff_pub_live_... or ff_pub_dev_...) tied to your verified domain in FormFork, you can pass a human-readable form name instead of a form-... ID:
const client = createFormForkClient({
endpointHost: 'https://forms.yourdomain.com',
embedToken: 'ff_pub_live_98a7bc65d4e3f210',
});
// Automatically creates "Waitlist Signup" in your dashboard on first submission
await client.submit('Waitlist Signup', {
email: '[email protected]',
role: 'Engineering Lead',
});How Offline Fallback Legitimacy Works (fallbackTicket & embedToken)
When your primary FormFork server is down for maintenance or unreachable, the fallback server (fallbackEndpoint) validates incoming browser requests using:
X-FormFork-Ticket(fallbackTicket): A stateless HMAC-SHA256 signed ticket generated by@formforkio/node(createFallbackTicket) that proves theformId, allowed domains (Origin/Referer), and file upload limits without needing database access.X-FormFork-Token(embedToken): Verified against the SHA-256 token hash list in the fallback server's syncedallowlist_manifest.json.
API Reference
new FormForkClient(options?) / createFormForkClient(options?)
FormForkClientOptions
| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| embedToken | string | undefined | Public embed token (ff_pub_live_... or ff_pub_dev_...). Sent via X-FormFork-Token. |
| fallbackTicket | string | undefined | Stateless HMAC ticket generated by @formforkio/node. Sent via X-FormFork-Ticket. |
| endpointHost | string | 'https://formfork.io' | Primary FormFork server URL. |
| fallbackEndpoint | string | 'https://fallback.formfork.io/index.php' | Secondary Store-and-Forward fallback endpoint URL. |
| timeoutMs | number | 6000 | Milliseconds to wait for primary server before failing over to fallbackEndpoint. |
| enableOfflineOutbox | boolean | true | Store failed submissions in localStorage when both primary and fallback are unreachable. |
client.submit(formIdOrName, payload)
formIdOrName:string— Either an existing FormFork ID (form-...) or a human-readable form name (whenembedTokenis provided).payload:HTMLFormElement | FormData | Record<string, any>— Form element,FormData, or JSON key-value object.
Returns Promise<FormForkSubmitResponse>
| Property | Type | Description |
| :--- | :--- | :--- |
| success | boolean | true if accepted by primary, fallback node, or offline outbox. |
| submissionId | string \| undefined | Unique submission or spool ID returned by the server. |
| queuedViaFallback | boolean \| undefined | true when spooled on the Store-and-Forward fallback server. |
| queuedOffline | boolean \| undefined | true when saved to localStorage due to complete network loss. |
| message | string | Human-readable status or validation error message. |
| redirectUrl | string \| undefined | Optional redirect URL configured on the form. |
| code | string \| undefined | Machine-readable error code (e.g. FORBIDDEN_ORIGIN, network_unavailable). |
License
MIT
