Skip to main content

ONLYOFFICE Create App Integration Sample: A Complete Next.js Reference for Docs Integrations

If you build on top of ONLYOFFICE Docs, this sample gives you a working reference app in one command.

Updated 5 min readMarkdown
ONLYOFFICE Create App Integration Sample: A Complete Next.js Reference for Docs Integrations

If you build on top of ONLYOFFICE Docs, the new Create App integration sample gives you a working reference app in one command, so you can study a complete integration instead of assembling one from scattered snippets.

The sample, added to the ONLYOFFICE API site in version 10.2.0, documents @onlyoffice/create-app: a CLI that generates a Next.js + React + TypeScript file manager already connected to ONLYOFFICE Docs. In this post, we look at:

  • what the generated app does and what it deliberately leaves out;
  • the three integration points that every Docs integration needs: the editor config, JWT and the save callback;
  • how to run it against your own Docs server;
  • where to take it next.

Let's dig in.

What the Sample Does

The Create App integration sample generates a small file manager that can:

  • list, upload, download and delete files;
  • create blank documents, spreadsheets, presentations and PDF forms from the ONLYOFFICE document templates;
  • open each file in the editor or, for view-only formats, in the viewer;
  • sign the editor configuration with JWT and verify JWT in the save callback;
  • save edited documents back to disk.

The generated app is for testing only: it has no authentication, opens every file as a single demo user, and serves files to anyone who can reach it. Do not run it on a public server as is.

Why this matters: the app is small enough to read in one sitting, yet it covers the full round trip described in How it works: your app serves a file, Docs edits it, and Docs sends the result back.

How It Works

The Editor Config, Built and Signed on the Server

The browser asks /api/editor-config for a configuration. src/lib/editor-config.ts builds it from the config reference and signs it with the shared secret:

  const config: Config = {
    type: 'desktop',
    documentType: format.type,
    document: {
      fileType: extOf(name) as FileType,
      key: documentKey(name, info.mtimeMs, info.size),
      title: name,
      // The Document Server downloads the file from here.
      url: `${baseUrl}/api/files/${encodedName}/download`,
      permissions: {
        edit: canEdit,
        review: canEdit && format.actions.includes('review'),
        comment: canEdit && format.actions.includes('comment'),
        fillForms: canEdit && format.actions.includes('fill'),
        modifyFilter: canEdit && format.actions.includes('customfilter'),
        download: true,
        print: true,
        copy: true,
      },
    },
    editorConfig: {
      // The Document Server posts editing events and the saved document here.
      callbackUrl: `${baseUrl}/api/callback?file=${encodedName}`,
      mode: decision.mode,
      lang,
      region,
      user: { id: env.user.id, name: env.user.name },
      customization: {
        autosave: true,
        forcesave: false,
        // ...
      },
    },
  };

  if (env.jwtSecret) {
    config.token = await signPayload(config, env.jwtSecret);
  }

Permissions come from what Docs reports for each format. The app calls GET /meta/formats (ONLYOFFICE Docs 9.4 or later) and falls back to src/lib/fallback-formats.json on older servers.

Why this matters: the secret stays on the server, and the editor opens in edit or view mode based on what the server actually supports, not on a hard-coded extension list.

A Document Key Without a Database

export function documentKey(name: string, mtimeMs: number, size: number): string {
  const hash = createHash('sha1')
    .update(`${name} ${Math.floor(mtimeMs)} ${size}`)
    .digest('hex');
  const stem = name
    .replace(/\.[^.]+$/, '')
    .replace(/[^0-9a-zA-Z._=-]/g, '_')
    .slice(0, 40);
  return `${stem}_${hash}`.slice(0, 128);
}

Everyone who opens the same key joins the same co-editing session, and the key must change once the file on disk changes. Hashing the name, modification time and size gives exactly that.

The Save Callback, Verified With JWT

Docs posts editing events to the callback handler at /api/callback. The route verifies the token before it trusts the body, then saves on status 2 or 6:

  // With JWT enabled the decoded token is the source of truth, not the plain JSON body.
  const body = await verifyCallbackBody(request, rawBody, env);

  try {
    switch (body.status) {
      case 2:
      case 6:
        await saveDocument(fileName, body, env);
        // ...
        break;
      case 3:
      case 7:
        console.error(`[callback] ${fileName}: the Document Server reported a saving error`, body);
        break;
      // ...
    }
  } catch (error) {
    // ...
  }

  return Response.json({ error: 0 });

verifyCallbackBody() in src/lib/jwt.ts accepts the token either in the body (token field) or in the header set by DOCUMENT_SERVER_JWT_HEADER (Authorization by default), as described in JWT signature.

Why this matters: an unsigned callback can't overwrite your files, and the handler always answers {"error": 0} on success so the editor doesn't show an error.

Running the Sample

Requirements: ONLYOFFICE Docs 8.2 or later (self-hosted or cloud) that can reach your machine, and Node.js 20.12 or later.

npx @onlyoffice/create-app my-app
cd my-app
# edit .env: DOCUMENT_SERVER_URL, DOCUMENT_SERVER_JWT_SECRET, APP_URL
npm start

Set three variables in .env:

  • DOCUMENT_SERVER_URL: Docs as the browser sees it, e.g. http://localhost/;
  • DOCUMENT_SERVER_JWT_SECRET: the same secret as in Docs (JWT_SECRET in Docker);
  • APP_URL: your app as Docs sees it. http://localhost:3000 only works when both run on the same host without containers; otherwise use your LAN address, e.g. http://192.168.1.10:3000.

npm start builds and serves the app on port 3000; npm run dev starts the dev server with hot reload. The first start downloads the blank templates from the ONLYOFFICE/document-templates repository. Then open http://localhost:3000, create a document and click it.

https://github.com/ONLYOFFICE/create-app

Where to Take It Next

  • Replace the single demo user (DEMO_USER_ID, DEMO_USER_NAME) with your own authentication.
  • Swap src/lib/storage.ts for your storage backend.
  • Adjust editor parameters in src/lib/editor-config.ts, for example customization.
  • Enable force saving: the callback already handles status 6, while the generated config sets forcesave: false.

To get started, generate the app and read src/lib/editor-config.ts first.

About the author

  • Tech content creator at ONLYOFFICE. Author of how-to articles, contributor on GitHub, switched from outdated Windows to Kubuntu, have been working with open-source office software since 2017.

No comments yet. Be the first to comment.