# Yatoon Booking REST API

Namespace: `yatoon/v1`
Base URL: `https://example.com/wp-json/yatoon/v1`

This is a **server-side automation API**, not a public booking API. Every route
requires an authenticated WordPress user who holds a Yatoon capability. There is
no anonymous access and no API key of Yatoon's own.

Introduced piecemeal across 4.x; this document first shipped with 4.7.1, together
with `GET /availability`.

---

## Authentication

Use either of WordPress's own mechanisms.

**Application Password** (recommended for server-to-server):

```bash
curl -u 'wp_user:xxxx xxxx xxxx xxxx xxxx xxxx' \
     'https://example.com/wp-json/yatoon/v1/services'
```

**Cookie + nonce** (only from inside wp-admin JavaScript):

```js
fetch( '/wp-json/yatoon/v1/bookings', { headers: { 'X-WP-Nonce': wpApiSettings.nonce } } )
```

### Capabilities

| Route group | Required capability |
|---|---|
| `bookings`, `orders/*/refunds` | `yatoon_manage_bookings` or `manage_options` |
| `availability` | `yatoon_manage_bookings`, `yatoon_manage_services` or `yatoon_manage_staff` |
| `services`, `locations` (read) | `yatoon_manage_bookings` or `yatoon_manage_services` |
| `services`, `locations` (write) | `yatoon_manage_services` or `manage_options` |
| `staff` (read) | `yatoon_manage_bookings` or `yatoon_manage_staff` |
| `staff` (write) | `yatoon_manage_staff` or `manage_options` |
| `customers` | `yatoon_manage_bookings` or `manage_options` |

Administrators receive all Yatoon capabilities on activation.

---

## Availability

### `GET /availability`

Free start times for one service. This is the same calculation the booking form
performs, not a parallel implementation: staff qualification, business hours,
staff schedules, breaks, closed dates, per-service buffers, minimum lead time,
the booking horizon, employee-specific durations and existing appointments are
all applied.

| Parameter | Required | Description |
|---|---|---|
| `date` | yes | Local calendar date, `Y-m-d`. |
| `service_id` | yes | Service to check. |
| `staff_id` | no | Omit or `0` for any qualified staff member. |
| `days` | no | Consecutive days to return, `1`–`31`. Default `1`. |
| `duration` | no | Override the service duration, in minutes. |

```bash
curl -u 'user:app-password' \
  'https://example.com/wp-json/yatoon/v1/availability?date=2026-09-10&service_id=4&days=3'
```

```json
{
  "service":  { "id": 4, "name": "Gel Manicure", "duration": 60 },
  "staff_id": 0,
  "timezone": "America/Indianapolis",
  "days": [
    {
      "date": "2026-09-10",
      "slots": [
        { "start": "2026-09-10 09:00:00", "display": "9:00 AM" },
        { "start": "2026-09-10 09:30:00", "display": "9:30 AM" }
      ]
    }
  ]
}
```

`start` is a local datetime in the site's timezone, in the exact form
`POST /bookings` expects. An empty `slots` array means the day is closed, fully
booked, outside the booking horizon, or past the lead-time cutoff — the endpoint
does not distinguish between those.

**Availability is not a reservation.** A slot returned here can be taken before
you post the booking. Booking creation re-checks availability server-side and
rejects a conflict, so treat that rejection as the authority.

---

## Bookings

| Method | Route | Purpose |
|---|---|---|
| `GET` | `/bookings` | List. `page`, `per_page` (max 100), plus filters. |
| `POST` | `/bookings` | Create. Availability is re-validated server-side. |
| `GET` | `/bookings/{id}` | Read one. |
| `PUT` / `PATCH` | `/bookings/{id}` | Update. |
| `DELETE` | `/bookings/{id}` | Cancel. Cancels; it does not erase the row. |

`POST /orders/{id}/refunds` issues a refund through the payment provider
configured under Payments.

---

## Directory

The four directory entities behave identically:

`services` · `staff` · `locations` · `customers`

| Method | Route |
|---|---|
| `GET` | `/{entity}` — supports `page`, `per_page`, `search` |
| `POST` | `/{entity}` |
| `GET` | `/{entity}/{id}` |
| `PUT` / `PATCH` | `/{entity}/{id}` |

Writable fields per entity:

- **services** — `square_id`, `name`, `description`, `duration_minutes`, `price`,
  `estimated_cost`, `category`, `image_url`, `is_active`, `is_addon_only`,
  `require_policy`, `require_consent`, `allow_staff_select`,
  `allow_design_upload`, `buffer_before_min`, `processing_time_min`,
  `cleanup_time_min`, `buffer_after_min`, `min_lead_time_min`, `sort_order`,
  `is_featured`
- **staff** — `square_id`, `name`, `email`, `phone`, `bio`, `image_url`,
  `calendar_color`, `is_bookable`, `is_active`, `sort_order`, `portal_role`,
  `portal_permissions`
- **locations** — `square_id`, `name`, `address`, `phone`, `timezone`, `is_active`
- **customers** — `customer_name`, `customer_email`, `customer_phone`, `notes`,
  `allergies`, `preferences`, `preferred_staff`, `tags`

`search` matches: service name/description/category · staff name/email/phone ·
location name/address/phone · customer name/email/phone.

---

## Errors

Standard WordPress REST error envelopes.

| Code | Status | Meaning |
|---|---|---|
| `rest_forbidden` | 401 / 403 | Not authenticated, or missing the capability. |
| `yatoon_service_not_found` | 404 | No service with that id. |
| `yatoon_bad_date` | 400 | `date` is not a `Y-m-d` calendar date. |
| `yatoon_unavailable` | 503 | Availability code is not loaded on this site. |

---

## Notes and limits

- Employee-specific durations (4.7.1) apply automatically when `staff_id` is
  given. With `staff_id=0` the service's own duration is used, so a slot offered
  for "any staff" can be longer or shorter once a specific technician is chosen.
- The REST module can be switched off per site under Feature Modules. When it is
  off the routes are not registered at all.
- Rate limiting is not applied to authenticated routes; a runaway integration
  can generate real database load. Cache `availability` responses at your end
  when polling.
