Skip to content
Lazyform
Docs menu

Developers

npm package

Send forms to Lazyform from any JavaScript app with one function. Typed results, no dependencies, under 1 KB.

The lazyform package sends a submission from the browser (or any runtime with fetch) and tells you exactly what happened. It has no dependencies and works with every framework.

The package isn't on npm yet. Until it is, use fetch or the drop-in widget, which work today with the same endpoint.

bash
npm install lazyform

submit()

ts
import { submit } from 'lazyform'

const result = await submit('YOUR_ACCESS_KEY', { name: 'Ada', email: 'ada@example.com', message: 'Hello' })

if (result.success) {
  console.log('Saved as', result.id)
} else {
  console.log(result.error.code, result.error.message)
}

submit(accessKey, data, options?) takes the data in whichever shape you have it:

data Sent as
A plain object URL-encoded. Arrays become repeated fields (checkbox groups); null and undefined are left out; numbers and booleans become text
An object with a File or Blob value Multipart, with the files
A FormData Multipart (your FormData isn't changed)
A <form> element Multipart, every named field in the form, like the browser would send it

The package never sends JSON, so the browser never needs a CORS preflight request: submissions are one round trip.

The result

submit() never throws. It resolves to one of:

ts
{ success: true, id: 'sub_…', message: 'Submission received.' }
{ success: false, error: { code: 'invalid_email', message: 'Please enter a valid email address.', status: 400 } }

error.message is written for visitors, so you can show it as is. error.code is one of the codes in Responses and errors, or one the package adds itself:

code When
network_error The request didn't reach Lazyform (offline, blocked, DNS)
invalid_response Something other than Lazyform answered (a proxy error page, for example)
aborted You cancelled it with signal

Options

Option Default Meaning
endpoint https://api.lazyform.dev/submit Where to post. https://api.lazyform.dev/f/YOUR_ACCESS_KEY works too
startedAt none When the form was shown (ms or Date), sent as _lf_started for the time trap
visitId none Links an earlier partial save to this submission (_lf_visit)
submitter none The clicked submit button, so its name/value is included
signal none An AbortSignal to cancel the request
fetch global fetch Your own fetch (tests, instrumentation, older runtimes)

attach()

Turns an existing <form> into an in-page form in one line. It adds the _lf_started field, sends the form without reloading the page, ignores double clicks and clears the form on success.

ts
import { attach } from 'lazyform'

const detach = attach(document.querySelector('#contact'), 'YOUR_ACCESS_KEY', {
  onSuccess: (result, form) => form.replaceWith('Thanks! We will reply soon.'),
  onError: (error) => alert(error.message),
})

Options: everything submit() takes (except startedAt and submitter, which it sets itself), plus onSuccess, onError and resetOnSuccess (default true). It returns a function that removes the listener.

If you'd rather not write any JavaScript, the drop-in widget does the same from HTML attributes and also shows the messages.

savePartial()

Saves what a visitor has typed before they submit, so you can follow up on abandoned forms. Needs the Save partial submissions feature on the form.

ts
import { newVisitId, savePartial, submit } from 'lazyform'

const visitId = newVisitId()
form.addEventListener('change', () => savePartial('YOUR_ACCESS_KEY', visitId, form))
// Later, the real submission completes the partial:
await submit('YOUR_ACCESS_KEY', form, { visitId })

Files, passwords, captcha tokens and empty fields are never sent. It resolves true when saved and false otherwise; it never throws. The widget does all of this for you with data-lazyform-partial.

TypeScript

Every type is exported: SubmitResult, SubmitSuccess, SubmitFailure, LazyformError, ErrorCode, SubmitOptions, SubmitData, FieldValue. Narrow on success:

ts
import { submit, type SubmitResult } from 'lazyform'

const result: SubmitResult = await submit(key, data)
if (!result.success && result.error.code === 'rate_limited') {
  // wait and try again later
}

React

See the React hook: useLazyform() and <LazyformForm> from lazyform/react.