# Azure - CosmosDB

> Execute queries and CRUD operations on Azure CosmosDB NoSQL containers.

Source: https://docs.sommatic.ai/en/nodes/integrations.microsoft.azure.cosmos/

- Slug: `integrations.microsoft.azure.cosmos`
- Family: Integrations
- Version: 1.0.0
- Status: Available
- Execution: async

Execute queries and CRUD operations on Azure CosmosDB NoSQL containers.

Connect your Azure CosmosDB account and automate database operations — run SQL queries, read documents by ID, upsert records, and delete items.

**Operations:**
- **Query:** Execute a SQL query against a container with optional parameters.
- **Read:** Retrieve a single document by its ID and partition key.
- **Upsert:** Create or update a document in the container.
- **Delete:** Remove a document by its ID and partition key.

## Configuration

- `connection_id` (string, required): Credential for Azure CosmosDB (SQL API). Accepts credentials of type `connection-string` (AccountEndpoint=...;AccountKey=...;) or `basic-auth` (endpoint + key).
- `database` (string, required, accepts expressions): Name of the CosmosDB database.
- `container` (string, required, accepts expressions): Name of the container to operate on.
- `operation` (string, optional): The database operation to perform. Default: `query`. Allowed values: `query`, `read`, `upsert`, `delete`.
- `query` (string, optional, accepts expressions): SQL query to execute against the container (e.g., "SELECT * FROM c WHERE c.status = @status"). Shown when: operation = `query`.
- `parameters` (array, optional): Array of { name, value } objects for parameterized SQL queries. Shown when: operation = `query`.
- `item` (object, optional, accepts expressions): The document object to upsert into the container. Shown when: operation = `upsert`.
- `partitionKey` (string, optional, accepts expressions): Partition key value for the target document. Shown when: operation = `read` | `upsert` | `delete`.

## Output ports

- `out` (default): Operation result or items
- `error`: Error occurred during execution

## Input contract

- `connection_id` (string, required)
- `database` (string, required)
- `container` (string, required)
- `operation` (string, optional)
- `query` (string, optional)
- `parameters` (array, optional)
- `item` (object, optional)
- `partitionKey` (string, optional)

## Output contract

- `result` (object, optional): Cosmos response Resource or FeedResponse

## In expressions

What a later node can read from this node's output with `$("Azure - CosmosDB").item.json.<path>` (write your node's label instead of `Azure - CosmosDB`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Azure - CosmosDB").item.json.result` (object) = `[…] (1)`: Cosmos response Resource or FeedResponse


## How it works

Runs one Azure Cosmos DB (SQL API) operation on a container: a parameterized SQL query, a point read, an upsert or a delete, and returns the result under `result`.

1. Resolves the expressions of the node config. Empty `connection_id`, `database`, `container`, `operation`, `query`, `parameters` and `partitionKey` fall back to the field of the same name in the incoming item; `item` is taken from the config only when it has at least one key. `operation` defaults to `query`.
2. Resolves the account endpoint and key from: a raw `AccountEndpoint=…;AccountKey=…;` string in `connection_id`; a `connection-string` credential with that format; or a `basic-auth` / `object` credential with `endpoint` and `key` (or `password` / `accountKey`).
3. Runs the operation: `query` executes `query` with `parameters` and reads every page; `read` and `delete` address `item.id` with `partitionKey` (defaults to `item.id`); `upsert` writes `item`.
4. Returns `{ result }` on `out`: an array of documents for `query`, the document for `read` and `upsert`, and `{ deleted: true, id }` for `delete`. Failures are returned as `{ error }` on `error`.

## In a workflow

### Node

```json
{
  "node_id": "node-open-orders",
  "label": "Find open orders",
  "operator_slug": "integrations.microsoft.azure.cosmos",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "database": "sales",
    "container": "orders",
    "operation": "query",
    "query": "SELECT * FROM c WHERE c.status = @status",
    "parameters": [
      {
        "name": "@status",
        "value": "open"
      }
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": [
      {
        "id": "ord_123",
        "status": "open",
        "total": 129.99,
        "_rid": "q7x1AKl0VxsBAAAAAAAAAA==",
        "_etag": "\"0b00a5c1-0000-0100-0000-6745f2b30000\"",
        "_ts": 1764165811
      }
    ]
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The operation succeeded. A query with no matches also leaves here, with `result: []`.

```json
{
  "result": [
    {
      "id": "ord_123",
      "status": "open",
      "total": 129.99,
      "_rid": "q7x1AKl0VxsBAAAAAAAAAA==",
      "_etag": "\"0b00a5c1-0000-0100-0000-6745f2b30000\"",
      "_ts": 1764165811
    }
  ]
}
```

### `error`

Validation, credential resolution or the Cosmos call failed.

```json
{
  "error": "Query string required for query operation"
}
```

## Behaviour in the code

- `item`: Used by `read` and `delete` (only its `id`) as well as by `upsert` (the whole document). An empty object is treated as missing and replaced by the incoming item's `item` field.
- `partitionKey`: Ignored by `upsert` (the SDK takes the partition key from the document). For `read` and `delete` it defaults to `item.id`, which is only correct when the container is partitioned by `/id`.
- `parameters`: Array of `{ "name": "@status", "value": "open" }` objects matching the `@` placeholders in `query`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required` | `error` | No credential in the config or in the incoming item. |
| `Query string required for query operation` | `error` | `operation` is `query` and `query` is empty. |
| `item.id required for <read\|delete>` | `error` | `read` and `delete` need `item` with an `id`. Set `item` to `{ "id": "…" }` even though the form only shows it for upsert. |
| `item object required for upsert` | `error` | `upsert` without a document. |
| `Unsupported operation: <operation>` | `error` | `operation` is not query, read, upsert or delete. |
| `Invalid Cosmos SQL connection string format` | `error` | The connection string lacks `AccountEndpoint=` or `AccountKey=`. |
| `Credential <connection_id> (type '<type>') missing endpoint / key` | `error` | A structured credential lacks the endpoint or the key. |

## Notes and limits

- `query` loads every page into memory. Add `TOP` or `OFFSET … LIMIT` to bound large containers.
- A `read` of a missing document does not fail: the SDK returns no resource, so the node leaves through `out` without a `result` value.
- There is no `empty` port and the worker adds no retries of its own.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "integrations.microsoft.azure.cosmos",
  "name": "Azure - CosmosDB",
  "category": "integrations",
  "group": [
    {
      "key": "microsoft",
      "name": "Microsoft",
      "icon_url": "/icons/integrations/microsoft/microsoft.svg"
    },
    {
      "key": "azure",
      "name": "Azure",
      "icon_url": "/icons/integrations/microsoft/azure.svg"
    }
  ],
  "icon_url": "/icons/integrations/microsoft/azure-cosmos.svg",
  "version": "1.0.0",
  "description": "Execute queries and CRUD operations on Azure CosmosDB NoSQL containers.\n\n  Connect your Azure CosmosDB account and automate database operations — run SQL queries, read documents by ID, upsert records, and delete items.\n\n  **Operations:**\n  - **Query:** Execute a SQL query against a container with optional parameters.\n  - **Read:** Retrieve a single document by its ID and partition key.\n  - **Upsert:** Create or update a document in the container.\n  - **Delete:** Remove a document by its ID and partition key.",
  "capabilities": [
    "nosql",
    "database",
    "cosmos",
    "azure",
    "cloud"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "CosmosDB Credential",
        "description": "Credential for Azure CosmosDB (SQL API). Accepts credentials of type `connection-string` (AccountEndpoint=...;AccountKey=...;) or `basic-auth` (endpoint + key).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "connection-string",
            "basic-auth"
          ],
          "types": [
            "cosmos"
          ]
        }
      },
      "database": {
        "type": "string",
        "title": "Database",
        "description": "Name of the CosmosDB database.",
        "format": "expression"
      },
      "container": {
        "type": "string",
        "title": "Container",
        "description": "Name of the container to operate on.",
        "format": "expression"
      },
      "operation": {
        "type": "string",
        "title": "Operation",
        "enum": [
          "query",
          "read",
          "upsert",
          "delete"
        ],
        "default": "query",
        "description": "The database operation to perform."
      },
      "query": {
        "type": "string",
        "title": "SQL Query",
        "description": "SQL query to execute against the container (e.g., \"SELECT * FROM c WHERE c.status = @status\").",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "query"
            ]
          }
        }
      },
      "parameters": {
        "type": "array",
        "title": "Query Parameters",
        "description": "Array of { name, value } objects for parameterized SQL queries.",
        "displayOptions": {
          "show": {
            "operation": [
              "query"
            ]
          }
        }
      },
      "item": {
        "type": "object",
        "title": "Document",
        "description": "The document object to upsert into the container.",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "upsert"
            ]
          }
        }
      },
      "partitionKey": {
        "type": "string",
        "title": "Partition Key",
        "description": "Partition key value for the target document.",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "read",
              "upsert",
              "delete"
            ]
          }
        }
      }
    },
    "required": [
      "connection_id",
      "database",
      "container"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "database": {
        "type": "string"
      },
      "container": {
        "type": "string"
      },
      "operation": {
        "type": "string"
      },
      "query": {
        "type": "string"
      },
      "parameters": {
        "type": "array"
      },
      "item": {
        "type": "object"
      },
      "partitionKey": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "database",
      "container"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "Cosmos response Resource or FeedResponse"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Result",
      "description": "Operation result or items",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Error occurred during execution",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "cloud-io"
  ],
  "tags": [
    "nosql",
    "azure"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://sommatic.ai",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "CosmosDB Credential",
      "description": "Credential for Azure CosmosDB (SQL API). Accepts credentials of type `connection-string` (AccountEndpoint=...;AccountKey=...;) or `basic-auth` (endpoint + key).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "connection-string",
          "basic-auth"
        ],
        "types": [
          "cosmos"
        ]
      }
    },
    "database": {
      "type": "string",
      "title": "Database",
      "description": "Name of the CosmosDB database.",
      "format": "expression"
    },
    "container": {
      "type": "string",
      "title": "Container",
      "description": "Name of the container to operate on.",
      "format": "expression"
    },
    "operation": {
      "type": "string",
      "title": "Operation",
      "enum": [
        "query",
        "read",
        "upsert",
        "delete"
      ],
      "default": "query",
      "description": "The database operation to perform."
    },
    "query": {
      "type": "string",
      "title": "SQL Query",
      "description": "SQL query to execute against the container (e.g., \"SELECT * FROM c WHERE c.status = @status\").",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "query"
          ]
        }
      }
    },
    "parameters": {
      "type": "array",
      "title": "Query Parameters",
      "description": "Array of { name, value } objects for parameterized SQL queries.",
      "displayOptions": {
        "show": {
          "operation": [
            "query"
          ]
        }
      }
    },
    "item": {
      "type": "object",
      "title": "Document",
      "description": "The document object to upsert into the container.",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "upsert"
          ]
        }
      }
    },
    "partitionKey": {
      "type": "string",
      "title": "Partition Key",
      "description": "Partition key value for the target document.",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "read",
            "upsert",
            "delete"
          ]
        }
      }
    }
  },
  "required": [
    "connection_id",
    "database",
    "container"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "database": {
      "type": "string"
    },
    "container": {
      "type": "string"
    },
    "operation": {
      "type": "string"
    },
    "query": {
      "type": "string"
    },
    "parameters": {
      "type": "array"
    },
    "item": {
      "type": "object"
    },
    "partitionKey": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "database",
    "container"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "Cosmos response Resource or FeedResponse"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Result",
    "description": "Operation result or items",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Error occurred during execution",
    "is_default": false,
    "supports_condition": false
  }
]
```
