Reads form schemas and submits form data, via lynkow.forms.

Fetch a form's schema to render it, then submit the visitor's answers keyed by field id. Submissions carry the honeypot and timestamp spam fields automatically.

Access via: lynkow.forms

Methods

3 methods

clearCache

TypeScript
clearCache(): void

Clears every cached form schema, so the next getBySlug() fetches the current fields and settings. Call it after a form changes, for example on a form.* webhook. Has no effect without cache: true.

Returns: void

TypeScript
lynkow.forms.clearCache()

getBySlug

TypeScript
getBySlug(slug: string, options?: BaseRequestOptions): Promise<Form>

Fetches an active form by its slug: its field schema, settings and verification policy. With cache: true, cached for 10 minutes per locale.

With a locale, a form not translated in it is served from the site's next locale and carries isFallback: true. On a multi-language site, fallback: false turns that case into a 404.

Throws LynkowError NOT_FOUND with apiCode RESOURCE_NOT_FOUND when no active form has this slug, and without a request for an empty, . or .. slug.

Parameter

Type

Description

slug

string

The form's slug, e.g. 'contact'.

options

BaseRequestOptions

Request options: locale, fallback and fetchOptions.

Returns: Promise<Form>

TypeScript
const form = await lynkow.forms.getBySlug('contact')

// Each field's `id` is the key you submit its value under.
form.schema.fields.forEach(field => {
  console.log(field.id, field.type, field.label, field.required)
})

submit

TypeScript
submit(slug: string, data: FormSubmitData, options?: TimedSubmitOptions & BaseRequestOptions): Promise<FormSubmitResponse>

Submits a visitor's answers to a form, keyed by FormField.id. Adds the spam fields _hp and _ts (the client's creation time, capped at 55 minutes ago, unless you pass formStartedAt) and the signed-in customer's session, which a requireAuth form needs. When the site or form uses reCAPTCHA, pass a v2 token or a v3 token with action form_submit. The locale selects the translation, as in getBySlug(). Sent once, never retried.

Throws LynkowError VALIDATION_ERROR per failing field, NOT_FOUND for an unknown slug, BAD_REQUEST with apiCode RESOURCE_LOCKED (form closed), RATE_SUBMISSION_EXPIRED, SPAM_RECAPTCHA_REQUIRED or SPAM_RECAPTCHA_FAILED, UNAUTHORIZED with CUSTOMER_AUTH_REQUIRED, and TOO_MANY_REQUESTS with RATE_SUBMISSION_TOO_FAST when sent too soon after _ts.

Parameter

Type

Description

slug

string

The form's slug, e.g. 'contact'.

data

FormSubmitData

Values keyed by FormField.id from getBySlug(). Upload a file first and send its reference.

options

TimedSubmitOptions & BaseRequestOptions

recaptchaToken, formStartedAt (see TimedSubmitOptions), locale and fetchOptions.

Returns: Promise<FormSubmitResponse>

TypeScript
const form = await lynkow.forms.getBySlug('contact')

// Build the payload with field.id as key. The SDK does not transform
// semantic names into ids; you must do that yourself based on the schema.
const data: Record<string, string> = {}
for (const field of form.schema.fields) {
  data[field.id] = readValueForField(field) // your UI binding
}

const result = await lynkow.forms.submit('contact', data)

// Never hand `redirectUrl` to `window.location` as it comes: parse it, and
// follow it only when it resolves to http or https.
function redirectTarget(redirectUrl?: string): string | null {
  if (!redirectUrl) return null
  try {
    const url = new URL(redirectUrl, window.location.href)
    return url.protocol === 'http:' || url.protocol === 'https:' ? url.href : null
  } catch {
    return null
  }
}

const target = redirectTarget(result.redirectUrl)
if (target) {
  // The form is configured to send the visitor to a thank-you page
  window.location.assign(target)
} else {
  // Show the success message
  console.log(result.message)
}