# Plain HTML quickstart

> Take bookings on any website without a build step, first with the booking widget in two lines, then with a booking form of your own that calls the public booking API with fetch.

Source: https://docs.bifrostorbit.com/booking/developers/quickstart-html/

This works on any website you can add HTML to, without npm or a build step. Part 1 shows Booking Orbit's own booking screens on your page in five minutes. Part 2 builds a booking form in your own design with the public booking API.

**Tested with:** Chromium, a page served with `python3 -m http.server`, against a local Booking Orbit.

## Before you start

You need:

- **A Booking Orbit location** with at least one service and someone who takes it, such as `northwilds-oslo`. Its short name is the last part of its booking page address.
- **A way to serve a page,** such as `python3 -m http.server 8080` in a folder. Opening the file straight from disk doesn't work for part 2, because the browser then sends `null` as the website's address, which can't be allowed.

## Part 1: the widget

## 1. Add the widget

```html
<!-- index.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Book a boot fitting at Northwilds Oslo</title>
    <script src="https://app.bifrostorbit.com/booking/widget.js" async></script>
  </head>
  <body>
    <h1>Book a boot fitting</h1>
    <div data-booking-orbit="northwilds-oslo" data-service="boot-fitting"></div>

    <p>Or <button type="button" data-booking-orbit-open="northwilds-oslo">see every service</button>.</p>
  </body>
</html>
```

The `div` shows the booking screens for boot fittings in the page. The button opens every service in a popup.

## 2. Try it

Open `http://localhost:8080/index.html` and book. The widget needs no settings in Booking Orbit, works on any website, and also takes payments for services paid in advance. [Embed the booking widget](https://docs.bifrostorbit.com/booking/developers/embed-the-widget/) lists its options.

## Part 2: your own form

## 3. Allow your website

Under the location's **Settings › Your website**, add `http://localhost:8080` to **Other websites**, and your real website's address before you go live. Reading services works from any website, but holding and booking only from allowed ones. See [Allowed websites](https://docs.bifrostorbit.com/booking/developers/allowed-websites/).

## 4. Add the form

```html
<!-- book.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Book at Northwilds Oslo</title>
  </head>
  <body>
    <main>
      <h1>Book at Northwilds Oslo</h1>
      <p id="error" role="alert" hidden></p>
      <ul id="services"></ul>
      <section id="times" hidden><h2>Pick a time</h2></section>
      <form id="details" hidden>
        <h2 id="chosen"></h2>
        <p>We're holding this time for you for 10 minutes.</p>
        <label>Name <input name="name" required autocomplete="name" /></label>
        <label>Email <input name="email" type="email" required autocomplete="email" /></label>
        <button type="submit">Book</button>
      </form>
    </main>

    <script type="module">
      const API = "https://app.bifrostorbit.com/booking/api/public/northwilds-oslo";
      const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
      const timeOf = (iso) => new Intl.DateTimeFormat("en-GB", { timeZone, hour: "2-digit", minute: "2-digit" }).format(new Date(iso));
      const dateOf = (iso, zone = timeZone) => new Intl.DateTimeFormat("en-GB", { timeZone: zone, weekday: "long", day: "numeric", month: "long" }).format(new Date(iso));
      const $ = (id) => document.getElementById(id);
      let service = null;
      let hold = null;

      async function call(path, body) {
        const response = await fetch(API + path, body ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) } : {});
        const answer = await response.json();
        if (answer.error?.code === "payment_required") location.href = answer.error.bookingUrl;
        if (!response.ok) throw Object.assign(new Error(answer.error.message), { status: response.status });
        return answer.data;
      }

      function show(message) {
        $("error").textContent = message;
        $("error").hidden = !message;
      }

      function button(label, onClick) {
        const element = document.createElement("button");
        element.type = "button";
        element.textContent = label;
        element.addEventListener("click", onClick);
        return element;
      }

      async function choose(next) {
        service = next;
        hold = null;
        $("details").hidden = true;
        const slots = await call(`/slots?service=${service.id}&days=7&timeZone=${encodeURIComponent(timeZone)}`);
        $("times").replaceChildren($("times").firstElementChild);
        for (const day of slots.days.filter((entry) => entry.slots.length)) {
          const group = document.createElement("div");
          const heading = document.createElement("h3");
          heading.textContent = dateOf(`${day.date}T12:00:00Z`, "UTC");
          group.append(heading, ...day.slots.map((slot) => button(timeOf(slot.startsAt), () => pick(slot.startsAt))));
          $("times").append(group);
        }
        $("times").hidden = false;
      }

      async function pick(startsAt) {
        show("");
        try {
          hold = await call("/holds", { service: service.id, startsAt });
          hold.startsAt = startsAt;
          $("chosen").textContent = `${service.name} at ${timeOf(startsAt)}`;
          $("times").hidden = true;
          $("details").hidden = false;
        } catch (error) {
          show(error.message);
          if (error.status === 409) await choose(service);
        }
      }

      $("details").addEventListener("submit", async (event) => {
        event.preventDefault();
        const form = new FormData(event.currentTarget);
        try {
          const { appointment } = await call("/appointments", { service: service.id, startsAt: hold.startsAt, holdToken: hold.token, name: form.get("name"), email: form.get("email"), timeZone });
          const main = document.querySelector("main");
          main.innerHTML = "<h1>You're booked</h1><p></p><p></p><p><a>Change or cancel</a></p>";
          main.querySelectorAll("p")[0].textContent = `${appointment.service.name} with ${appointment.staff ?? "our team"}, ${dateOf(appointment.startsAt)} at ${timeOf(appointment.startsAt)}.`;
          main.querySelectorAll("p")[1].textContent = `Your reference is ${appointment.reference}. We've sent the details to ${appointment.attendee.email}.`;
          main.querySelector("a").href = appointment.manageUrl;
        } catch (error) {
          show(error.message);
          if (error.status === 409) await choose(service);
        }
      });

      const { services } = await call("/services");
      $("services").append(...services.map((entry) => {
        const item = document.createElement("li");
        item.append(button(`${entry.name}, ${entry.durationMinutes} minutes`, () => choose(entry)));
        return item;
      }));
    </script>
  </body>
</html>
```

What happens:

1. `GET /services` lists the services as buttons.
2. `GET /slots` asks for seven days of free times in the visitor's time zone, already grouped by their days.
3. `POST /holds` keeps the chosen time for 10 minutes while the visitor types. A `409` means someone else took it a moment ago, so the free times load again. For a service paid online when it's booked, the answer is `409` with the code `payment_required`, and the visitor goes to its `bookingUrl` on the booking page to book and pay.
4. `POST /appointments` books with the hold's token. Booking Orbit emails the confirmation and the reminders, and `manageUrl` is the visitor's page to change or cancel.

Text from the API is set with `textContent`, never as HTML.

## 5. Try it

Open `http://localhost:8080/book.html`, choose a service, pick a time and book. The booking appears under **Calendar** in Booking Orbit. Cancel it with the **Change or cancel** link when you're done.

If booking fails with "This website isn't allowed to book here", the address in your browser isn't among the allowed websites. `http://localhost:8080` and `http://127.0.0.1:8080` are different addresses.

## Make it yours

- **Ask what the location asks:** `GET /` says whether a phone number and notes are `required`, `optional` or `hidden`, and `GET /services/<service>` returns the form's questions. See the [public booking API](https://docs.bifrostorbit.com/booking/developers/public-api/).
- **Paid services:** The API doesn't take payments, so the form sends visitors to the booking page for services paid online, as above. To keep them on your page, use the widget from part 1.
- **Using npm?** The [Booking SDK](https://docs.bifrostorbit.com/booking/developers/booking-sdk/) wraps these calls with types and errors.
