Skip to content

Commit 37e8977

Browse files
authored
Merge pull request #294631 from maud-lv/ml-EID-quickstart-js
Add Entra ID tab to quickstart-javascript
2 parents 734f005 + cbe50a6 commit 37e8977

File tree

1 file changed

+153
-95
lines changed

1 file changed

+153
-95
lines changed

articles/azure-app-configuration/quickstart-javascript.md

Lines changed: 153 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ ms.service: azure-app-configuration
77
ms.devlang: javascript
88
ms.topic: sample
99
ms.custom: mode-other, devx-track-js
10-
ms.date: 03/20/2023
10+
ms.date: 02/20/2025
1111
ms.author: malev
1212
#Customer intent: As a JavaScript developer, I want to manage all my app settings in one place.
1313
---
1414
# Create a Node.js app with the Azure SDK for JavaScript
1515

1616
This document shows examples of how to use the [Azure SDK for JavaScript](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/appconfiguration/app-configuration) to access key-values in Azure App Configuration.
1717

18-
>[!TIP]
18+
>[!IMPORTANT]
1919
> App Configuration offers a JavaScript provider library that is built on top of the JavaScript SDK and is designed to be easier to use with richer features. It enables configuration settings to be used like a Map object, and offers other features like configuration composition from multiple labels, key name trimming, and automatic resolution of Key Vault references. Go to the [JavaScript quickstart](./quickstart-javascript-provider.md) to learn more.
2020
2121
## Prerequisites
@@ -68,76 +68,6 @@ Add the following key-value to the App Configuration store and leave **Label** a
6868
> [!NOTE]
6969
> The code snippets in this example will help you get started with the App Configuration client library for JavaScript. For your application, you should also consider handling exceptions according to your needs. To learn more about exception handling, please refer to our [JavaScript SDK documentation](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/appconfiguration/app-configuration).
7070

71-
## Configure your App Configuration connection string
72-
73-
1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command:
74-
75-
### [Windows command prompt](#tab/windowscommandprompt)
76-
77-
To run the app locally using the Windows command prompt, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
78-
79-
```cmd
80-
setx AZURE_APPCONFIG_CONNECTION_STRING "<app-configuration-store-connection-string>"
81-
```
82-
83-
### [PowerShell](#tab/powershell)
84-
85-
If you use Windows PowerShell, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
86-
87-
```azurepowershell
88-
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>"
89-
```
90-
91-
### [macOS](#tab/unix)
92-
93-
If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
94-
95-
```console
96-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
97-
```
98-
99-
### [Linux](#tab/linux)
100-
101-
If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
102-
103-
```console
104-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
105-
```
106-
107-
1. Print out the value of the environment variable to validate that it is set properly with the command below.
108-
109-
### [Windows command prompt](#tab/windowscommandprompt)
110-
111-
Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command:
112-
113-
```cmd
114-
echo %AZURE_APPCONFIG_CONNECTION_STRING%
115-
```
116-
117-
### [PowerShell](#tab/powershell)
118-
119-
If you use Windows PowerShell, run the following command:
120-
121-
```azurepowershell
122-
$Env:AZURE_APPCONFIG_CONNECTION_STRING
123-
```
124-
125-
### [macOS](#tab/unix)
126-
127-
If you use macOS, run the following command:
128-
129-
```console
130-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
131-
```
132-
133-
### [Linux](#tab/linux)
134-
135-
If you use Linux, run the following command:
136-
137-
```console
138-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
139-
```
140-
14171
## Code samples
14272

14373
The sample code snippets in this section show you how to perform common operations with the App Configuration client library for JavaScript. Add these code snippets to the body of `run` function in *app-configuration-example.js* file you created earlier.
@@ -158,12 +88,31 @@ Learn below how to:
15888

15989
### Connect to an App Configuration store
16090

161-
The following code snippet creates an instance of **AppConfigurationClient** using the connection string stored in your environment variables.
91+
The following code snippet creates an instance of **AppConfigurationClient**. You can connect to your App Configuration store using Microsoft Entra ID (recommended), or a connection string.
92+
93+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
94+
95+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
16296

16397
```javascript
98+
const { DefaultAzureCredential } = require("@azure/identity");
99+
const { AppConfigurationClient } = require("@azure/app-configuration");
100+
101+
const client = new AppConfigurationClient(
102+
process.env.AZURE_APPCONFIG_ENDPOINT,
103+
new DefaultAzureCredential()
104+
);
105+
```
106+
107+
### [Connection string](#tab/connection-string)
108+
109+
```javascript
110+
const { AppConfigurationClient } = require("@azure/app-configuration");
111+
164112
const connection_string = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
165113
const client = new AppConfigurationClient(connection_string);
166114
```
115+
---
167116

168117
### Get a configuration setting
169118

@@ -253,14 +202,19 @@ In this example, you created a Node.js app that uses the Azure App Configuration
253202

254203
At this point, your *app-configuration-example.js* file should have the following code:
255204

205+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
206+
256207
```javascript
208+
const { DefaultAzureCredential } = require("@azure/identity");
257209
const { AppConfigurationClient } = require("@azure/app-configuration");
258210

259211
async function run() {
260212
console.log("Azure App Configuration - JavaScript example");
261213

262-
const connection_string = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
263-
const client = new AppConfigurationClient(connection_string);
214+
const client = new AppConfigurationClient(
215+
process.env.AZURE_APPCONFIG_ENDPOINT,
216+
new DefaultAzureCredential()
217+
);
264218

265219
const retrievedConfigSetting = await client.getConfigurationSetting({
266220
key: "TestApp:Settings:Message"
@@ -305,37 +259,141 @@ async function run() {
305259
run().catch(console.error);
306260
```
307261

308-
In your console window, navigate to the directory containing the *app-configuration-example.js* file and execute the following command to run the app:
262+
### [Connection string](#tab/connection-string)
309263

310-
```console
311-
node app.js
312-
```
264+
```javascript
265+
const { AppConfigurationClient } = require("@azure/app-configuration");
313266

314-
You should see the following output:
267+
async function run() {
268+
console.log("Azure App Configuration - JavaScript example");
315269

316-
```output
317-
Azure App Configuration - JavaScript example
270+
const connection_string = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
271+
const client = new AppConfigurationClient(connection_string);
318272

319-
Retrieved configuration setting:
320-
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
273+
const retrievedConfigSetting = await client.getConfigurationSetting({
274+
key: "TestApp:Settings:Message"
275+
});
276+
console.log("\nRetrieved configuration setting:");
277+
console.log(`Key: ${retrievedConfigSetting.key}, Value: ${retrievedConfigSetting.value}`);
321278

322-
Added configuration setting:
323-
Key: TestApp:Settings:NewSetting, Value: New setting value
279+
const configSetting = {
280+
key: "TestApp:Settings:NewSetting",
281+
value: "New setting value"
282+
};
283+
const addedConfigSetting = await client.addConfigurationSetting(configSetting);
284+
console.log("Added configuration setting:");
285+
console.log(`Key: ${addedConfigSetting.key}, Value: ${addedConfigSetting.value}`);
324286

325-
Retrieved list of configuration settings:
326-
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
327-
Key: TestApp:Settings:NewSetting, Value: New setting value
287+
const filteredSettingsList = client.listConfigurationSettings({
288+
keyFilter: "TestApp*"
289+
});
290+
console.log("Retrieved list of configuration settings:");
291+
for await (const filteredSetting of filteredSettingsList) {
292+
console.log(`Key: ${filteredSetting.key}, Value: ${filteredSetting.value}`);
293+
}
328294

329-
Read-only status for TestApp:Settings:NewSetting: true
295+
const lockedConfigSetting = await client.setReadOnly(addedConfigSetting, true /** readOnly */);
296+
console.log(`Read-only status for ${lockedConfigSetting.key}: ${lockedConfigSetting.isReadOnly}`);
330297

331-
Read-only status for TestApp:Settings:NewSetting: false
298+
const unlockedConfigSetting = await client.setReadOnly(lockedConfigSetting, false /** readOnly */);
299+
console.log(`Read-only status for ${unlockedConfigSetting.key}: ${unlockedConfigSetting.isReadOnly}`);
332300

333-
Updated configuration setting:
334-
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
301+
addedConfigSetting.value = "Value has been updated!";
302+
const updatedConfigSetting = await client.setConfigurationSetting(addedConfigSetting);
303+
console.log("Updated configuration setting:");
304+
console.log(`Key: ${updatedConfigSetting.key}, Value: ${updatedConfigSetting.value}`);
305+
306+
const deletedConfigSetting = await client.deleteConfigurationSetting({
307+
key: "TestApp:Settings:NewSetting"
308+
});
309+
console.log("Deleted configuration setting:");
310+
console.log(`Key: ${deletedConfigSetting.key}, Value: ${deletedConfigSetting.value}`);
311+
}
335312

336-
Deleted configuration setting:
337-
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
313+
run().catch(console.error);
338314
```
315+
---
316+
317+
## Configure an environment variable
318+
319+
1. Configure an environment variable using Microsoft Entra ID (recommended) or a connection string.
320+
321+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
322+
323+
Set an environment variable named **AZURE_APPCONFIG_ENDPOINT** to the endpoint of your App Configuration store found under the **Overview** of your store in the Azure portal.
324+
325+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
326+
327+
```cmd
328+
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
329+
```
330+
331+
If you use PowerShell, run the following command:
332+
333+
```powershell
334+
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
335+
```
336+
337+
If you use macOS or Linux, run the following command:
338+
339+
```bash
340+
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
341+
```
342+
343+
### [Connection string](#tab/connection-string)
344+
345+
Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING** to the read-only connection string of your App Configuration store found under **Access keys** of your store in the Azure portal.
346+
347+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
348+
349+
```cmd
350+
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
351+
```
352+
353+
If you use PowerShell, run the following command:
354+
355+
```powershell
356+
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
357+
```
358+
359+
If you use macOS or Linux, run the following command:
360+
361+
```bash
362+
export AZURE_APPCONFIG_CONNECTION_STRING='<connection-string-of-your-app-configuration-store>'
363+
```
364+
---
365+
366+
1. In your console window, navigate to the directory containing the *app-configuration-example.js* file and execute the following command to run the app:
367+
368+
```console
369+
node app.js
370+
```
371+
372+
You should see the following output:
373+
374+
```output
375+
Azure App Configuration - JavaScript example
376+
377+
Retrieved configuration setting:
378+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
379+
380+
Added configuration setting:
381+
Key: TestApp:Settings:NewSetting, Value: New setting value
382+
383+
Retrieved list of configuration settings:
384+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
385+
Key: TestApp:Settings:NewSetting, Value: New setting value
386+
387+
Read-only status for TestApp:Settings:NewSetting: true
388+
389+
Read-only status for TestApp:Settings:NewSetting: false
390+
391+
Updated configuration setting:
392+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
393+
394+
Deleted configuration setting:
395+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
396+
```
339397
340398
## Clean up resources
341399

0 commit comments

Comments
 (0)