> ## Documentation Index
> Fetch the complete documentation index at: https://help.nextmatter.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate with Google Docs

> You can copy and populate documents with data from the process.

<Tip>
  Check out our ready-made templates available when you add a step and select **Templates > 3rd party templates**. You can use the templates to create a set of pre-configured steps. Need a specific template? Click the **Contact us** button in the top right of the page and let us know.
</Tip>

## Before you begin

* Connect Google services in Next Matter with a service account and share data with this account. [Learn more...](/docs/integrate-with-google)

* Check the [required scopes](/docs/security-matrix) to learn more about which data is shared between the apps.

* Get the Google Drive folder ID. You can find it in the folder's URL, which is [`https://drive.google.com/drive/u/0/folders/FOLDER_ID`](https://drive.google.com/drive/u/0/folders/FOLDER_ID)

* Get your Google document ID. To do this, open your Google file - it can be a spreadsheet or a document. Copy the value from the URL of your file: [https://docs.google.com/document/d/143uVKzah2WhnPEHY3ATwM4UM0K-hjvZ3OB\_kT93qrQU/edit](https://docs.google.com/document/d/143uVKzah2WhnPEHY3ATwM4UM0K-hjvZ3OB_kT93qrQU/edit)

* You need to have a template ready and available in Google Drive.

## Copy and populate a Google Docs template

1. In your Next Matter portal, click **Workflows** which should copy and populate a document.

2. Click **+ Add step** and select **Integrations > Custom integration** as the step type.

3. Click **Settings** to configure the step. In this step, you'll be creating a copy of the template.

4. Enter the following details:

   <AccordionGroup>
     <Accordion title="Where is the file / document ID in Google">
       Open your Google file - it can be a spreadsheet or a document. Copy the value from the URL of **your file** ( between /d/ and /edit)

       [`https://docs.google.com/document/d/143uVKzah2WhnPEHY3ATwM4UM0K-hjvZ3OB_kT93qrQU/edit`](https://docs.google.com/document/d/143uVKzah2WhnPEHY3ATwM4UM0K-hjvZ3OB_kT93qrQU/edit)
     </Accordion>

     <Accordion title="Where do I find a file name of my document in Google">
       Open your Google file - the name of your document is in the top left-hand corner. By default, Google calls your file *Untitled* until you rename it.
     </Accordion>
   </AccordionGroup>

   ```json theme={null}
   {
     "name":"FILE_NAME",
     "parents":"PARENT_FOLDER_ID"
   }
   ```

   * To make sure Google Sheets uses the next empty row every time a new data set is populated, click **+ Add variable**. Create an **id** variable and enter the **\$.id** value.

   - **Authorization**: Google Drive

   - **Headers**: Content-Type: application/json

   - In the **Body** enter the values. For example, the body might look like the following:

   * **Method**: POST

   * **URL**: [`https://www.googleapis.com/drive/v3/files/DOCUMENT_ID/copy?supportsAllDrives=True`](https://www.googleapis.com/drive/v3/files/DOCUMENT_ID/copy?supportsAllDrives=True)

5. Save your changes.

6. Now click **+ Add step** and create another integration step to populate the data.

7. Click **Settings** to configure the step.

8. Enter the following details:

   <Info>
     Note that in the URL above the data in {} is a data reference from the previous step. Click the {} icon in the editor and find the data reference you need.
   </Info>

   * **Authorization**: Google Docs

   * **Headers**: Content-Type: application/json

   * In the **Body** enter the values. For example, the body might look like the following.

   * **Method**: POST

   * **URL**: `https://docs.googleapis.com/v1/documents/{64. Copy the template - Copy the template document linked here - id (plain)}:batchUpdate/`

9. Save your changes.

10. Now export the editable file to PDF. To do that, create an integration step with the following settings:

    <Info>
      Note that in the URL above the data in {} is a data reference from the previous step. Click the {} icon in the editor and find the data reference you need.
    </Info>

    * **Authorization**: Google Docs

    * **Headers**: Content-Type: application/json

    - **Method**: GET

    - **URL**: `https://docs.googleapis.com/drive/v2/files/{64. Copy the template - Copy the template document linked here - id (plain)}/pdf/export`

## Insert the signature to a Google Docs file

The process of copying and populating the document has been described above. If you want the new version of the document to be signed, you need to get the signature before you copy and populate the file and after the file has been copied and populated with data, insert the signature in the document.

**To get the signature**:

1. Click **+ Add step**. Select to add a form.

2. Click **+ Add form field**. Select **Signature**.

When you have the signature, go to the template file and place an image where you want the signature to be. This can be any image - you'll be replacing it in the course of the workflow.

To replace the image, you'll need its object ID. It will be the same for all signatures, so you only need to get it once.

**To get the ObjectId, do the following**:

1. Click this Google Developer [link](https://developers.google.com/docs/api/reference/rest/v1/documents/get).

2. In the right-hand side **Try this method** section, in the **Request parameters**, enter your document ID in the **documentId** field.

3. Click **Execute**.

4. Copy the **ObjectId** from the response. It might look the following `"objectId": "kix.pc8vwkqlqnur"`. You can find it within the `inlineObjects` attribute.

Copy the document file and populate it with data as described above.

When the new file is ready, replace the image with the signature.

**To replace the image, do the following:**

1. In your workflow, create a new step. Select **Integration> Custom integration** as the type.

2. Click **Settings** and enter the following details:

   <Info>
     Note that in the URL above the data in {} is a data reference from the previous step. Click the {} icon in the editor and find the data reference you need.
   </Info>

   ```json theme={null}
   {
     "requests": [
       {
         "replaceImage": {
           "imageObjectId": "OBJECT_ID",
           "uri": "{72. Provide the signature - Signature Input - Signature (plain)}",
           "imageReplaceMethod": "CENTER_CROP"
         }
       }
     ]
   }
   ```

   * **Authorization**: Google Docs

   * **Headers**: Content-Type: application/json

   * In the **Body** enter the values. For example, the body might look like the following:

   - **Method**: POST

   - **URL**: `https://docs.googleapis.com/v1/documents/{64. Copy the template - Copy the template document linked here - id (plain)}:batchUpdate/`

3. Save your changes.

## Add a footnote to the document

1. In your workflow, create a new step. Select **Integration> Custom integration** as the type.

2. Click **Settings** and enter the following details:**Tip**: use `document.get` from the Google Doc API to obtain the position of the index. See [Google documentation](https://developers.google.com/docs/api/reference/rest/v1/documents/get).

   ```json theme={null}
   {
     "requests": [
       {
         "createFootnote": {
           "location": {
             "index": "INDEX"
           }
         }
       }
     ]
   }
   ```

   * Add a variable for the `footnote_id` with the value of `$.replies[0].createFootnote.footnoteId`.

   - **Method**: POST

   - **URL**: [`https://docs.googleapis.com/v1/documents/DOCUMENT_ID:batchUpdate`](https://docs.googleapis.com/v1/documents/DOCUMENT_ID:batchUpdate)

   - **Authorization**: Google Docs

   - **Headers**: Content-Type: application/json

   - In **Body**, enter the values. For example, the body might look like the following:

3. Save your changes.

4. To add the text to the footnote, create another integration step and edit the settings with the following values:

   ```json theme={null}
   {
     "requests": [
       {
         "insertText": {
           "text": "TEXT",
           "endOfSegmentLocation": {
             "segmentId": "{2. Create Footnote - Fill out the template - footnote_id (plain)}"
           }
         }
       }
     ]
   }
   ```

   * **Method**: POST

   * **URL**: [`https://docs.googleapis.com/v1/documents/DOCUMENT_ID:batchUpdate`](https://docs.googleapis.com/v1/documents/DOCUMENT_ID:batchUpdate)

   * **Authorization**: Google Docs

   * **Headers**: Content-Type: application/json

   * In **Body**, enter the values. For example, the body might look like the following:

5. Save your changes.
