# Document copy

<a id="overview">

## Overview

The document copy feature allows users to copy an entire document from an active collaborative editing session or from [the document storage](document-storage.md). The copy of the document can be then edited independently from the original document but will still contain all the comments and suggestions from the source document.

<a id="prerequisites">

## Prerequisites

* An [editor bundle](editor-bundle.md) needs to be uploaded.
* The source document needs to be created after uploading the editor bundle.

<a id="usage">

## Usage

Copying a document means creating a new collaborative editing session using the document data from an already existing one on the CKEditor Collaboration Server. It is done by sending a request to `POST /documents/{source_document_id}` [method](https://help.cke-cs.com/api/docs#tag/Documents/paths/~1documents~1%7Bsource_document_id%7D/post) from our [REST API](https://help.cke-cs.com/api/docs).\
You can also use the endpoint to quickly create a copy of the document with comment and suggestion markers excluded without the need to modify the original document.

<a id="example">

## Example

The example below has been prepared in `Node.js`.

1. Run the following commands

```bash
mkdir cs-copy-example && cd cs-copy-example && npm init -y && npm i axios && touch copy.js
```

2. Open `cs-copy-example/copy.js` and paste the following code snippet:

```javascript
const crypto = require( 'crypto' );
const axios = require( 'axios' );

// Update with your credentials and application endpoint
const environmentId = 'txQ9sTfqmXUyWU5LmDbr';
const apiSecret = '4zZBCQoPfRZ7Rr7TEnGAuRsGgbfF58Eg0PA8xcLD2kvPhjGjy4VGgB8k0hXn';
const applicationEndpoint = 'https://33333.cke-cs.com';
const apiEndpoint = `${ applicationEndpoint }/api/v5/${ environmentId }/documents/${ sourceDocumentId }`;

// Set source and target document id
const sourceDocumentId = 'my_document_id_source';
const targetDocumentId = 'my_document_id_target';

const body = {
	'target_document_id': targetDocumentId,
	'omit_comments': false,
	'omit_suggestions': false
};

const CSTimestamp = Date.now();
const config = {
   headers: {
	  'X-CS-Timestamp': CSTimestamp,
	  'X-CS-Signature': generateSignature( apiSecret, 'POST', apiEndpoint, CSTimestamp, body )
   },
};

axios.post( apiEndpoint, body, config )
   .then( response => {
	  console.log ( response.status );
   } ).catch( error => {
	  console.log( error.message );
	  console.log( error.response.data );
   } );

function generateSignature( apiSecret, method, uri, timestamp, body ) {
   const url = new URL( uri );
   const path = url.pathname + url.search;
   const hmac = crypto.createHmac( 'SHA256', apiSecret );

   hmac.update( `${ method.toUpperCase() }${ path }${ timestamp }` );

   if ( body ) {
	  hmac.update( Buffer.from( JSON.stringify( body ) ) );
   };

   return hmac.digest( 'hex' );
}
```

3. Update your credentials, the `sourceDocumentId` and the `targetDocumentId` values in the code snippet.

4. Execute the `node copy.js` command.

After a successful response with a status code `201`, you can open the editor with the same `targetDocumentId` as set in the snippet to see your copied content.

<a id="troubleshooting">

## Troubleshooting

* In case of any operation while copying fails, e.g. fail while copying comments all data of a target document will be revoked. The data of the source document are not changed.
* We suggest enabling [Insight Panel](../../developer-resources/monitoring/insights-panel.md), where you will find detailed logs from the copy process including exact reasons for any failed requests.

<a id="more-info">

## More info

You will find more info in our [REST API documentation](https://help.cke-cs.com/api/docs#tag/Documents/paths/~1documents~1%7Bsource_document_id%7D/post).

---

Full index of the Cloud Services documentation: [llms.txt](../../../llms.txt)
