DEV Community

Cover image for ONLYOFFICE Docs API Sample: Scaffold a Next.js Integration with Create App in One Command
Kseniya Fedoruk for ONLYOFFICE

Posted on

ONLYOFFICE Docs API Sample: Scaffold a Next.js Integration with Create App in One Command

Want to see how ONLYOFFICE Docs fits into a modern web app without wiring every callback by hand? The new Create App integration sample on the ONLYOFFICE API site walks you through @onlyoffice/create-app, a CLI that scaffolds a complete Next.js integration in one command, much like create-next-app does for a site.

Here's what's inside:

  • One command to generate a Next.js + React + TypeScript file manager wired to ONLYOFFICE Docs
  • Three .env variables to connect it to your Docs server
  • JWT in both directions: a signed editor config and a verified save callback
  • Real save flow: edited files are written back to disk through the callback handler

🧩 What the sample does

The Create App integration sample generates a small file manager. The app 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.

πŸ’‘ Tip: The sample page says 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. Don't run it on a public server as is.

πŸ› οΈ Prerequisites

  • ONLYOFFICE Docs 8.2 or later, self-hosted or cloud. Create App doesn't install Docs, and Docs must be able to reach the machine running the app.
  • Node.js 20.12 or later.
  • Docker (optional) if you want to run Docs locally. The repository README gives this command:
docker run -i -t -d -p 80:80 -e JWT_SECRET=developer-only-not-a-real-secret onlyoffice/documentserver
Enter fullscreen mode Exit fullscreen mode

βš™οΈ How it works

Scaffold the project

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

The CLI copies the app, creates .env from .env.example and installs dependencies. It doesn't create a Git repository. You can pick the package manager with --use-npm, --use-pnpm, --use-yarn or --use-bun, or skip the install with --skip-install. npm init @onlyoffice/app my-app, pnpm create @onlyoffice/app my-app and yarn create @onlyoffice/app my-app work too.

Point it at your Docs server

Three variables in .env are required:

# URL of ONLYOFFICE Docs (Document Server) as reachable from the user's BROWSER.
# Required.
DOCUMENT_SERVER_URL=http://localhost/

# ...

# JWT secret configured in the Document Server (services.CoAuthoring.secret / JWT_SECRET).
# Leave empty only if JWT is disabled on the Document Server (not recommended).
DOCUMENT_SERVER_JWT_SECRET=developer-only-not-a-real-secret

# ...

# Public base URL of THIS application as reachable from the Document Server.
# The Document Server downloads files from here and posts save callbacks here,
# so "localhost" only works when both run on the same host without containers.
# Example: http://192.168.1.10:3000
APP_URL=http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

DOCUMENT_SERVER_URL is how the browser sees Docs; APP_URL is how Docs sees your app. If Docs can't reach APP_URL, the editor reports Download failed; if the browser can't reach DOCUMENT_SERVER_URL, the editor doesn't load. Both cases are listed under Troubleshooting on the sample page. Optional variables include DOCUMENT_SERVER_INTERNAL_URL (for Docker networks), DOCUMENT_SERVER_JWT_HEADER, DOCS_LANG, DEMO_USER_ID, DEMO_USER_NAME, STORAGE_DIR and ALLOWED_DEV_ORIGINS.

Build and sign the editor config

src/lib/editor-config.ts builds the editor configuration on the server and signs it, so the JWT secret never reaches the browser:

  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`,
      // ...
    },
    editorConfig: {
      // The Document Server posts editing events and the saved document here.
      callbackUrl: `${baseUrl}/api/callback?file=${encodedName}`,
      mode: decision.mode,
      // ...
    },
  };

  if (env.jwtSecret) {
    config.token = await signPayload(config, env.jwtSecret);
  }
Enter fullscreen mode Exit fullscreen mode

documentKey() derives the key from the file name, modification time and size, so the key changes after every save without a database. signPayload() signs the config with HS256 using jose (see JWT signature).

On the client, src/components/Editor.tsx renders the DocumentEditor component from @onlyoffice/document-editor-react (see the React guide) with the signed config from /api/editor-config.

Handle the save callback

src/app/api/callback/route.ts is the callback handler. It verifies the JWT first, then saves the file when Docs reports 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 1:
        console.log(`[callback] ${fileName}: editing (users: ${body.users?.join(', ') ?? '-'})`);
        break;
      case 2:
      case 6:
        await saveDocument(fileName, body, env);
        // ...
        break;
      // ...
    }
  } catch (error) {
    // ...
  }

  return Response.json({ error: 0 });
Enter fullscreen mode Exit fullscreen mode

The handler must reply {"error": 0}; anything else makes the editor show an error.

πŸš€ Try it yourself

  1. Start ONLYOFFICE Docs 8.2 or later (for example, with the Docker command above).
  2. Run npx @onlyoffice/create-app my-app and cd my-app.
  3. Set DOCUMENT_SERVER_URL, DOCUMENT_SERVER_JWT_SECRET and APP_URL in .env.
  4. Run npm start (or npm run dev for hot reload). On first start, the app downloads the blank documents once, so it needs internet access.
  5. Open http://localhost:3000, create or upload a document, and click it.

<!-- IMAGE: a document open in the ONLYOFFICE editor inside the sample app, with the "Back to files" link; alt="Document opened in ONLYOFFICE Docs from the Create App sample" -->

πŸ’‘ Tip: The app asks Docs for supported formats via GET /meta/formats (ONLYOFFICE Docs 9.4 or later). With older versions, it falls back to the built-in list in src/lib/fallback-formats.json.

βœ… Key takeaways

  • Two URLs, two viewpoints: DOCUMENT_SERVER_URL for the browser, APP_URL for Docs.
  • Sign on the server: the config is signed in a route handler, never in the browser.
  • Trust the token: the callback uses the decoded JWT, not the plain JSON body.
  • Key = file state: deriving document.key from name, time and size keeps co-editing sessions correct.

GitHub logo ONLYOFFICE / create-app

CLI for creating starter projects with ONLYOFFICE integrations.

@onlyoffice/create-app

Scaffold a demo application that integrates the ONLYOFFICE Docs editors, the same way create-next-app scaffolds a site:

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

The generated project is a small file manager built with Next.js + React + TypeScript that:

  • lists, uploads, downloads and deletes files;
  • creates blank docx, xlsx, pptx and pdf files from ONLYOFFICE document templates, downloaded over HTTPS the first time the app starts;
  • asks the Document Server which formats it supports (GET /meta/formats) and opens each file either in the editor or, for read-only formats, in the viewer;
  • signs the editor configuration with JWT and verifies JWT on the save callback;
  • saves edited documents back to disk through the callback handler.

Everything about the Document Server connection is configured through environment variables in .env See the generated project's README.md for the full list…

Want a step-by-step walkthrough with screenshots? Read Get started with document editing in your web app using ONLYOFFICE Create App on the ONLYOFFICE Blog. To run Docs on your own server, get ONLYOFFICE Docs Developer.

Grab the sample, point it at your Docs server, and start building your integration today. We'd love to hear what you build. Drop your questions or feedback in the comments! πŸ‘‡

Top comments (0)