- Developer home
- Samples
- The Acme Contracts example
The Acme Contracts example
A complete partner app embedding MR Office's editors: page, token minting, file serving and a verified save-callback receiver, in one Node file.
What it is
Acme Contracts is a fictional contract-management app that embeds MR Office's editors through the public SDK. It is one dependency-free Node file and one page: it serves its page, mints session tokens, serves its documents to MR Office and receives the signed save callbacks, keeping each new version.
The four parts
<!-- public/index.html: the contract page of "Acme Contracts" -->
<div id="editor" style="height: 80vh"></div>
<script src="https://mroffice.ai/embed/v1/embed.js"></script>
<script>
const getToken = () =>
fetch("/token", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ contract: "42", file: "service-agreement.docx", user: { id: "u-7", name: "Alex" } }),
}).then((r) => r.json()).then((b) => b.token);
const editor = MROffice.open({
container: "#editor",
token: getToken,
refreshToken: getToken,
locale: "en",
onReady: (e) => console.log("open:", e.document.name),
onSave: (s) => console.log("saved v" + s.version),
onError: (err) => alert(err.message),
});
</script>// POST /token — Acme checks who is signed in, then signs a token for one file
if (request.method === "POST" && url.pathname === "/token") {
const body = JSON.parse((await readBody(request)).toString("utf8") || "{}");
const file = basename(String(body.file || ""));
if (!SAFE_NAME.test(file)) return json(response, 400, { error: "unknown file" });
const token = mintSessionToken({
fileKey: `acme-${String(body.contract).replace(/[^A-Za-z0-9-]/g, "")}`,
fileName: file,
fileUrl: `${PUBLIC_URL}/files/${encodeURIComponent(file)}`,
userId: String(body.user?.id || "user").slice(0, 64),
userName: String(body.user?.name || "Guest").slice(0, 64),
mode: body.mode === "view" ? "view" : "edit",
});
return json(response, 200, { token });
}// GET /files/<name> — MR Office's servers fetch the document from here
if (request.method === "GET" && url.pathname.startsWith("/files/")) {
const name = decodeURIComponent(url.pathname.slice("/files/".length));
if (!SAFE_NAME.test(name)) return json(response, 404, { error: "not found" });
const bytes = await readFile(join(FILES_DIR, name));
response.writeHead(200, { "content-type": TYPES[name.split(".").pop()], "content-length": bytes.length });
response.end(bytes);
return;
}// POST /callback — verify, store each event once, keep the new version
if (request.method === "POST" && url.pathname === "/callback") {
const raw = await readBody(request);
if (!verifySignature(raw, request.headers["mro-signature"], WEBHOOK_SECRET)) {
return json(response, 401, { error: "bad signature" });
}
const event = JSON.parse(raw.toString("utf8"));
if (seenEvents.has(event.id)) return json(response, 200, { ok: true, duplicate: true });
seenEvents.add(event.id);
if (event.type === "document.saved") {
const file = await fetch(event.download_url); // valid for an hour
const bytes = Buffer.from(await file.arrayBuffer());
const partnerVersion = `acme-${event.document.key}-v${event.document.version}`;
versions.unshift({ ...event.document, partnerVersion, bytes });
return json(response, 200, { version: partnerVersion }); // the next tokens' file.version
}
return json(response, 200, { ok: true });
}Run it
- Make an app in the consoleAdd your page's origin (http://localhost:4400 for local development) and set the callback URL to PUBLIC_URL + /callback.
- Start the serverWith the app's ID, secret key and webhook key in the environment.
- Open http://localhost:4400Pick a contract: the editor opens it, and each save arrives at /callback as a new version.
MROFFICE_APP_ID=app_… MROFFICE_SECRET_KEY=mro_sk_… MROFFICE_WEBHOOK_SECRET=mro_whsec_… \
MROFFICE_ORIGIN=https://mroffice.ai PUBLIC_URL=https://contracts.example \
node server.mjs # Acme Contracts on http://localhost:4400PUBLIC_URL is where MR Office's servers fetch the files: public https in production; local development may use http://localhost.