Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
APPLIES TO:
NoSQL
This article shows you how to connect to Azure Cosmos DB for NoSQL using the JavaScript SDK. Once connected, you can perform operations on databases, containers, and items.
Package (npm) | Samples | API reference | Library source code | Give Feedback
Create a new directory for your JavaScript project in a bash shell.
mkdir cosmos-db-nosql-javascript-samples && cd ./cosmos-db-nosql-javascript-samples
Create a new JavaScript application by using the npm init
command with the console template.
npm init -y
Install the required dependency for the Azure Cosmos DB for NoSQL JavaScript SDK.
npm install @azure/cosmos
To connect to the API for NoSQL of Azure Cosmos DB, create an instance of the CosmosClient
class. This class is the starting point to perform all operations against databases.
To connect to your API for NoSQL account using the Microsoft Entra, use a security principal. The exact type of principal depends on where you host your application code. The table below serves as a quick reference guide.
Where the application runs | Security principal |
---|---|
Local machine (developing and testing) | User identity or service principal |
Azure | Managed identity |
Servers or clients outside of Azure | Service principal |
The @azure/identity npm package contains core authentication functionality that is shared among all Azure SDK libraries.
Import the @azure/identity npm package using the npm install
command.
npm install @azure/identity
In your code editor, add the dependencies.
const { DefaultAzureCredential } = require("@azure/identity");
If you're testing on a local machine, or your application will run on Azure services with direct support for managed identities, obtain an OAuth token by creating a DefaultAzureCredential
instance. Then create a new instance of the CosmosClient class with the COSMOS_ENDPOINT
environment variable and the TokenCredential object as parameters.
const { CosmosClient } = require("@azure/cosmos");
const { DefaultAzureCredential } = require("@azure/identity");
const credential = new DefaultAzureCredential();
const cosmosClient = new CosmosClient({
endpoint,
aadCredentials: credential
});
As you build your application, your code will primarily interact with four types of resources:
The API for NoSQL account, which is the unique top-level namespace for your Azure Cosmos DB data.
Databases, which organize the containers in your account.
Containers, which contain a set of individual items in your database.
Items, which represent a JSON document in your container.
The following diagram shows the relationship between these resources.
Hierarchical diagram showing an Azure Cosmos DB account at the top. The account has two child database nodes. One of the database nodes includes two child container nodes. The other database node includes a single child container node. That single container node has three child item nodes.
Each type of resource is represented by one or more associated classes. Here's a list of the most common classes:
Class | Description |
---|---|
CosmosClient |
This class provides a client-side logical representation for the Azure Cosmos DB service. The client object is used to configure and execute requests against the service. |
Database |
This class is a reference to a database that may, or may not, exist in the service yet. The database is validated server-side when you attempt to access it or perform an operation against it. |
Container |
This class is a reference to a container that also may not exist in the service yet. The container is validated server-side when you attempt to work with it. |
The following guides show you how to use each of these classes to build your application.
Guide | Description |
---|---|
Create a database | Create databases |
Create a container | Create containers |
Create and read an item | Point read a specific item |
Query items | Query multiple items |
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Learning path
Get started with Azure Cosmos DB for NoSQL - Training
Get started with Azure Cosmos DB for NoSQL
Certification
Microsoft Certified: Azure Cosmos DB Developer Specialty - Certifications
Write efficient queries, create indexing policies, manage, and provision resources in the SQL API and SDK with Microsoft Azure Cosmos DB.
Documentation
Quickstart - Azure SDK for Node.js - Azure Cosmos DB for NoSQL
Deploy a Node.js Express web application that uses the Azure SDK for Node.js to interact with Azure Cosmos DB for NoSQL data in this quickstart.
Query items in Azure Cosmos DB for NoSQL using JavaScript
Learn how to query items in your Azure Cosmos DB for NoSQL account using the JavaScript SDK.
Azure Cosmos DB client library for JavaScript