SDK reference
@mrcharles/sdk is zero-dependency TypeScript, shipped as ESM (import), as an IIFE for <script> (window.MrCharles) and with full types. About 11 kB gzipped. The usage guide is JavaScript SDK; this page is the surface.
createClient(config?) → MrCharlesClient
Section titled “createClient(config?) → MrCharlesClient”| Option | Type | Default | Notes |
|---|---|---|---|
env | 'production' | 'production' | The only public environment; kept for forward compatibility. |
apiBase, appBase, sitesBase | string | production hosts | Endpoint overrides for tests and proxies. |
fetch | typeof fetch | globalThis.fetch | Server runtimes, tests, custom agents. |
locale | string | site default | Any BCP-47 tag. Drives lang on links/embeds, the hold locale (mapped to es/ca/en), Accept-Language, t() and format. |
headers | Record<string,string> | — | Extra headers on every request, e.g. X-Forwarded-For from your server. |
client.sites
Section titled “client.sites”| Method | Returns | Endpoint |
|---|---|---|
get(slug) | PublicSite | GET /public/sites/{slug} |
resolve(host) | PublicSite | GET /public/sites/resolve?host= |
availability(slug, roomId, { from, to }) | RoomAvailability | GET /public/sites/{slug}/rooms/{roomId}/availability |
quote(slug, roomId, { date, startTime, durationMinutes, groupSizeId?, partySize?, locale? }) | Quote | POST /public/sites/{slug}/rooms/{roomId}/quote |
client.holds
Section titled “client.holds”| Method | Returns | Endpoint |
|---|---|---|
create(slug, HoldRequest) | Hold | POST /public/sites/{slug}/holds |
verify(holdId, code) | Hold | POST /public/holds/{id}/verify |
resend(holdId) | Hold | POST /public/holds/{id}/resend |
confirm(holdId, { paymentMode, notes?, email? }) | Confirmation | POST /public/holds/{id}/confirm |
client.bookings
Section titled “client.bookings”| Method | Returns | Endpoint |
|---|---|---|
get(manageToken) | Booking | GET /public/bookings/manage/{token} |
cancel(manageToken, reason?) | Booking | POST /public/bookings/manage/{token}/cancel |
client.teachers
Section titled “client.teachers”| Method | Returns |
|---|---|
profile(instructorUserId) | TeacherProfile |
offers(instructorUserId) | LessonOffer[] (published only) |
offer(offerId) | LessonOffer |
windows(instructorUserId, { from, to }) | TeacherWindows |
policy(instructorUserId) | LessonPolicy |
slots({ instructorUserId, offerId, date, days? }) | LessonSlots |
client.links
Section titled “client.links”| Method | Builds |
|---|---|
site(slug) | https://{slug}.sites.mrcharles.app/ |
bookRoom({ slug, roomId, date?, start?, durationMinutes?, lang?, embed? }) | the hosted booking screen, prefilled |
manage(slug, manageToken) | the guest’s manage page |
bookLesson({ instructorId, offerId? }) | the app’s lesson booking screen |
teacherProfile(instructorUserId) | the teacher’s profile in the app |
client.mountRoomBooking(target, options) → EmbedHandle
Section titled “client.mountRoomBooking(target, options) → EmbedHandle”target is a selector or element. Options are RoomLinkOptions plus onBooked({ reference, manageUrl }). The iframe resizes itself and only accepts messages from the hosted site’s origin. handle.destroy() removes it.
Languages on the client
Section titled “Languages on the client”| Member | What |
|---|---|
locale / setLocale(tag) | the client’s language (undefined = each site’s default) |
hostedLocale | what the hosted screen and messages will use: es, ca or en |
t(key, params?) | a UI string: mc.t('hint.resendsLeft', { n: 2 }) |
errorMessage(err) | a guest-facing sentence for any error |
format | price, date, time, timeRange, duration, weekday, countdown |
pick(localized, default?) | one language out of { es, ca, en } |
SDK strings exist for es, ca, en, fr, pt, it, de (LOCALES); the hosted screen for es, ca, en (HOSTED_LOCALES). Standalone: t, messageFor, translator, normalizeLocale, toHostedLocale, intlTag, createFormatter, MESSAGES.
client.roomBooking(options) → RoomBooking
Section titled “client.roomBooking(options) → RoomBooking”The headless flow. options: slug, roomId, onChange(state), locale?. The state carries locale and errorMessage (the last error, translated); flow.t() and flow.setLocale() match the client’s.
| Method | Effect on state.step |
|---|---|
loadSite() | → pick (room, policy, captcha and Stripe keys on state.site) |
loadAvailability({ from?, days? }) | fills state.days |
startTimesFor(date, durationMinutes) / durationsFor(date, start) | pure helpers on the loaded availability |
rateAt(date, start, groupSizeId?) | hourly rate hint at a start time, from the loaded availability |
quote({ date, startTime, durationMinutes, groupSizeId?, partySize? }) | none; resolves the itemised price (labels in the flow’s language) |
hold(details) — details.groupSizeId?, details.partySize? | → verify (state.hold, state.hold.devCode in sandbox) |
verify(code) / resend() | → confirm |
confirm({ paymentMode, notes?, email? }) | → done (state.confirmation: reference, manageUrl, clientSecret for card) |
reset() | → pick |
Errors
Section titled “Errors”MrCharlesError has code (the API’s error.code), status, requestId and three predicates:
slotIsGone—SLOT_TAKEN,OUTSIDE_HOURS,ROOM_NOT_BOOKABLE: offer another time.holdIsDead—HOLD_EXPIRED,HOLD_NOT_FOUND,TOO_MANY_ATTEMPTS: start over.isTransient—RATE_LIMITED, network, 5xx: wait, then retry once.
isMrCharlesError(e) narrows unknown errors.
Helpers
Section titled “Helpers”startTimes, durationOptions, toMinutes, toHHMM, addMinutes, formatPrice, dateKey, addDays, uuid, isTrustedOrigin, parseEmbedEvent, resolveEndpoints, ENVIRONMENTS, VERSION. Embed handles have setLang(tag).
Everything is exported from the package root: PublicSite, PublicRoom, RoomAvailability, Hold, HoldRequest, Confirmation, Booking, TeacherProfile, LessonOffer, LessonSlots, RoomBookingState, RoomStep, EmbedOptions, ErrorCode, Locale…
Versioning
Section titled “Versioning”Semver. Minor versions add; majors may rename. The CDN path /sdk/v0/ tracks the latest 0.x; pin the npm version in production builds.