# Server-side work

> Convert, edit, and export documents in Node with revise/server.

`revise/server` is the SDK's self-contained Node entry point, included in
`revise`. The [SDK license](https://revise.io/developer/docs/editor-sdk/license)
covers programmatic use, including commercial-use requirements.

<CodeGroup>

```bash npm
npm i revise@4.3.0
```

```bash pnpm
pnpm add revise@4.3.0
```

```bash yarn
yarn add revise@4.3.0
```

</CodeGroup>

The [library docs](https://revise.io/developer/docs/editor-sdk/api/library) cover this API.
The editor never imports `revise/server`, and server code that imports it
doesn't load React.

Documents move between the two unchanged: a `Y.Doc` the editor edits is a
`Y.Doc` `revise/server` opens, and the reverse. It runs the same converters and
the same document tools, with the same schemas and `{ ok, value | error }`
results, so a server can seed a room, edit it while people have it open, and
export it to Word:

```ts
import { ReviseDocument } from "revise/server";
import type * as Y from "yjs";

async function reviewRoom(room: Y.Doc): Promise<Uint8Array> {
  const doc = await ReviseDocument.fromYDoc(room, { author: "Review bot" });
  try {
    const [hit] = doc.grep("Delaware");
    if (hit) {
      // A tracked change in every open editor.
      await doc.tools.call("replace", {
        id: hit.id,
        replacements: [{ find: "Delaware", replace: "New York" }],
      });
    }
    const { bytes } = await doc.export({ format: "docx" });
    return bytes;
  } finally {
    doc.dispose(); // the room stays alive
  }
}
```

<CardGroup cols={2}>
  <Card title="Library" icon="code" href="https://revise.io/developer/docs/editor-sdk/api/library">
    Open, read, edit, review, and export documents.
  </Card>
  <Card title="Collaboration" icon="users" href="https://revise.io/developer/docs/editor-sdk/api/server-collaboration">
    Seed, edit, and export live rooms.
  </Card>
  <Card title="Server editing" icon="server" href="https://revise.io/developer/docs/editor-sdk/guides/collaboration#server-editing">
    A server and browsers in the same room.
  </Card>
  <Card title="Tools on the server" icon="robot" href="https://revise.io/developer/docs/editor-sdk/guides/agent-tools#server-side-editing">
    The agent tools without a browser.
  </Card>
</CardGroup>

## Upgrade from 2.x

The 2.x server entry point exported standalone functions; 4.x is built around
`ReviseDocument`. The 2.x names still import from `revise/server`, so a build
doesn't break on them, but each one throws when used, naming its replacement.
The [2.3 reference](https://revise.io/developer/docs/editor-sdk/v2.3/api/backend) documents them.

| 2.x                                                  | 4.x                                                                                                  |
| ---------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| `createServerDocumentSession(ydoc)`                  | `ReviseDocument.fromYDoc(ydoc)`, then `doc.tools`                                                    |
| `parseDocument`, `documentToDocx`, `fileToYDoc`      | `ReviseDocument.open(file)`, `doc.export({ format: "docx" })`, `doc.ydoc`                            |
| `exportDocument(room, { mode })`, `ydocToDocx(room)` | `ReviseDocument.fromYDoc(room)`, then `doc.export({ format, mode })`                                 |
| `seedYDocFromFile(room, bytes, filename)`            | `seedYDoc(room, { bytes, filename })`                                                                |
| `encodeYDoc`, `decodeYDoc`                           | Y.js itself: `Y.encodeStateAsUpdate(doc.ydoc)`; `ReviseDocument.restore(update)` opens a bare update |

Three differences when porting:

- `ReviseDocument` methods are async: `await` them.
- Tools return `{ ok: true, value }` or `{ ok: false, error }` instead of
  throwing. `doc.tools.call()` is the typed form; `doc.tools.run()` takes a
  tool name and JSON from a model.
- `doc.export()` returns `{ bytes, fidelity, … }`: write `.bytes`.

The server runtime includes its required dependencies.
`doc.snapshot()` is Revise's own storage format, not a Y.js update: keep
`Y.encodeStateAsUpdate(doc.ydoc)` for anything that speaks Y.js, such as a
sync server or the editor.

## License

The [SDK license](https://revise.io/developer/docs/editor-sdk/license) covers `revise/server`, its bindings,
and its bundled engine. Its commercial-use requirements apply to server-only
applications as well as applications embedding the editor.
