Your server
Strict mode
Strict mode keeps every key out of your pages. Your page asks your own server for the editor's license; your server asks Dorvyn, with its secret key, for the user signed in to your app. It works like a payment provider's secret key: the part that costs something happens on your server.
It's recommended for every live site where people sign in, and it's required on the Organization plan.
What you need#
- An application in the dashboard with:
- a secret key (
dvn_sk_…) for your server, and - a publishable key of the same environment listing your app's domains. Strict mode never puts it in a page. Dorvyn only checks your pages' addresses against its list.
- a secret key (
- A server you control: Next.js, Express, NestJS, Hono, Remix, SvelteKit, a serverless function, or anything else that runs JavaScript (Node 18 or later, or an edge runtime).
- Your own sign-in, so your server knows who is asking.
For development, make both keys test keys and list http://localhost:3000 (or wherever your app runs) on the publishable one. For production, make live ones listing https://app.example.com.
1. Install#
npm install @dorvyn/serverIt has no dependencies. Never import it in code that runs in the browser. In Next.js, @dorvyn/next/server is the same package.
2. Set the secret key#
# Your server's environment only
DORVYN_SECRET_KEY=dvn_sk_live_…Keep it with your other server secrets (your host's environment settings, a secrets manager). Never commit it. Never give it a NEXT_PUBLIC_, VITE_ or PUBLIC_ prefix, or anything else your build puts into pages.
3. Add the route#
dorvynRoutes answers two requests under one path, …/grant and …/export. Mount it at a path ending in /:action.
Next.js (App Router):
// app/api/dorvyn/[action]/route.ts
import { dorvynRoutes } from "@dorvyn/next/server";
import { auth } from "@/auth";
export const POST = dorvynRoutes({
user: async () => (await auth())?.user?.id,
});Express (and NestJS on Express):
import express from "express";
import { dorvynRoutes, nodeHandler } from "@dorvyn/server";
// `user` gets a web Request, so read Express's own session from `req` here.
app.post("/api/dorvyn/:action", express.json({ limit: "250mb" }), (req, res) =>
nodeHandler(dorvynRoutes({ user: () => req.session?.userId }))(req, res),
);Hono, Cloudflare Workers, Deno, Bun, SvelteKit, Remix, or anything with web Request and Response:
const dorvyn = dorvynRoutes({ user: async (request) => (await getSession(request))?.userId });
// Hono
app.post("/api/dorvyn/:action", (c) => dorvyn(c.req.raw));
// SvelteKit: src/routes/api/dorvyn/[action]/+server.ts
export const POST = ({ request }) => dorvyn(request);Node's own http:
const dorvyn = nodeHandler(dorvynRoutes({ user: (request) => userIdFromCookie(request.headers.get("cookie")) }));
http.createServer((req, res) => {
if (req.method === "POST" && req.url.startsWith("/api/dorvyn/")) return dorvyn(req, res);
// …
});Saving as Word sends the whole document through this route, so allow request bodies as large as the largest document you expect (your plan's file limit is the ceiling). Express's express.json() allows only 100 KB by default.
4. Point the editor at it#
<dorvyn-editor server-url="/api/dorvyn" style="display: block; height: 100vh"></dorvyn-editor><DorvynEditor serverUrl="/api/dorvyn" /> // React and Next.js<dorvyn-editor dorvynServerUrl="/api/dorvyn"></dorvyn-editor> <!-- Angular -->That's all the page has: no key, no other settings.
Who the user is#
user is called for every request with the incoming web Request. With nodeHandler, that's a Request made from Node's req, so Express's req.session or req.user isn't on it: read them from req in a closure, as in the Express example. Return your signed-in user's id, or nothing (null, undefined, "") when nobody is signed in. Then there's no license, and the route answers 401 Sign in to edit.
- Use an opaque id, like your database's user id: 1 to 128 letters, digits, or
. _ : @ | -. Never an email address or a name. Dorvyn doesn't need to know who your users are, only to tell them apart. - Each grant and each saved file names that id, so usage is counted and limited per user, and anything unusual on your Security page points at an account you can look up.
- Limits per user are well above what anyone editing needs, so one account can't use up your whole plan.
What happens#
- The editor starts and sends
POST /api/dorvyn/grantto your server. The body has no key, only the SDK's version and random ids for the page session. - Your route checks who is signed in, then asks Dorvyn with your secret key for a grant for that user and the page's address (its
Origin). - Dorvyn checks the address against your application's domains and your plan, and signs a grant for that page and that user.
- The editor verifies the grant itself and becomes editable. It renews in the background the same way for as long as the page is open.
- When the person saves as Word, the editor sends the document to
POST /api/dorvyn/export. Your route gets Dorvyn's approval for that one save with your secret key, has the export service build the file, and passes the.docxback.
Your secret key never leaves your server, and nothing your page holds can be used anywhere else:
- A grant copied from the browser's Network tab works only on your site, for that user, which they already have. Dorvyn's export service won't save anything with it unless your server approves the save.
- A user calling your routes directly, with Postman for example, gets no more than their own browser does, within their own limits, under their own id.
- Another site calling your routes is refused: browsers send the page's
Origin, and Dorvyn makes grants only for your application's domains.
When your API is on another address#
The editor sends cookies to server-url only when it's on the page's own origin, as any fetch does. The simplest setup is a route on the same origin (a rewrite or proxy in front of your API works too).
If your app signs in with a token instead of cookies, or your API is on another origin, give the editor a fetch that adds what your API needs:
<DorvynEditor
serverUrl="https://api.example.com/dorvyn"
options={{
fetch: async (url, init) => fetch(url, { ...init, headers: { ...init?.headers, authorization: `Bearer ${await getAccessToken()}` } }),
}}
/>For cookies to another origin, use credentials: "include". Then your API must answer CORS for your app's origin with credentials allowed.
Options#
dorvynRoutes({
user, // required: your signed-in user's id, or nothing
origins: ["https://app.example.com"], // optional: only these page addresses may ask
secretKey: process.env.MY_DORVYN_KEY, // optional: instead of DORVYN_SECRET_KEY
engine: { url: "/dorvyn/engine.wasm" }, // optional: serve the engine from your own site
});All of them, and the lower-level requestGrant and requestExport, are in the @dorvyn/server reference.
Environment variables#
| Variable | Needed | What it is |
|---|---|---|
DORVYN_SECRET_KEY | Yes | Your application's secret key (dvn_sk_live_… or dvn_sk_test_…) |
DORVYN_EXPORT_URL | No | Your own export service's address (self-hosted export) |
DORVYN_ENGINE_URL | No | The engine's address, if you serve it from your own site |
Everything else has the right default.
Testing it#
- Open the page signed in. It becomes editable within a second or two.
- Sign out in another tab. At the editor's next renewal (within about ten minutes) it becomes read-only and says why.
- In the browser's Network tab,
grantandexportgo only to your own server, and no request carries advn_key. - Save as Word. The file downloads.
If something doesn't work, the route's answer in the Network tab says why: see Messages and errors.