> ## 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 Drive

> You can use Next Matter integration steps to directly integrate your workflow with Google Drive to, for example, upload files.

## 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 a Google Drive file ID.

  <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>

* 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)

<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>

## Upload files to process and then Drive

1. Create a step to upload a file. To do that, in your workflow add a step that's a form.

2. Click **+ Add form field** and select the **File upload** as the form field.

3. Create the next step to upload the file to Google Drive. Click **Settings** to configure the step.

4. Enter the following details:

   | Key      | Type | Value                                                                                               |
   | -------- | ---- | --------------------------------------------------------------------------------------------------- |
   | metadata | text | `{"name": "{data reference to the filename of the uploaded file}", "parents":["GDRIVE_FOLDER_ID"]}` |
   | key1     | file | `{data reference to the "First File" field from the file upload step}`                              |

   <Info>
     You can only upload one file from the File Upload step.
   </Info>

   * **Method**: POST

   * **URL**: [`https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart`](https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart)

   * **Authorization**: Google Drive

   * **Body type**: Form-data

   * In the **Body** add the two following rows:

5. If you need the Google Drive file ID of the file to be referenced in later steps, click **+ Create variable** and create a `fileid` reference of the value **\$id**.

6. Save your changes.

## Give access to the file stored in Google Drive to a user

1. Create an **Integrations > Custom integration** step to upload the file.

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

3. Enter the following details:

   <CodeGroup>
     ```json json theme={null}
     {
       "type": "user",
       "role": "reader OR writer",//select a permission profile
     "value":"EMAIL_OF_THE_GDRIVE_ACCOUNT_TO_HAVE_ACCESS"
     }
     ```
   </CodeGroup>

   * **Method**: POST

   * **URL**: [`https://www.googleapis.com/drive/v2/files/FILE_ID/permissions?supportsAllDrives=true&includeTeamDriveItems=true&includeItemsFromAllDrives=true`](https://www.googleapis.com/drive/v2/files/FILE_ID/permissions?supportsAllDrives=true\&includeTeamDriveItems=true\&includeItemsFromAllDrives=true)

   * **Authorization**: Google Drive

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

   * In the **Body** enter the following data:

4. If you need the Google Drive file ID of the file to be referenced in later steps, click **+ Create variable** and create a `fileid` reference of the value **\$id**.

5. Save your changes.

## Copy file stored in Drive

1. Create an **Integrations > Custom integration** step to copy the file.

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

3. Enter the following details:

   <CodeGroup>
     ```json json theme={null}
     {"name": "FILENAME_OF_THE_FILE_TO_COPY", 
      "parents": ["GDRIVE_FOLDER_ID"] } 
     ```
   </CodeGroup>

   * **Method**: POST

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

   * **Authorization**: Google Drive

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

   * In the **Body** enter the following data:

4. If you need the Google Drive file ID of the file to be referenced in later steps, click **+ Create variable** and create a `fileid` reference of the value **\$id**.

5. Save your changes.

## Create a Google Drive folder

### Before you begin

* Get the Google Drive parent 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.) The parent folder is the folder in which you're creating a sub-folder.

1. Create an **Integrations > Custom integration** step.

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

3. Enter the following details:

   <CodeGroup>
     ```json json theme={null}
     {
         "name": "FOLDER_NAME",//if the name was referenced in a previous step, use data reference. If not use real value.
         "mimeType": 'application/vnd.google-apps.folder',
          "parents": ["PARENT_FOLDER_ID"] //if the ID was referenced in a previous step, use data reference. If not use real value.
     }
     ```
   </CodeGroup>

   * **Method**: POST

   * **URL**: \`[https://www.googleapis.com/drive/v3/files](https://www.googleapis.com/drive/v3/files) Note that for the request to work, you need to give full access to the parent folder, such as the "Anyone with the link can edit" permission.

   * **Authorization**: Google Drive

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

   * In the **Body** enter the following data:

4. If you need the Google Drive file ID of the file to be referenced in later steps, click **+ Create variable** and create a `folderid` reference of the value **\$id**.

If you need to move a file to this newly created folder, create a follow-up integration step with the following details:

* **Method**: PATCH

* **URL**: [`https://www.googleapis.com/drive/v3/files/FILE_ID?addParents=FOLDER_ID`](https://www.googleapis.com/drive/v3/files/FILE_ID?addParents=FOLDER_ID)

* **Authorization**: Google Drive

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

* **Body** is empty

## Move a file to a new folder

You can move a file from one folder to another in Google Drive.

1. In Next Matter, create an **Integrations > Custom integration** step.

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

3. Enter the following details:

* **Method**: PATCH

* **URL**: [`https://www.googleapis.com/drive/v3/files/FILE_ID?addParents=PARENT_FOLDER_ID`](https://www.googleapis.com/drive/v3/files/FILE_ID?addParents=PARENT_FOLDER_ID)

<Info>
  The file ID and the parent folder ID can also be references to data from previous steps.
</Info>

* **Authorization**: Google Drive

* **Body** is empty.

## Replace one file with another

1. Create a step to upload a file. To do that, in your workflow add an action step.

2. Click **+ Add action** and select the **File upload** as the action type.

3. Create the next step to replace an old file with the uploaded one in Google Drive. Click **Settings** to configure the step.

4. Enter the following details:

   | Key      | Type | Value                                                               |
   | -------- | ---- | ------------------------------------------------------------------- |
   | metadata | text | `{"name": "{data reference to the filename of the uploaded file}"}` |
   | key1     | file | `{data reference to the uploaded file}`                             |

   * **Method**: PUT

   * **URL**: [`https://www.googleapis.com/upload/drive/v3/FILE_ID`](https://www.googleapis.com/upload/drive/v3/FILE_ID)

   * **Authorization**: Google Drive

   * **Body type**: Form-data

   * In the **Body** add the two following rows:

5. If you need the Google Drive file ID of the file to be referenced in later steps, click **+ Create variable** and create a `fileid` reference of the value **\$id**.

6. Save your changes.
