# Forms

Contact, quote, signup and other forms on a site hosted by Site Star.

## What it does

A visitor fills in a form on the site. Site Star stores the submission for that site and tells the owner about it. There is nothing to set up: a form on any hosted site works as soon as it is published.

- **Each form has a name**, such as `contact` or `quote`. The owner's submissions are grouped by it. Names are stored in capitals (`contact` becomes `CONTACT`), and a submission with no name lands under `GENERAL`.
- **The site is known from where the form was sent.** A submission belongs to the site whose address the page was served from. A form cannot name a different site.
- **Fields are free-form.** Any field name is accepted and stored as text. Two names mean something: `email`, which must look like an email address when it is filled in, and `phone`, which becomes the visitor's contact number.
- **A submission must contain something.** One with every field empty is refused.
- **Bots are screened.** Every form carries an empty hidden field named `_honeypot`. A submission that fills it is refused.
- **The owner sees where the visitor came from.** The page the form was on, and how the visitor reached the site, are recorded with the submission.
- **Being stored is not the same as being answered.** A successful submission means Site Star has it. It does not mean an email was sent to the visitor.

Limits: 10 KB per submission, about 37 fields, and 2,000 characters per value (longer values are cut). Values are plain text: no files, lists or nested objects.

## Configure

A form needs no settings. Two things are optional, and the owner controls both in Site Star.

**Form rules.** The owner can add rules for a named form: required fields, formats, allowed values and lengths. Site Star then refuses a submission that breaks them and says which field failed. A form with no rules is accepted as it is sent.

Rules are one setting per form, `forms.definitions.<form name>`, holding that form's whole definition:

```json
{
  "properties": {
    "name": { "minLength": 2 },
    "email": { "format": "email" },
    "phone": { "format": "phone" },
    "service": { "enum": ["repair", "install", "inspection"] }
  },
  "required": ["name", "email"]
}
```

| Keyword | Where | Meaning |
|---|---|---|
| `properties` | Form or step | The fields the rules cover. At most 50 per form. |
| `required` | Form or step | Field names that must be filled in. |
| `additionalProperties` | Form | `false` refuses any field that is not listed. |
| `format` | Field | `email`, `phone`, `date` or `uri`. |
| `enum` | Field | The allowed values. At most 100. |
| `minLength`, `maxLength` | Field | Length limits. `maxLength` is at most 2,000. |
| `steps` | Form | A multi-step form: 2 to 10 steps in order, each with an `id`, `properties` and `required`. |

`pattern` is not supported. A field name may not start with `_`.

> A rule only checks fields the page already sends. Requiring a field the form does not show makes every submission of that form fail. Add the field to the page first, publish, then add the rule.

The owner sets rules by asking their Site Star assistant, or with [`edit_site_config`](/guides/configuration) over the API or MCP. The dashboard lists them under Settings, Forms.

**Spam controls.** The same Settings, Forms section holds the owner's spam controls.

## Use in your site

**Tell your agent:** "Add a contact form with name, email and message, and show the visitor a thank-you when it is sent."

```html
<form id="contact-form">
  <label for="contact-name">Name</label>
  <input id="contact-name" name="name" autocomplete="name" required />

  <label for="contact-email">Email</label>
  <input id="contact-email" name="email" type="email" autocomplete="email" required />

  <label for="contact-message">Message</label>
  <textarea id="contact-message" name="message" required></textarea>

  <!-- Stays empty. Hidden from people, present in the page. -->
  <input name="_honeypot" tabindex="-1" autocomplete="off" aria-hidden="true"
         style="position:absolute;left:-9999px" />

  <button type="submit">Send</button>
  <p id="contact-status" role="status"></p>
</form>
```

```typescript
import { createSiteStar, FormSubmitError } from '@sitestar/sdk'

async function setUpContactForm(): Promise<void> {
  const form = document.querySelector<HTMLFormElement>('#contact-form')
  const status = document.querySelector<HTMLElement>('#contact-status')
  if (!form || !status) return

  // Reuse the site's one createSiteStar() call if it already has one.
  const sitestar = await createSiteStar()
  let sending = false

  form.addEventListener('submit', async (event) => {
    event.preventDefault()
    if (sending) return
    sending = true
    form.setAttribute('aria-busy', 'true')

    const fields: Record<string, string> = {}
    for (const [name, value] of new FormData(form)) {
      if (typeof value === 'string') fields[name] = value
    }

    try {
      await sitestar.forms.submit('contact', fields)
      status.textContent = 'Thanks. We have your message.'
      form.reset()
    } catch (error) {
      if (!(error instanceof FormSubmitError)) throw error
      status.textContent = error.message
    } finally {
      sending = false
      form.removeAttribute('aria-busy')
    }
  })
}

void setUpContactForm()
```

### Rules

1. **Name every form.** The first argument to `submit` is the form's name. Use one stable, lowercase name per form, and keep it once the form is live: the owner's rules and history are filed under it.
2. **Keep the `_honeypot` input.** It is in the page, hidden from people, always empty, and sent with the other fields.
3. **Name the fields `email` and `phone`** when the form collects them. Under any other name they are stored as ordinary text and the owner cannot reply from them.
4. **Send plain values.** Strings, numbers and booleans. No files, lists or objects.
5. **Do not send your own lead event.** The SDK sends Google's `generate_lead` once for each accepted submission. A second one double-counts every conversion.
6. **Do not add a site, domain or key field.** The site is known from where the form was sent.
7. **Stop a second click while one is sending.** Use a flag and `aria-busy`, as above. Do not set `disabled` on the button: it drops keyboard focus mid-send.
8. **Keep what the visitor typed when a submission fails.**

### When a submission fails

`submit` throws a `FormSubmitError`. Its `message` is ready to show the visitor.

| `error.kind` | What happened | What to offer |
|---|---|---|
| `network` | The request never reached Site Star. | Try again. |
| `rejected` | The submission was refused: a field broke a rule, or it was empty. | Fix the fields. See `error.fieldErrors`. |
| `rate-limited` | Too many attempts. | Wait, then try again. |
| `server` | Something is wrong on Site Star's side or in the site's setup. | Another way to get in touch. The visitor cannot fix this. |

When the form has rules, `error.fieldErrors` names each field that failed:

```typescript
error.fieldErrors
// { name: { code: 'min-length', message: 'must be at least 2 characters' } }
```

Show each one beside its input and connect it with `aria-describedby`. `message` is a short English phrase written to follow the field's label. A site in another language shows its own text for each `code`: `required`, `not-accepted`, `format`, `enum`, `min-length`, `max-length`, `unknown-step`, `step-order` or `unknown`.

To change or translate the four general messages, pass them once at start-up:

```typescript
const sitestar = await createSiteStar({
  strings: { formErrorNetwork: 'Check your connection and try again.' },
})
```

The keys are `formErrorNetwork`, `formErrorRejected`, `formErrorRateLimited` and `formErrorServer`.

### Multi-step forms

A multi-step form needs rules with `steps`. Each step is sent on its own, named by its `id`, and passes the previous step's token on:

```typescript
const first = await sitestar.forms.submit('quote', { email }, { step: 'contact' })

await sitestar.forms.submit(
  'quote',
  { budget },
  { step: 'details', updateToken: first.updateToken },
)
```

- The first step creates the submission, so the owner has the lead even if the visitor stops there.
- Steps are accepted in order. A step sent too early is refused with `step-order`, naming the step that comes first.
- Every step returns a new token. Always pass the most recent one.
- A token lasts about two hours. If a later step arrives with an expired token, it is kept as a second submission, so nothing the visitor typed is lost.
- Keep the token in memory only. Do not write it to storage.

### Local development

A form can only be sent from a page that reaches Site Star through the site's own address. On a local dev server, proxy `/api/v1` to the site's Site Star address.

## Reference

- [Publish contract](/build/publish-contract)
- [Configuration](/guides/configuration), for setting form rules over the API or MCP
- [Agent guide for building a site](/agents/site-builder.md)
