MSM customer SDK (@msm-platform/customer-sdk)
A typed client for MSM's customer API: sign-in, public business reads, reservations and bookings. Generated from the engine's customer contract with openapi-typescript, on top of openapi-fetch 0.17.0. Versions are 0.x until engine 1.0.0; a minor version can break. CHANGELOG.md in the package lists each version's changes.
npm install @msm-platform/customer-sdk
Two modes
Server mode is for your own server. Give it your application client's ID and secret: the SDK sends them as HTTP Basic (Authorization: Basic base64(clientId:clientSecret)) on every call, so there is no token to fetch, cache or renew. It refuses to run where window exists, so a secret bundled into browser code throws before any request.
import { createCustomerClient } from "@msm-platform/customer-sdk";
const msm = createCustomerClient({
baseUrl: process.env.MSM_ENGINE_URL!, // https://… without a trailing /web
clientId: process.env.MSM_CLIENT_ID!,
clientSecret: process.env.MSM_CLIENT_SECRET!,
});
Proxy mode is for a browser or any code that must not hold the secret. Give it only a base URL and, if needed, a custom fetch, and put a proxy on your server in front of the engine that adds the credentials. MSM's customer screens (@msm-platform/customer-react) work this way, behind their server half on the partner's server.
const msm = createCustomerClient({ baseUrl: "/msm", fetch: myFetch });
Sessions
A customer signs in with a code sent by SMS. The session token is yours to store, for example in an HttpOnly cookie, and you pass it on each call that acts as the customer:
import { sessionHeaders } from "@msm-platform/customer-sdk";
// The Turnstile token the screens' widget gave the customer's browser, and the
// customer's address as your own trusted proxy reports it.
const { data: started } = await msm.POST("/web/identity/initiateLogin", {
body: { phone: "+3545550004", botCheckToken, clientIp },
});
const { data: login } = await msm.POST("/web/identity/customerLogin", {
body: { challenge: started!.challenge, code: "123456" },
});
// A phone with no customer yet gets a registration ticket instead of a session.
const session =
login!.result === "session"
? login!
: (
await msm.POST("/web/identity/completeRegistration", {
body: {
registrationTicket: login!.registrationTicket,
...(login!.nameRequired && { name: "Kata" }),
},
})
).data;
const headers = sessionHeaders(session!.sessionToken);
const { data: reservation } = await msm.POST(
"/web/businesses/{businessId}/reservations",
{ params: { path: { businessId } }, body: { eventId }, headers },
);
const { data: booking } = await msm.POST(
"/web/businesses/{businessId}/reservations/{reservationId}/confirm",
{ params: { path: { businessId, reservationId: reservation!.id } }, headers },
);
The engine sends a code only after checking botCheckToken with the Turnstile widget MSM owns for your website (403 BotCheckFailed, or 503 BotCheckUnavailable when it cannot) and counting the request against clientIp (by default 5 code requests per address per 10 minutes, then 429 TooManyAttempts with Retry-After; 503 LimiterUnavailable when it cannot count). Never take clientIp from a header the customer can set, such as the leftmost X-Forwarded-For entry. Every phone gets a code, so the answer never tells whether it has an account. With the right code, customerLogin answers { result: "session", sessionId, sessionToken, expiresAt } for a customer, or { result: "registrationRequired", registrationTicket, nameRequired, registrationExpiresAt } when the phone has no customer yet. Then POST /web/identity/completeRegistration with { registrationTicket, name? } returns the session. Send name when nameRequired is true: without it the engine answers 422 NameRequired and the ticket stays usable for a retry with the name. A name sent when it isn't needed is ignored, and the person keeps theirs. The ticket is used once, with your client, within 10 minutes; otherwise it is 401 NotAuthenticated, as a wrong code is. POST /web/identity/logout with { sessionToken } ends a session of your client. Sign-in, registration and logout are on /web and need your client's credentials, so call them from server mode or through your proxy; /api is not for website clients. A reservation holds the seat or slot for a few minutes; confirming it makes the booking.
The same session reads the customer's own details, bookings and passes with every business of your partner, and ends all their sessions with your client:
const { data: me } = await msm.GET("/web/me", { headers });
const { data: upcoming } = await msm.GET("/web/me/bookings", {
params: { query: { period: "upcoming" } },
headers,
});
const { data: passes } = await msm.GET("/web/me/memberships", { headers });
await msm.PATCH("/web/me", { body: { name: "Kata Jónsdóttir" }, headers });
await msm.DELETE("/web/me/sessions", { headers });
Each booking carries its business's name, its event and cancellableUntil and lateCancellationFrom; each pass its creditsRemaining, null when unlimited. Phone and email are read-only.
Errors
Every non-2xx response throws CustomerApiError with status, the engine's stable code, message, retryAfterSeconds (from Retry-After on 429 and 503), issues (the failed input checks on ValidationFailed, each {path, message} with path a JSON Pointer into the request such as /body/eventId), the parsed body and the original response. isCustomerApiError(error, method, path) is true only for that operation's errors and narrows code to the codes the contract declares for it, so TypeScript checks the cases you handle:
import { isCustomerApiError } from "@msm-platform/customer-sdk";
try {
await msm.POST("/web/businesses/{businessId}/reservations", request);
} catch (error) {
if (isCustomerApiError(error, "post", "/web/businesses/{businessId}/reservations")) {
switch (error.code) {
case "EventFull":
case "SlotTaken":
return "Pick another time";
}
}
throw error;
}
Until the engine declares its codes per operation, code is a string; the cases then compile without the check.
The SDK never retries a request: a 401 throws, and the next call sends the same credentials. Writes are not idempotent yet: retry a reservation only when you know the first attempt did not reach the engine.
Next.js server example
examples/customer-nextjs in the repository is a Next.js App Router site, the partner starter. It serves MSM's customer screens for sign-in and the customer's account, and its own pages use the SDK in server mode with the session the screens signed in (read from their encrypted HttpOnly cookie): the business's availability, and booking (reserve, then confirm) with server actions. The SDK and the secret live only in server code (lib/msm.ts):
// lib/msm.ts, imported only by server components and server actions
export const msm = createCustomerClient({
baseUrl: process.env.MSM_ENGINE_URL!,
clientId: process.env.MSM_CLIENT_ID!,
clientSecret: process.env.MSM_CLIENT_SECRET!,
});
// app/actions.ts
("use server");
export async function book(form: FormData) {
const sessionToken = (await cookies()).get("msm-session")?.value;
const headers = sessionHeaders(sessionToken!);
const { data: reservation } = await msm.POST(
"/web/businesses/{businessId}/reservations",
{
params: { path: { businessId } },
body: { eventId: String(form.get("eventId")) },
headers,
},
);
await msm.POST(
"/web/businesses/{businessId}/reservations/{reservationId}/confirm",
{
params: { path: { businessId, reservationId: reservation!.id } },
headers,
},
);
}
To run it against a local engine, see the example's README.
Inside msm-platform
pnpm generate writes src/generated/api.ts from the customer contract, contracts/openapi/customer.json, and pnpm generate:check fails when it is stale. Never edit it by hand. Publishing is manual and only Daníel does it: docs/runbooks/customer-sdk.md. A pull request that changes this package or the customer contract adds an entry to its CHANGELOG.md, or says Web impact: none in its description.