# Extract document content in Node.js

This article presents an example of an application that allows for extracting document content using the [Server-side Editor API](../../developer-resources/server-side-editor-api/editor-scripts.md) feature.

<a id="dependencies">

## Dependencies

This example uses the following dependencies:

* `axios`

It also uses core dependencies from Node.js: `crypto`.

<a id="example">

## Example

This example demonstrates how to use the Server-side Editor API to extract document content using Node.js. Make sure you have Node.js and npm installed before following these steps.

1. Create a new project and install dependencies:

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

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

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

// Set document id
const documentId = 'my_document_id';

// 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 }/collaborations/${ documentId }/evaluate-script`;

// Example script that extracts document content
const script = `
	const data = editor.getData();

	return {
		content: data,
		wordCount: data.split(' ').length,
		characterCount: data.length
	};
`;

const body = {
	'script': script,
	'user': {
		'id': 'txQ9sTfqmXUyWU5LmDbr',
		'name': 'System Process',
	}
};

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( 'Status:', response.status );
		console.log( 'Response data:', response.data );
	} ).catch( error => {
		console.log( 'Error:', error.message );
		console.log( 'Response data:', 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 and the `documentId` values in the code snippet.

4. Execute the script:

```bash
node sse-api.js
```

After a successful response with a status code `201`, you should see the document content, word count, and character count in the console output.

> **Note**
>
> To learn more about the Server-side Editor API, head to the [Developer resources section](../../developer-resources/server-side-editor-api/editor-scripts.md).

---

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