# AutoTill × your booking system — developer guide

> One file. Paste it into Claude, ChatGPT, Cursor or Copilot along with your codebase and say "implement levels 1–2 first".
> Tyson (the AutoTill agent) **never takes payment and never confirms a booking**. Your booking page always does that.

---

## 0. Before you start — what you need, and when

| # | Item | Needed for | Where to get it |
|---|------|-----------|-----------------|
| A | **Website snippet** | Required | AutoTill dashboard → **Install** → copy snippet |
| B | **Website ID** | Required for webhook (level 4) | Dashboard → **Booking setup** |
| C | **AutoTill API key** (`sk_live_…`) | Only level 2 (push services/prices) | Dashboard → **API keys** → Create key. Shown once. Server-side only. |
| D | **Your own developer key** | Only level 3, if your postcode/slot endpoints need auth | You create it; paste it into **Booking setup → Free slots → Developer key** |
| E | **Webhook signing secret** | Only level 4 | Dashboard → **Booking setup → Booking paid** |

Never put C, D or E in browser code.

Each level is independent. Level 1 alone already works.

---

## Level 1 — Snippet + booking link (no keys)

1. Paste snippet (A) on every page, before `</body>`, or via Google Tag Manager.
2. In **Booking setup → Booking link** enter your booking page address. Placeholders are optional:

```
https://order.example.com/?step=1&postcode={postcode}&services={services}&collection={collection}&ref={ref}
```

| Placeholder | Example value |
|---|---|
| `{postcode}` | `E1 6AN` |
| `{services}` | `Wash & Fold,Ironing` (comma-separated, exact service names) |
| `{collection}` | `Sunday 25 October 7:00am–10:00am` (preference only) |
| `{delivery}` | as above |
| `{ref}` | AutoTill conversation reference |

If you don't use `{ref}`, AutoTill appends `autotill_ref=…`. **Store that value with the booking** — level 4 uses it.

Your page should read these query params and pre-fill its steps. Unknown params must be ignored safely.

**Test:** ask Tyson "I'd like to book a collection in E1 6AN" → the "Continue booking" button should open your page pre-filled.

---

## Level 2 — Services and prices (API key C)

Base URL: `https://fslswfrutcitukxzkhib.supabase.co/functions/v1/api-v1/v1`
Auth: `Authorization: Bearer sk_live_…` (or `X-API-Key`).

```bash
curl -X POST "$BASE/services" \
  -H "Authorization: Bearer $AUTOTILL_API_KEY" -H "Content-Type: application/json" \
  -d '{"services":[
    {"name":"Wash & Fold","summary":"Washed, dried and neatly folded","price_signal":"From £3.10/kg","image_url":"https://cdn.example.com/icons/wash-fold.png","url":"https://example.com/laundry"},
    {"name":"Ironing","summary":"Pressed only, for clean items","price_signal":"From £1.90/item","image_url":"https://cdn.example.com/icons/ironing.png"}
  ]}'
```

- Upserts by `name`. Items edited by hand in the dashboard are left alone.
- `price_signal` is shown **word for word** — Tyson never calculates or invents prices.
- `image_url` must be HTTPS; it appears beside the service in chat.
- `enabled: false` hides a service.
- Set **minimum order** and **service charge** in Booking setup (e.g. £20.00 and £1.99); Tyson shows them on the service card.

```js
// Node / server only
await fetch(`${BASE}/services`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.AUTOTILL_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ services }),
});
```

Run on deploy or whenever prices change.

**Test:** ask Tyson "What are your prices?" → a service list with Select buttons appears.

---

## Level 3 — Live postcode and slot checks (your endpoints, optional key D)

Expose two **read-only** GET endpoints. AutoTill calls them server-to-server only, sends key D as `Authorization: Bearer <key>` if you set one, and gives up after **4 seconds** (Tyson then says to check on the booking page).

```
GET https://api.example.com/autotill/coverage?postcode=E1%206AN
→ 200 { "covered": true }

GET https://api.example.com/autotill/slots?postcode=E1%206AN
→ 200 { "slots": [
  { "label": "Sunday 25 October, 7:00am–10:00am collection" },
  { "label": "Sunday 25 October, 6:00pm–9:00pm collection" }
] }
```

- Wrap your existing postcode/address service — you do not need AutoPostcode.
- Labels are shown exactly; use UK local time and include the day.
- Return only genuinely free slots. Tyson shows up to three and never reserves them.
- Paste both URLs into **Booking setup → Postcode coverage / Free slots**.

**Test:** `curl` both URLs with the key, then ask Tyson "Can you collect from E1 6AN?".

---

## Level 4 — Booking paid webhook (signing secret E)

When a booking is paid or cancelled, POST to:

```
POST https://autotill.com/api/public/bookings/webhook
x-autotill-store: <Website ID (B)>
x-autotill-signature: <hex HMAC-SHA256 of the raw body, key = signing secret E>
Content-Type: application/json

{ "booking_id":"L2L-104233", "status":"paid", "value":34.50, "currency":"GBP",
  "ref":"<autotill ref from level 1>", "postcode":"E1 6AN",
  "services":["Ironing","Alterations"],
  "collection_slot":"Sunday 25 October, 7:00am–10:00am",
  "delivery_slot":"Monday 26 October, 6:00pm–9:00pm" }
```

```js
import crypto from "node:crypto";
const body = JSON.stringify(payload);
const sig = crypto.createHmac("sha256", process.env.AUTOTILL_SIGNING_SECRET).update(body).digest("hex");
await fetch("https://autotill.com/api/public/bookings/webhook", {
  method: "POST",
  headers: { "x-autotill-store": process.env.AUTOTILL_WEBSITE_ID, "x-autotill-signature": sig, "Content-Type": "application/json" },
  body, // sign and send the exact same string
});
```

`status`: `paid` or `cancelled`. Bookings without a `ref` are still recorded but not credited to Tyson.

---

## Troubleshooting

| Symptom | Fix |
|---|---|
| Webhook 401 | Sign the **exact raw body** you send; don't re-serialise. Check Website ID header. |
| Services don't appear | Website type must be **Bookings**; key must be `sk_live_`; check `enabled`. |
| Tyson says "check on the booking page" | Coverage/slot endpoint slower than 4 s, non-200, or wrong JSON shape. |
| Wrong times | Labels must already be in UK local time — AutoTill doesn't convert. |
| Bookings not credited | `ref` / `autotill_ref` not stored and returned in the webhook. |

---

## Prompt for your AI assistant

```
You are integrating AutoTill into this booking website. Read the guide above.
1. Add the AutoTill snippet to the global layout.
2. Make the booking page read postcode, services, collection, delivery and ref/autotill_ref
   from the query string, pre-fill those steps, and persist ref on the booking record.
3. Add a server job that POSTs our services (name, summary, price_signal, image_url) to
   AutoTill /services using env AUTOTILL_API_KEY whenever prices change.
4. Add GET /autotill/coverage and /autotill/slots wrapping our existing postcode and slot
   logic, protected by a bearer key in env AUTOTILL_INBOUND_KEY, responding within 2 seconds.
5. On payment success and cancellation, send the signed webhook using env
   AUTOTILL_SIGNING_SECRET and AUTOTILL_WEBSITE_ID.
Never expose keys to the browser. Don't change pricing or payment logic. Add tests for 2–5.
```
