Backend Document Manipulation enables a variety of use cases, such as:
- Live translation of document content.
- Server-side components, like executing an SQL query and displaying results.
- Automatic typo and grammar corrections.
- Version history integration and conflict-free merging of concurrent edits.
This powerful feature set allows for extensive customization and automation of document handling on the Tiptap platform, catering to a wide range of collaborative editing scenarios.
## Update Document
To update an existing document on the Collaboration server, you can use the `PATCH` method with the following API endpoint:
```bash
PATCH /api/documents/:identifier?format=:format
```
This endpoint accepts a Yjs update message and applies it to the specified document. The `format` query parameter specifies the format of the update and can be one of the following:
-`binary`: Directly using Yjs's `Y.encodeStateAsUpdate` method.
-`base64`: The binary state encoded as a Base64 string.
-`json`: Updates the document using JSON format (with some caveats, see below).
Upon successful update, the server will return HTTP status `204`. If the document does not exist, it will return `404`, and if the payload is invalid or the update cannot be applied, it will return `422`.
When updating via JSON, the server computes the difference between the current document state and the provided JSON, then internally calculates the required Yjs update to reach the target state.
To ensure precise updates, especially for node-specific changes, it is recommended to use the `nodeAttributeName` and `nodeAttributeValue` parameters. These can be generated by Tiptap's [UniqueID Extension](https://tiptap.dev/docs/editor/api/extensions/unique-id) or a custom implementation.
-`nodeAttributeName`: Configured as `attributeName` in the UniqueID extension.
-`nodeAttributeValue`: The unique value generated for the node to be updated.
From version 3.3.0 onwards, you can use `?mode=append` to append nodes to the document's JSON representation without altering existing content.
Omitting these parameters may result in overwriting any updates made between fetching the document and issuing the update call.
Here is an example of how to update a document using JSON:
```typescript
// Define the document name, secret, and application ID
const docName = ''
const secret = ''
const appId = '';
// Construct the base URL
const url = `https://${appId}.collab.tiptap.dev`
// Fetch the current document's JSON representation
To seed a new document on the Tiptap Collab server, use the `POST` method with the following endpoint:
```bash
POST /api/documents/:identifier?format=:format
```
The server will return HTTP status `204` for successful creation, `409` if the document already exists (you must delete it first to overwrite), and `422` if the action failed.
The `format` parameter accepts the same values as the update endpoint (`binary`, `base64`, or `json`).