Skip to content

Commit 4a0121d

Browse files
committed
edits
1 parent d38bf8b commit 4a0121d

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

articles/cosmos-db/nosql/how-to-javascript-get-started.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export COSMOS_KEY="<cosmos-account-PRIMARY-KEY>"
196196
Create a new instance of the **CosmosClient** class with the ``COSMOS_ENDPOINT`` and ``COSMOS_KEY`` environment variables as parameters.
197197
198198
```javascript
199-
199+
const client = new CosmosClient({ endpoint, key });
200200
```
201201
202202
### Connect with a connection string
@@ -296,16 +296,8 @@ export COSMOS_CONNECTION_STRING="<cosmos-account-PRIMARY-CONNECTION-STRING>"
296296
Create a new instance of the **CosmosClient** class with the ``COSMOS_CONNECTION_STRING`` environment variable as the only parameter.
297297
298298
```javascript
299-
// Get Cosmos Client
300-
import { CosmosClient } from "@azure/cosmos";
301-
302-
// Provide required connection from environment variables
303-
const key = process.env.COSMOS_KEY;
304-
// Endpoint format: https://YOUR-RESOURCE-NAME.documents.azure.com:443/
305-
const endpoint = process.env.COSMOS_ENDPOINT;
306-
307-
// Authenticate to Azure Cosmos DB
308-
const cosmosClient = new CosmosClient({ endpoint, key });
299+
// New instance of CosmosClient class using a connection string
300+
const cosmosClient = new CosmosClient(process.env.COSMOS_CONNECTION_STRING);
309301
```
310302
311303
### Connect using the Microsoft Identity Platform
@@ -335,16 +327,18 @@ The **@azure/identity** npm package contains core authentication functionality t
335327
```
336328
#### Create CosmosClient with default credential implementation
337329
338-
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``](/javascript/api/@azure/identity/defaultazurecredential) instance.
330+
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``](/javascript/api/@azure/identity/defaultazurecredential) instance. Then create a new instance of the **CosmosClient** class with the ``COSMOS_ENDPOINT`` environment variable and the **TokenCredential** object as parameters.
339331

340332
```javascript
333+
const { CosmosClient } = require("@azure/cosmos");
334+
const { DefaultAzureCredential } = require("@azure/identity");
341335
342-
```
343-
344-
Create a new instance of the **CosmosClient** class with the ``COSMOS_ENDPOINT`` environment variable and the **TokenCredential** object as parameters.
345-
346-
```javascript
336+
const credential = new DefaultAzureCredential();
347337
338+
const cosmosClient = new CosmosClient({
339+
endpoint,
340+
aadCredentials: credential
341+
});
348342
```
349343
350344
#### Create CosmosClient with a custom credential implementation
@@ -353,15 +347,24 @@ If you plan to deploy the application out of Azure, you can obtain an OAuth toke
353347
354348
For this example, we create a [``ClientSecretCredential``](/javascript/api/@azure/identity/tokencredential) instance by using client and tenant identifiers, along with a client secret.
355349
356-
```javascript
357-
```
358-
359350
You can obtain the client ID, tenant ID, and client secret when you register an application in Azure Active Directory (AD). For more information about registering Azure AD applications, see [Register an application with the Microsoft identity platform](../../active-directory/develop/quickstart-register-app.md).
360351
361352
Create a new instance of the **CosmosClient** class with the ``COSMOS_ENDPOINT`` environment variable and the **TokenCredential** object as parameters.
362353
363354
```javascript
364-
355+
const { CosmosClient } = require("@azure/cosmos");
356+
const { DefaultAzureCredential } = require("@azure/identity");
357+
358+
const credential = new ClientSecretCredential(
359+
tenantId: process.env.AAD_TENANT_ID,
360+
clientId: process.env.AAD_CLIENT_ID,
361+
clientSecret: process.env.AAD_CLIENT_SECRET
362+
);
363+
364+
const cosmosClient = new CosmosClient({
365+
endpoint,
366+
aadCredentials: credential
367+
});
365368
```
366369
367370
## Build your application

0 commit comments

Comments
 (0)