# Efficient reads for a CRAD integration

These are optional patterns for developers building an admin or booking screen. CRAD maintains API and database performance for all integrations. You can adopt individual patterns with your existing server key when they fit your interface and its current grants.

## Open a calendar or customer card

For a calendar view, request the visible interval with `GET /v1/management/appointments`. For one customer's history, add their `businessCustomerId`. This is the business contact ID. An integration mapping ID identifies a different resource. The caller needs `appointments:read` and a calendar grant covering the requested resources.

This example runs on the business's backend after it authorizes the staff member for the selected business, customer and calendar. `from` and `to` are ISO timestamps with offsets, at most 93 elapsed days apart. Use the credentials and provider filter for that authorized calendar.

```js
async function readAppointmentPage({ apiKey, businessCustomerId, providerId, from, to, cursor }) {
  const url = new URL('https://api.crad.ai/v1/management/appointments');
  url.search = new URLSearchParams({ from, to, limit: '20' }).toString();
  if (businessCustomerId !== undefined) url.searchParams.set('businessCustomerId', businessCustomerId);
  if (providerId !== undefined) url.searchParams.set('providerId', providerId);
  if (cursor) url.searchParams.set('cursor', cursor);

  const response = await fetch(url, {
    headers: { Authorization: `Bearer ${apiKey}` },
    cache: 'no-store',
  });
  if (!response.ok) throw new Error(`Calendar request failed (HTTP ${response.status})`);
  return response.json();
}
```

Use `providerId=unassigned` for the unassigned calendar. Omitting `providerId` selects all resources allowed by the key's calendar grant. Show the first page when it arrives. When `hasMore` is true, pass `nextCursor` with the same interval, filters and limit. Start a fresh page sequence when any of those change. Appointments are ordered by increasing start time. A limit of four returns the first four matches in that interval.

The appointment can already include the basic customer fields when `contacts:read` is granted. A list can use those returned fields without an additional customer lookup for every row. A detail screen can fetch `GET /v1/management/customers/{businessCustomerId}` when it needs the full basic card. If history loads separately, distinguish an unfinished page or a failed request from an empty result. [Calendar API](https://crad.ai/en/developers/website-booking/api/listbusinessappointments/) · [Customer card API](https://crad.ai/en/developers/website-booking/api/getbusinesscustomer/)

## Show recent and upcoming appointments on a customer card

On the business backend, authorize the staff member and use the existing Management key with `appointments:read` and a calendar grant. Put the customer’s business contact ID (`businessCustomerId`) in the path. Choose the counts for each screen.

`GET /v1/management/customers/{businessCustomerId}/appointment-overview`

| Screen | Query |
| --- | --- |
| Customer card | `?recentLimit=4&upcomingLimit=1` |
| Last ten completed treatments with one provider | `?recentLimit=10&upcomingLimit=0&recentStatus=COMPLETED&providerId={providerId}` |
| Next three appointments | `?recentLimit=0&upcomingLimit=3` |

Defaults are four recent and one upcoming. Each limit accepts an integer from 0 to 20. Zero skips the corresponding database read and omits the section. Request at least one section. Omit `providerId` to read across all calendars the key can access, or use `unassigned` when the unassigned calendar is authorized.

Both lists share one cutoff, `asOf`. `recent.items` contains appointments whose scheduled end is at or before the cutoff, most recent first. `upcoming.items` shows active appointments in the current time window first, followed by the next scheduled appointments. A future cancellation is excluded from upcoming. Show “awaiting clinic approval” only when `status=PENDING` and `approvalRequired=true`. Use the returned treatment and provider names when available. A section’s `hasMore: true` can link to the full calendar for the same customer under the same grants. [Overview reference and example response](https://crad.ai/en/developers/website-booking/api/getbusinesscustomerappointmentoverview/)

### Load more history on the same customer card

Request a cursor on the first page when the screen offers “Show more”. Read `recent.nextCursor` when present and pass it as `recentCursor` on the same customer path. This example displays four appointments initially and ten more on the next click.

```text
?recentLimit=4&upcomingLimit=1&includeRecentCursor=true
?recentCursor={cursor}&recentLimit=10
```

The second request returns only `recent`. Choose a new `recentLimit` from 1 to 20. The server restores `asOf` and both the provider and status filters from the cursor and rechecks access. Pass just the cursor and desired page size. Paging ends when `recent.nextCursor` is absent. Changes to appointments during paging can affect results.

The cursor expires 30 minutes after paging starts. On `410 CURSOR_EXPIRED`, start again. On `409 PROJECTION_CHANGED`, reload the first page under the current grant. On `403 SCOPE_REQUIRED`, stop paging. A regular request keeps only `items` and `hasMore` in the section.

## Share an in-flight read within one screen

If two components need the same read at the same time, their authorized request handler can start one promise and pass it to both. Share only when business, staff access, server credential, resource, filters, cursor and response options match. Let each authorization context start its own request. After a mutation, obtain the current version before the next edit. A failed or completed promise can be discarded. Booking availability, permissions, consent and connection readiness are evaluated again when an action uses them.

## Load WhatsApp templates when someone wants to choose one

An inbox can open a retained conversation first and request `GET /v1/management/conversations/{conversationId}/templates` when the user opens the template picker. This keeps catalogue loading off the initial screen for users who just want to read. This operation requires `conversations:read` and `conversations:send`. Use the catalogue for the selected conversation and current language. Sending still uses the current permission, connection, template and recipient eligibility. [Conversation template API](https://crad.ai/en/developers/website-booking/api/listbusinessconversationtemplates/)

## Book a slot and handle a lost response

In a **staff admin**, `POST /v1/management/appointment-availability` returns slots and the quote for the chosen customer, provider, service, options and local date. If your screen has already received that quote for the current selection, it can use its `quoteVersion` when creating the appointment. Request a fresh quote when the selection changes or the quote expires. A screen that displays a price before a date has been chosen can still use the separate quote operation. The public customer booking flow has its own verification, intent and commit sequence.

For an admin creation, persist one `Idempotency-Key` with the exact create command. If the response is lost, look up that operation, and follow its documented recovery procedure before treating the result as final. A displayed slot remains subject to CRAD's availability check at creation. [Staff availability](https://crad.ai/en/developers/website-booking/api/getbusinessappointmentavailability/) · [Create appointment](https://crad.ai/en/developers/website-booking/api/createbusinessappointment/) · [Look up the operation](https://crad.ai/en/developers/website-booking/api/getbusinessappointmentoperation/)

## Measure the experience your users see

Measure time from a click to the first useful result, then time until the full list is ready. Record the operation name, HTTP status, request count, duration and CRAD request ID when available. Compare like screens and workloads, including slow requests. Keep keys, authorization headers, phone numbers, patient data and response bodies out of performance logs.
