# Astro

## What this is

Two Astro pages for the same contact form:

- [`src/pages/contact.astro`](src/pages/contact.astro): a plain HTML `<form>` posting straight to ContactUsIQ, redirecting to `/thanks` on success. No client JavaScript except Turnstile. Ideal for static Astro sites.
- [`src/pages/contact-embed.astro`](src/pages/contact-embed.astro): the hosted embed (one `div`, one `script`) as an alternative when you want fields and theme managed from the dashboard.
- [`src/pages/thanks.astro`](src/pages/thanks.astro): the thank-you page.

This sample uses the placeholder form id `au_xxxxxxx` and the Turnstile site key placeholder `0x4AAAAAAA_PLACEHOLDER`. Replace both, or set `PUBLIC_CONTACTUSIQ_FORM_ID` and `PUBLIC_TURNSTILE_SITE_KEY` in `.env`.

## Prerequisites

- An Astro project (any adapter; static output is fine).
- `site` set in `astro.config.mjs` (used to build the absolute `_redirect` URL), for example `site: 'https://www.example.com'`.
- A ContactUsIQ workspace with the site's domain verified and a published form.

## Steps

1. Copy the three `.astro` files into `src/pages/`.
2. Add to `.env`: `PUBLIC_CONTACTUSIQ_FORM_ID=au_xxxxxxx` and `PUBLIC_TURNSTILE_SITE_KEY=0x4AAAAAAA_PLACEHOLDER`, or edit the defaults at the top of each file.
3. Make sure `site` is set in `astro.config.mjs` so `_redirect` becomes an absolute URL; otherwise hard-code it.
4. Build and deploy on the verified domain. Visit `/contact` (HTML form) or `/contact-embed` (hosted embed).

## The code

- `contact.astro`: the frontmatter reads the ids from `import.meta.env` and builds `thanksUrl` from `Astro.site`. The form is standard HTML: `action="https://api.contactusiq.com/s/au_xxxxxxx"`, `method="POST"`, fields `name`, `email`, `message`, an off-screen `_honeypot`, hidden `_redirect` and `_subject`, the `div.cf-turnstile` and the Turnstile script tag marked `is:inline` so Astro leaves it untouched.
- `contact-embed.astro`: `<div id="contactusiq-form">` plus `<script is:inline src="https://api.contactusiq.com/f/au_xxxxxxx/embed.js" data-target="#contactusiq-form">`. `is:inline` is required; without it Astro tries to bundle the external script.

## How to test

1. Open `/contact` on the verified domain, fill in the fields, complete Turnstile, click Send.
2. Expect a `303` redirect to `/thanks`. The Network tab shows `POST /s/au_xxxxxxx` -> `303` with `Location`.
3. `/contact-embed`: the form renders inside the div; submitting shows the inline thank-you and the request returns `200` with `{"ok":true,"id":"..."}`.

## Troubleshooting

- **Redirect goes to the wrong host or fails.** `_redirect` must be absolute. Check `site` in `astro.config.mjs` or hard-code the URL.
- **Redirected to thanks but nothing in the inbox, or 200 with no message.** Origin or Referer mismatch: the page is not on a verified domain (`astro dev` on `localhost` is not). Test on the deployed site or add a verified staging domain.
- **Embed script missing or 404 in the Network tab.** The `script` tag lacks `is:inline`, or the form id is wrong or unpublished.
- **Turnstile error.** Site key wrong or domain not listed on the Turnstile widget in Cloudflare.
- **429.** 5 per IP per form per 10 minutes; wait for `Retry-After`.

## For AI agents

For an Astro site, copy `src/pages/contact.astro` and `src/pages/thanks.astro` into the project, set `PUBLIC_CONTACTUSIQ_FORM_ID` (and optionally `PUBLIC_TURNSTILE_SITE_KEY`) in `.env`, and make sure `site` is set in `astro.config.mjs`. The page is a plain HTML form whose `action` is `https://api.contactusiq.com/s/{formId}` with a hidden empty `_honeypot`, a hidden absolute `_redirect`, and the Turnstile div and script (`is:inline`). Success is a `303` to the thank-you page. For the hosted embed instead, use `contact-embed.astro`, which is one div and one `is:inline` script tag.

```astro
---
const formId = import.meta.env.PUBLIC_CONTACTUSIQ_FORM_ID ?? 'au_xxxxxxx';
---
<form action={`https://api.contactusiq.com/s/${formId}`} method="POST">
  <input name="name" type="text" required />
  <input name="email" type="email" required />
  <textarea name="message" required></textarea>
  <input name="_honeypot" type="text" tabindex="-1" autocomplete="off" style="position:absolute;left:-10000px" />
  <input type="hidden" name="_redirect" value="https://example.com/thanks" />
  <div class="cf-turnstile" data-sitekey="0x4AAAAAAA_PLACEHOLDER"></div>
  <button type="submit">Send</button>
</form>
<script is:inline src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
```
