Description
n8n-nodes-azure-cosmos-sdk
This is an n8n community node for Azure Cosmos DB. It provides complete SQL query freedom using the official Azure Cosmos DB SDK, enabling advanced features like hybrid search, vector similarity search, and Microsoft Entra ID authentication.
Why Use This Node?
Unlike the native n8n Cosmos DB node (which uses REST API), this implementation:
- ✅ Uses Azure Cosmos DB SDK (not REST API) for full feature support
- ✅ Complete query freedom – write any SQL query including hybrid search
- ✅ Vector similarity search – supports
VectorDistance()and hybrid search queries - ✅ Vector field exclusion – optionally exclude large embedding fields to reduce payload
- ✅ Role-Based Access Control (RBAC) – supports Microsoft Entra ID authentication with granular permissions
- ✅ Modern SDK features – access to latest Cosmos DB capabilities
- Installation
- Operations
- Credentials
- Compatibility
- Usage
- Resources
> Note: Hybrid search and vector similarity features (VectorDistance(), RRF(), etc.) are only available through the SDK. The NoSQL REST API does not support these advanced query capabilities. This is why this node uses the official Azure Cosmos DB SDK instead of REST API.
Azure Cosmos DB is a fully managed NoSQL and relational database for modern app development with vector database support.
n8n is a fair-code licensed workflow automation platform.
Installation
Follow the installation guide in the n8n community nodes documentation.
Development
Install dependencies
npm installBuild the node
npm run buildRun in development mode
npm run dev
Operations
This node supports comprehensive operations for Azure Cosmos DB databases and containers:
Database Operations
#### Create Database
Create a new Cosmos DB database:
#### Delete Database
Delete a database and all its containers:
Container Operations
#### Create Container
Create a new container with advanced indexing:
/category, /userId) – Vector path, type (float32/int8/uint8), dimensions
– Distance function (cosine, dotproduct, euclidean)
– Index type (quantizedFlat, diskANN, flat)
#### Delete Container
Delete a container from a database:
Document Operations
#### Select (Query Documents)
Execute full SQL queries with complete freedom:
VectorDistance() function for semantic searchExample Queries:
Basic Query:
SELECT * FROM c WHERE c.status = "active"
Vector Similarity Search:
SELECT TOP 10 c.id, c.title, VectorDistance(c.embedding, [0.1, 0.2, ...]) AS similarity
FROM c
ORDER BY VectorDistance(c.embedding, [0.1, 0.2, ...])
Hybrid Search (Vector + Filters):
SELECT TOP 10 c.id, c.title, VectorDistance(c.embedding, [0.1, 0.2, ...]) AS similarity
FROM c
WHERE c.category = "research" AND c.year >= 2023
ORDER BY VectorDistance(c.embedding, [0.1, 0.2, ...])
#### Insert (Create Document)
Insert new documents into a container:
rid, self, _etag, etc.)Example:
{
"id": "unique-id-123",
"name": "John Doe",
"email": "john@example.com",
"status": "active"
}
#### Create or Update (Upsert)
Create a new document or update if it already exists:
#### Delete
Delete documents from a container:
Credentials
This node supports two authentication methods:
Option 1: Master Key Authentication (Default)
1. Azure Cosmos DB Account: Sign up at Azure Portal
2. Endpoint URL: Your Cosmos DB account endpoint (e.g., https://your-account.documents.azure.com:443/)
3. Access Key: Primary or secondary key from Azure Portal → Your Cosmos DB Account → Keys
The credential test uses HMAC-SHA256 signature authentication with master keys to verify your connection by listing databases.
Option 2: Microsoft Entra ID (Azure AD) Authentication with RBAC
For enhanced security using OAuth2 user delegation and Role-Based Access Control (RBAC):
This credential extends n8n’s Microsoft OAuth2 API credential, which handles the OAuth2 authorization code flow and automatic token refresh.
RBAC Benefits:
Setup Steps:
1. Create an App Registration in Azure Portal → Microsoft Entra ID
2. Add redirect URI: https://your-n8n-instance/rest/oauth2-credential/callback
3. Under “API permissions”, add delegated permission: Azure Cosmos DB → user_impersonation
4. Grant admin consent for the permission
5. Assign Cosmos DB RBAC roles to users in Azure Portal → Cosmos DB Account → Access Control (IAM):
– Cosmos DB Built-in Data Reader – Read-only access to data
– Cosmos DB Built-in Data Contributor – Read and write access to data
– Custom roles for fine-grained control
6. In n8n, create a “Microsoft OAuth2 API” credential with:
– Scope: https://cosmos.azure.com/userimpersonation offlineaccess
– Your app’s Client ID and Client Secret
7. Create “Azure Cosmos DB SDK (Entra ID) API” credential:
– Select your Microsoft OAuth2 credential
– Enter your Cosmos DB endpoint URL
– Configure token refresh buffer (optional, default: 900 seconds)
Scopes Used: https://cosmos.azure.com/userimpersonation with offlineaccess for token refresh
The node uses user delegation (on-behalf-of the authenticated user) with the Azure Cosmos DB SDK. Access is controlled by the RBAC roles assigned to the user.
Compatibility
Usage
Create Database
1. Add the Azure Cosmos DB (SDK) node to your workflow
2. Select or create credentials
3. Choose Create Database operation
4. Enter:
– Database Name: Unique name for your database
– Throughput (RU/s): Set to 0 for serverless, or minimum 400 for provisioned
Create Container with Vector Search
1. Add the Azure Cosmos DB (SDK) node to your workflow
2. Select or create credentials
3. Choose Create Container operation
4. Select or enter Database name
5. Enter Container Name and Partition Key Path (e.g., /category)
6. Enable Vector Index and configure:
– Vector Path: /embedding or /vector
– Dimensions: 1536 (for OpenAI embeddings) or your model’s dimension
– Distance Function: cosine (recommended for most use cases)
– Index Type: quantizedFlat (balanced) or diskANN (high performance)
7. Optionally enable Full-Text Index with paths like /text,/content
Select Operation
1. Add the Azure Cosmos DB (SDK) node to your workflow
2. Select or create credentials
3. Choose Select operation
4. Enter:
– Database Name: Your Cosmos DB database name
– Container Name: Your container name
– SQL Query: Your SQL query (e.g., SELECT * FROM c WHERE c.status = "active")
Excluding Vector Fields:
When working with vector embeddings, you can reduce payload size:
1. Expand the Options section
2. Enable Exclude Vector Fields
3. Optionally customize Vector Field Names (default: vector,embedding,embeddings)
This is useful when vector data isn’t needed in downstream nodes.
Insert Operation
1. Add the Azure Cosmos DB (SDK) node to your workflow
2. Select or create credentials
3. Choose Insert operation
4. Enter:
– Database Name: Your Cosmos DB database name
– Container Name: Your container name
– Document: JSON document to insert
Tips:
id field is required and must be uniqueDelete Database or Container
1. Add the Azure Cosmos DB (SDK) node to your workflow
2. Select or create credentials
3. Choose Delete Database or Delete Container operation
4. Select the resource from the dropdown or enter manually
5. For containers, select the database first, then the container list will load automatically