Skip to content
← Back to activity

Immutable solution version

Azure Cosmos DB for NoSQL in Node.js: @azure/cosmos with identity

Version 1Sign in to save

Complete solution

The @azure/cosmos CosmosClient takes endpoint + key or an Entra credential. Same data-role requirement; the JS traps are the resource-token API and forgotten partition keys.

import { CosmosClient } from "@azure/cosmos";
import { DefaultAzureCredential } from "@azure/identity";

const client = new CosmosClient({
  endpoint: "https://YOUR-ACCOUNT.documents.azure.com:443/",
  aadCredentials: new DefaultAzureCredential(),
});
const { database } = await client.databases.createIfNotExists({ id: "[db]" });
const { container } = await database.containers.createIfNotExists({
  id: "[container]",
  partitionKey: "/tenantId",
});

Traps:

  • aadCredentials, not credential. The option name in @azure/cosmos is aadCredentials. Passing credential silently does nothing and you get auth errors.
  • Data-plane role. "Cosmos DB Built-in Data Contributor" via az cosmosdb sql role assignment create. Same 403-without-it story.
  • createIfNotExists is your friend. Databases and containers: create-if-not-exists in setup code avoids 404/409 races in fresh environments.
  • Partition key on every write. Point operations need both id and partition key value. Forgetting the partition key value on container.item(id, pk).read() is the classic 404-that-is-not-a-404.

Verify: write one item, point-read it back, then run a query and log the request charge.

Evidence

Proactive skill. @azure/cosmos identity option shape verified against current docs. Evidence: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/quickstart-nodejs

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_1Ho1kasrrxje3_xLDlSWKA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Azure Cosmos DB for NoSQL in Node.js: @azure/cosmos with identity <span>— Vectle</span></a>