This example demonstrates how to use the Nylas Node.js SDK to work with folders.
- List all folders for a grant
- Use the
includeHiddenFoldersparameter (Microsoft only) to include hidden folders in the response - Filter folders by parent/child relationship
- Display folder attributes and metadata
- Copy
.env.exampleto.envin the examples directory - Add your Nylas API credentials and grant ID:
NYLAS_API_KEY=your_api_key_here NYLAS_GRANT_ID=your_grant_id_here NYLAS_API_URI=https://api.us.nylas.com # or your specific Nylas region
npm install
npm run foldersOr run directly with ts-node:
npx ts-node folders.tsThe includeHiddenFolders parameter is specific to Microsoft accounts:
const foldersWithHidden = await nylas.folders.list({
identifier: GRANT_ID,
queryParams: {
includeHiddenFolders: true, // Microsoft only - includes hidden folders
},
});This parameter defaults to false and when set to true, includes folders that are typically hidden from the user interface, such as system folders used by Microsoft Exchange for internal operations.
The example will output:
- A list of all visible folders
- A list of all folders including hidden ones (if using a Microsoft account)
- A filtered list showing only parent folders with their metadata
The example includes proper error handling and will display helpful error messages if:
- Environment variables are not set
- API requests fail
- Authentication issues occur =======
This directory contains examples of how to use the Nylas Folders API with the Nylas Node.js SDK, including the new singleLevel query parameter.
The singleLevel parameter is a new query parameter for the "list all folders" endpoint that controls folder hierarchy traversal:
singleLevel: true- Retrieves folders from a single-level hierarchy only (direct children)singleLevel: false- Retrieves folders across a multi-level hierarchy (all descendants, default behavior)- Microsoft accounts only - This parameter is ignored for other providers
- folders.ts - A comprehensive example showing how to use the
singleLevelparameter in various scenarios
To run these examples, you'll need to:
-
Install dependencies from the examples directory:
cd examples npm install -
Copy the
.env.examplefile to.envif you haven't already and add your credentials:cp .env.example .env # Edit .env with your editor -
Edit the
.envfile to include:NYLAS_API_KEY- Your Nylas API keyNYLAS_API_URI(optional) - The Nylas API server URI (defaults to "https://api.us.nylas.com")NYLAS_GRANT_ID- The Grant ID for a Microsoft account to see thesingleLevelparameter in action
-
Run the example:
# From the examples directory npx ts-node folders/folders.ts # Or if you add it to package.json scripts npm run folders
This example demonstrates:
- Default Behavior: Listing all folders with multi-level hierarchy (default)
- Single-Level Listing: Using
singleLevel: trueto get only direct children - Combined Parameters: Using
singleLevelwithparentIdto control the starting point - Comparison: Side-by-side comparison of single-level vs multi-level results
- Parameter Variations: All possible combinations of the
singleLevelparameter
The singleLevel parameter is particularly useful when:
- Building UI Navigation: You want to show only immediate child folders in a tree view
- Performance Optimization: Reducing the amount of data returned when you only need direct children
- Hierarchical Processing: Processing folder structures level by level rather than all at once
- Microsoft-Specific Features: Taking advantage of Microsoft's folder hierarchy capabilities
interface ListFolderQueryParams extends ListQueryParams {
/**
* (Microsoft and EWS only.) Use the ID of a folder to find all child folders it contains.
*/
parentId?: string;
/**
* (Microsoft only) If true, retrieves folders from a single-level hierarchy only.
* If false, retrieves folders across a multi-level hierarchy.
* @default false
*/
singleLevel?: boolean;
}import Nylas from 'nylas';
const nylas = new Nylas({ apiKey: 'your-api-key' });
// Get only direct children of a specific folder
const singleLevelFolders = await nylas.folders.list({
identifier: 'grant-id',
queryParams: {
parentId: 'parent-folder-id',
singleLevel: true
}
});
// Get all descendants (default behavior)
const allFolders = await nylas.folders.list({
identifier: 'grant-id',
queryParams: {
parentId: 'parent-folder-id',
singleLevel: false // or omit this parameter
}
});For more information, see the Nylas API Documentation.