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

# 2. Nested Folders and Document Organization

> [!WARNING]
>
> You are viewing the documentation of release 26.1, which is no longer the current one. [Go to the documentation of the current release 26.2](https://developers.circularo.com/).

## Nested Folders and Document Organization

This scenario demonstrates how to create hierarchical folder structures and organize existing documents within them. Nested folders provide a powerful way to categorize and manage documents according to your organizational needs.

**Key features:**

* Create hierarchical folder structures with parent-child relationships
* Organize existing documents by moving them into specific folders
* Implement logical document categorization for improved findability

### Prerequisites

Before working with nested folders and document organization, you need:

* A valid authentication token (see the "Authentication & Security" section for details)
* Existing documents that you want to organize
* A clear organizational structure for your document hierarchy

## Step 1 - Create root folder

This endpoint creates a new root-level folder that will serve as the parent for nested folders. Root folders are the top level of your folder hierarchy.

* The **name** parameter defines the display name of the folder
* The **isShared** parameter set to false creates a private folder visible only to you
* The **parentFolderId** parameter set to null places the folder at the root level

> [!NOTE]
>
> Private folders (isShared: false) are only accessible to the creator and users explicitly granted access. This is useful for personal document organization or sensitive content.

**Endpoint**

```http theme={null}
POST /folders
```

**Request**

```http theme={null}
POST /folders?token=468Yv8XfXBYNZF1pJ5rRLnAMeDHmQ95Q2ecInYbQ7j22idlgRIJTGAwfEX16vIkJ

Content-Type: application/json

{
  "name": "Completed Documents",  //Display name of the root folder
  "isShared": false,  //Private folder (false) vs. shared folder (true)
  "parentFolderId": null  //null indicates this is a root-level folder
}
```

**Response**

```json theme={null}
{
  "id": "2106ee3a-8ded-409e-943b-e5034ce1f038",
  "success": true
}
```

The response contains the following important properties:

* **rootFolderId** - Located at `id` in the response.
  * The unique identifier for the newly created root folder. This ID will be used as the parent for nested folders.
  * Example value: `2106ee3a-8ded-409e-943b-e5034ce1f038`

The root folder has been successfully created at the top level of the folder hierarchy.

* The response includes the folder ID which will be used to create nested folders within it
* The folder is created with private access settings, making it visible only to you
* This folder will serve as the parent for more specific nested folders

## Step 2 - Create nested folder

This endpoint creates a nested folder within the previously created root folder. Nested folders allow you to create a hierarchical structure for more granular document organization.

* The **name** parameter defines the display name of the nested folder
* The **isShared** parameter determines the access control settings
* The **parentFolderId** parameter specifies which folder this new folder belongs to

> [!WARNING]
>
> Nested folders inherit access control settings from their parent folders. A private folder cannot contain shared subfolders, and vice versa, to maintain consistent access control.

**Endpoint**

```http theme={null}
POST /folders
```

**Request**

```http theme={null}
POST /folders?token=468Yv8XfXBYNZF1pJ5rRLnAMeDHmQ95Q2ecInYbQ7j22idlgRIJTGAwfEX16vIkJ

Content-Type: application/json

{
  "name": "Invoices",  //Display name of the nested folder
  "isShared": false,  //Access control setting (must match parent folder)
  "parentFolderId": "2106ee3a-8ded-409e-943b-e5034ce1f038"  //ID of the parent folder where this folder will be created
}
```

**Response**

```json theme={null}
{
  "id": "678bd175-ec83-4d32-8120-1d980f1287a2",
  "success": true
}
```

The response contains the following important properties:

* **nestedFolderId** - Located at `id` in the response.
  * The unique identifier for the newly created nested folder. Use this ID to place documents in this folder.
  * Example value: `678bd175-ec83-4d32-8120-1d980f1287a2`

The nested folder has been successfully created within the parent folder.

* The response includes the folder ID which will be used to place documents in this folder
* The folder inherits access control settings from its parent folder
* The folder hierarchy now has two levels: the root "Completed Documents" folder and the nested "Invoices" folder

> [!NOTE]
>
> You can create multiple nested folders within the same parent to organize different types of documents.

## Step 3 - Move documents into folder

This endpoint moves existing documents into a specific folder. Moving documents helps organize your content.

* The **docs** array contains the IDs of documents you want to move
* The **folderId** parameter specifies the destination folder

**Endpoint**

```http theme={null}
PUT /folders/move
```

**Request**

```http theme={null}
PUT /folders/move?token=468Yv8XfXBYNZF1pJ5rRLnAMeDHmQ95Q2ecInYbQ7j22idlgRIJTGAwfEX16vIkJ

Content-Type: application/json

{
  "docs": [
    {
      "id": "be36c2ac-c01c-4601-8732-d8428e06dfca"  //Document ID that you want to move
    }
  ],
  "folderId": "678bd175-ec83-4d32-8120-1d980f1287a2"  //Destination folder ID where documents will be moved
}
```

**Response**

```json theme={null}
{
  "errors": false,
  "count": 1,
  "items": []
}
```

The document has been successfully moved to the specified folder.

* The document is now located in the nested "Invoices" folder
* The document will now appear in the folder's contents when browsing or searching

> [!NOTE]
>
> You can move multiple documents at once by adding more document IDs to the docs array. This is useful for bulk organization of related documents.

***

## Nested Folders and Document Organization Summary

You have successfully learned how to create hierarchical folder structures and organize documents within them in the Circularo system.

### Key Concepts

* **Folder Hierarchy**: Create multi-level folder structures for logical document organization
* **Nested Folders**: Place folders within other folders to create a tree-like structure
* **Document Movement**: Relocate existing documents into appropriate folders

### Best Practices

* Design your folder structure before implementation to ensure it meets your organizational needs
* Use consistent naming conventions for folders to make navigation intuitive
* Regularly review and reorganize your folder structure as your document collection grows

### Example Implementation

> [!NOTE]
>
> See our OpenAPI documentation to learn about the full set of API endpoints and parameters.

> [!NOTE]
>
> Please use proper exception handling and function decomposition in your own code. The code is provided for illustrative purposes only and is not intended for production use.

```javascript theme={null}
// Nested folders and document organization example
const URL = "https://sandbox.circularo.com";
const API_PATH = "/api/v1";
const TOKEN = "YOUR_AUTH_TOKEN"; // Obtained from login or API key
const DOCUMENT_ID = "YOUR_DOCUMENT_ID"; // ID of an existing document to organize

try {
    // Step 1: Create a root folder
    const createRootFolderResponse = await fetch(`${URL}${API_PATH}/folders?token=${TOKEN}`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            name: "Completed Documents",
            isShared: false,
            parentFolderId: null
        })
    });
    if (!createRootFolderResponse.ok) {
        throw new Error(`Root folder creation failed: ${createRootFolderResponse.status} ${createRootFolderResponse.statusText}`);
    }

    const rootFolderData = await createRootFolderResponse.json();
    const rootFolderId = rootFolderData.id;
    console.log(`Root folder created with ID: ${rootFolderId}`);

    // Step 2: Create a nested folder within the root folder
    const createNestedFolderResponse = await fetch(`${URL}${API_PATH}/folders?token=${TOKEN}`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            name: "Invoices",
            isShared: false,
            parentFolderId: rootFolderId
        })
    });
    if (!createNestedFolderResponse.ok) {
        throw new Error(`Nested folder creation failed: ${createNestedFolderResponse.status} ${createNestedFolderResponse.statusText}`);
    }

    const nestedFolderData = await createNestedFolderResponse.json();
    const nestedFolderId = nestedFolderData.id;
    console.log(`Nested folder created with ID: ${nestedFolderId}`);

    // Step 3: Move an existing document into the nested folder
    const moveDocumentResponse = await fetch(`${URL}${API_PATH}/folders/move?token=${TOKEN}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            docs: [
                { id: DOCUMENT_ID }
            ],
            folderId: nestedFolderId
        })
    });
    if (!moveDocumentResponse.ok) {
        throw new Error(`Document move failed: ${moveDocumentResponse.status} ${moveDocumentResponse.statusText}`);
    }

    const moveResult = await moveDocumentResponse.json();
    if (moveResult.errors === false) {
        console.log(`Successfully moved ${moveResult.count} document(s) to the nested folder`);
    } else {
        console.error('Some documents could not be moved:', moveResult.items);
    }

} catch (error) {
    console.error('Error in folder and document organization:', error.message);
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.