Appointments
Read availability and submit visitor booking requests for workspaces running the Brandfine Appointments plugin.
bf.appointments is the server-side SDK surface for the
Appointments plugin. Once a workspace activates Appointments in
the cms (Sidebar → Plugins → Appointments → Activate), this namespace
lets your backend code read availability and submit visitor
requests on their behalf.
import { createBrandfineClient } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: process.env.BRANDFINE_API_KEY!,
})
// 1. Load slots for the next ~14 days (or whatever booking window
// the workspace configured), with the page's language: `locale`
// in the answer says which language to render your booking UI in.
const { slots, days, requestPrecision, requestChoices, timezone, locale } =
await bf.appointments.getAvailability({ locale: pageLocale })
// 2. Show what the workspace asks visitors to pick (requestPrecision):
// exact times from `slots`, parts of days from `days[].parts`, or
// whole days from `days`, up to `requestChoices` options. Submit
// them as `preferences` with the page's locale (the visitor's
// emails use it) and, if you like, a few labelled values for the
// team. The team sets the exact time when it confirms.
await bf.appointments.createRequest({
visitorName: 'Visitor Name',
visitorEmail: 'visitor@example.com',
preferences: [{ time: slots[0].start }],
// or: [{ date: days[0].date, part: 'morning' }], or [{ date: days[0].date }]
locale: pageLocale,
metadata: { Treatment: 'Implant consultation' },
})For anonymous visitors that's the whole flow — status changes
(approve / decline / reschedule) reach them by email. For
authenticated sites there's more: sign the booking with a
verified identity and the same person
can read their own status back with list / get, and spend the
returned cancellationToken with cancel().
Wraps the external Appointments REST endpoints. Same shapes, same validation, same auth.
Which key
The site key (brandfine_pk_…) does everything here, from your server
or straight from the browser: it can't write anything or read anything
private. The one server-only part is signing a
verified identity with the identity
secret, which never goes in a page. Without one, bookings are simply
anonymous. See Static sites for a site
with no server.
API
getAvailability(opts?) → Promise<AppointmentAvailability>
type GetAvailabilityOptions = {
/** Optional clamp inside the workspace's configured booking
* window. Server-side ignores ranges outside the window — pass
* these to narrow the response, not to widen it. */
from?: Date | string
to?: Date | string
/** The page's locale (your i18n router's active one). */
locale?: string
}
type AppointmentAvailability = {
/** False = plugin not activated for this workspace, or the
* customer toggled "Accepting bookings" off in cms settings.
* UI should render a "not accepting bookings right now" state
* rather than throw. */
enabled: boolean
/** Source-of-truth IANA timezone for the workspace's hours
* (e.g. "Europe/Istanbul"). Render slots in the visitor's
* local TZ; show this as the "(local time: HH:MM)" subtext. */
timezone: string
slotDurationMinutes: number
leadTimeHours: number
bookingWindowDays: number
policyText: string | null
/** What a visitor picks: 'time' (an exact slot from `slots`),
* 'part' (a part of a day from `days[].parts`) or 'day' (a day
* from `days`), and how many options they may give (1–3). */
requestPrecision: 'time' | 'part' | 'day'
requestChoices: number
/** UTC ISO 8601 slot starts + ends, ordered chronologically. */
slots: Array<{ start: string; end: string }>
/** The days that still have a free slot. `date` is the workspace's
* local YYYY-MM-DD (send it back to book); `parts` are the parts
* of the day still free — morning (opening–12:00), midday
* (12:00–14:00), afternoon (14:00–closing), in its timezone. */
days: Array<{
date: string
start: string
end: string
parts: Array<{ part: 'morning' | 'midday' | 'afternoon'; start: string; end: string }>
}>
windowStart: string
windowEnd: string
/** The language to render your booking UI in: the `locale` you
* sent when the workspace has it, "en" when it doesn't, the
* workspace's default locale when you sent none. */
locale: string
}Throws BrandfineApiError on
non-2xx. 401 = invalid X-Api-Key.
createRequest(input) → Promise<CreatedAppointmentRequest>
type CreateAppointmentRequestInput = {
visitorName: string // required, 1–120 chars
visitorEmail: string // required, validated server-side
visitorPhone?: string // optional, up to 40 chars
visitorMessage?: string // optional, up to 2,000 chars
/** 1 to `requestChoices` options, each one of:
* { time } an exact slot start from `slots`
* { date, part } a part of a day from `days[].parts`
* { date } a whole day from `days`
* Each is checked against availability before accepting. */
preferences: Array<
| { time: string }
| { date: string; part: 'morning' | 'midday' | 'afternoon' }
| { date: string }
>
/** Optional cookie-derived session id from your frontend — an
* UNVERIFIED soft link across page loads. For real identity,
* use `visitor` below instead. */
visitorSessionId?: string
/** The page's locale. The visitor's emails (received, confirmed,
* rescheduled, declined) go out in it under the same rule as
* `getAvailability`'s `locale`. Absent → the workspace default. */
locale?: string
/** A few labelled values for the team (a chosen treatment, a room
* type): at most 10 keys of 1–40 chars, string values up to 200;
* anything else is a 400 naming `metadata`. Shown to the team as
* sent (inbox, the email to the business, MCP), never to the
* visitor. `{ Treatment: 'Implant consultation' }` */
metadata?: Record<string, string>
/** Signed host-app identity — same payload + signature Live
* Chat accepts (`liveChat.identityToken()` signs for both).
* Invalid or absent → the booking proceeds anonymously. */
visitor?: VerifiedVisitor
}
type CreatedAppointmentRequest = {
id: string
createdAt: string
/** The options as stored, sorted, in UTC. */
preferences: Array<{ kind: 'time' | 'part' | 'day'; start: string; end: string; part?: string }>
/** The start of the earliest option. */
requestedAt: string
durationMinutes: number
status: 'PENDING'
/** True iff visitor.identityToken HMAC-verified. */
identityVerified: boolean
/** One-time self-cancel credential — pass it to
* `appointments.cancel()` (or embed it in your own UI/emails).
* Cleared once any party acts on the request. */
cancellationToken: string | null
}Throws BrandfineApiError:
- 400 — validation failure (missing required field, invalid email, a malformed option, or more options than the workspace takes).
- 401 — missing or invalid
X-Api-Key. - 404 — workspace doesn't have Appointments activated, OR an option is no longer free (outside the window, or just taken).
err.body.messagedistinguishes the case; fetch availability again and let the visitor pick another. - 5xx — server error; usually transient, safe to retry on a backoff.
The server emails both the visitor ("request received") and the customer ("new request for X — review in inbox") immediately on success. Fire-and-forget on the server side — failures to send don't fail your request.
Verified visitor identity
If your site is authenticated, you already know who's booking — don't make them retype it, and don't trust free-text strings. Sign the person once, server-side, with the SAME helper Live Chat uses (one signature works for both plugins):
const identityToken = await bf.liveChat.identityToken(user.id)
await bf.appointments.createRequest({
visitorName: user.name, // still required (fallback contact)
visitorEmail: user.email,
preferences: [{ time: slot.start }],
visitor: {
externalId: user.id, // your stable id for the person
name: user.name, // verified values take precedence
email: user.email,
attributes: { plan: user.plan },
identityToken,
},
})A valid signature attributes the booking (identityVerified: true)
and links it to the same person's Live Chat conversations via the
shared externalId. An invalid or absent signature never
rejects the booking — it just proceeds anonymously (a broken
integration must not block a visitor).
The signature uses the workspace's identity secret (workspace
page → Access → Identity secret), the same one Live Chat checks, so
one token works for both. Pass it to the client as
createBrandfineClient({ identitySecret }) or set
BRANDFINE_IDENTITY_SECRET; it stays on your server.
list(identity) → Promise<Appointment[]>
Every appointment belonging to the verified person, newest first — the "where does my request stand?" surface for authenticated sites.
const appointments = await bf.appointments.list({
externalId: user.id,
identityToken: await bf.liveChat.identityToken(user.id),
})
// → [{ id, status: 'PENDING'|'CONFIRMED'|'REJECTED'|'CANCELLED',
// preferences, // what they asked for
// requestedAt, scheduledAt, // the team's time, once CONFIRMED
// durationMinutes, timezone,
// declineReason, // populated on REJECTED
// rescheduleCount, respondedAt, createdAt }]Reads require proof: a bad signature throws BrandfineApiError
401 (there is no anonymous downgrade for reading someone's
history). Sent as a POST so the token never lands in URLs or logs.
get(id, identity) → Promise<Appointment>
One appointment, identity-scoped. Throws 404 when the id doesn't exist OR belongs to someone else — indistinguishable by design.
cancel(cancellationToken) → Promise<{ id, status: 'CANCELLED' }>
Spends the one-time token from createRequest to cancel a
still-PENDING request. The token is the credential — no API key
needed. Throws 400 once the request is no longer PENDING,
404 for an unknown or already-spent token.
Static sites
Every call here works with the site key (brandfine_pk_…) from the
browser, so a fully static site (output: 'export', plain nginx)
shows slots and takes bookings without a server. Only signing a
verified identity needs one, because the identity secret never goes in
a page.
Framework recipes
Next.js (App Router)
Two route handlers — one for availability (GET), one for submit (POST). Frontend hits these, not Brandfine.
// app/api/appointments/availability/route.ts
import { NextResponse } from 'next/server'
import { createBrandfineClient } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: process.env.BRANDFINE_API_KEY!,
})
export async function GET() {
const availability = await bf.appointments.getAvailability()
return NextResponse.json(availability)
}// app/api/appointments/route.ts
import { NextResponse } from 'next/server'
import { createBrandfineClient, BrandfineApiError } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: process.env.BRANDFINE_API_KEY!,
})
export async function POST(req: Request) {
const body = await req.json()
// Validate body shape (zod recommended) before forwarding.
try {
const created = await bf.appointments.createRequest(body)
return NextResponse.json(created)
} catch (err) {
if (err instanceof BrandfineApiError && err.status === 404) {
return NextResponse.json({ error: err.body }, { status: 409 })
}
throw err
}
}Astro (server-rendered, no API roundtrip)
Render availability at request time, post submissions through an Astro API route.
---
// src/pages/book.astro
import { createBrandfineClient } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: import.meta.env.BRANDFINE_API_KEY,
})
const { slots, timezone, policyText } = await bf.appointments.getAvailability()
---
<form method="POST" action="/api/appointments">
<select name="slot" required>
{slots.map((s) => (
<option value={s.start}>{new Date(s.start).toLocaleString()}</option>
))}
</select>
<input name="visitorName" required />
<input name="visitorEmail" type="email" required />
<textarea name="visitorMessage"></textarea>
<button type="submit">Request appointment</button>
</form>
{policyText && <p>{policyText}</p>}// src/pages/api/appointments.ts
import type { APIRoute } from 'astro'
import { createBrandfineClient } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: import.meta.env.BRANDFINE_API_KEY,
})
export const POST: APIRoute = async ({ request }) => {
const form = await request.formData()
await bf.appointments.createRequest({
visitorName: String(form.get('visitorName')),
visitorEmail: String(form.get('visitorEmail')),
visitorMessage: String(form.get('visitorMessage') || ''),
preferences: [{ time: String(form.get('slot')) }],
})
return new Response(null, {
status: 303,
headers: { Location: '/book/thanks' },
})
}Remix (loader + action in one route)
// app/routes/book.tsx
import { json, type ActionFunctionArgs } from '@remix-run/node'
import { useLoaderData, Form } from '@remix-run/react'
import { createBrandfineClient } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: process.env.BRANDFINE_API_KEY!,
})
export async function loader() {
return json(await bf.appointments.getAvailability())
}
export async function action({ request }: ActionFunctionArgs) {
const form = await request.formData()
const created = await bf.appointments.createRequest({
visitorName: String(form.get('name')),
visitorEmail: String(form.get('email')),
preferences: [{ time: String(form.get('slot')) }],
})
return json(created)
}
export default function Book() {
const { slots } = useLoaderData<typeof loader>()
return (
<Form method="post">
<select name="slot">
{slots.map((s) => (
<option key={s.start} value={s.start}>
{new Date(s.start).toLocaleString()}
</option>
))}
</select>
<input name="name" required />
<input name="email" type="email" required />
<button type="submit">Book</button>
</Form>
)
}Generic Node server (Express, Hono, Fastify, Bun, Deno)
import { createBrandfineClient } from '@brandfine/client'
const bf = createBrandfineClient({
baseUrl: 'https://api.brandfine.co',
apiKey: process.env.BRANDFINE_API_KEY!,
})
app.get('/api/appointments/availability', async (_req, res) => {
res.json(await bf.appointments.getAvailability())
})
app.post('/api/appointments', async (req, res) => {
res.json(await bf.appointments.createRequest(req.body))
})Best practices
Render slots in the visitor's local timezone, not the workspace's. Use slot.start (UTC) + new Date(slot.start).toLocaleString(). Show the workspace's timezone as a subtle "(host's local time: HH:MM)" to avoid confusion across time zones. | |
| Cache availability for ~30 seconds at most. New CONFIRMED appointments invalidate the slot grid; stale data leads to double-booking attempts (the server rejects them, but the visitor's UX is worse). | |
| Don't retry on 404. The slot was likely taken between availability fetch and submit. Re-fetch availability and let the visitor pick again. | |
| Do retry on 5xx and network failures. Both are transient. Exponential backoff with a few attempts is fine. | |
Keep the identity secret on the server. The site key is fine in front-end variables (NEXT_PUBLIC_*, PUBLIC_*, VITE_*); the identity secret never is. | |
Surface policyText to visitors before they submit. The customer wrote it for a reason (cancellation rules, what to expect). |
CORS
The Appointments endpoints inherit the permissive /external/*
CORS policy — any origin can call them with
Content-Type: application/json + X-Api-Key. Practically:
your customers' visitors never hit Brandfine directly (server-
side only), so CORS rarely matters in v1.
Where appointment requests show up
Each request lands in the CMS Inbox as a booking, next to chats and forms, and on the schedule (sidebar → Plugins → Appointments), which aggregates every workspace in the active team. Confirm or Decline from either emails the visitor the outcome.
The per-workspace settings (business hours, slot duration, notification email, policy text) are under Inbox → Settings → Bookings, or the schedule's Settings button. Multiple workspaces can activate Appointments independently — each gets its own config and shows up as a chip on the catalog card.