Skip to content

Commit b5f4dcc

Browse files
authored
Merge pull request #281760 from diberry/diberry/ts-key-vault
Key Vault - TypeScript quickstarts
2 parents e91df4b + 181b14e commit b5f4dcc

File tree

6 files changed

+320
-86
lines changed

6 files changed

+320
-86
lines changed

articles/key-vault/certificates/quick-create-node.md

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
---
22
title: Quickstart - Azure Key Vault certificate client library for JavaScript (version 4)
3-
description: Learn how to create, retrieve, and delete certificates from an Azure key vault using the JavaScript client library
3+
description: Learn how to create, retrieve, and delete certificates from an Azure key vault using the JavaScript client library with either JavaScript or TypeScript
44
author: msmbaldwin
55
ms.author: mbaldwin
6-
ms.date: 02/01/2023
7-
ms.service: key-vault
6+
ms.date: 07/30/2024
7+
ms.service: azure-key-vault
88
ms.subservice: certificates
99
ms.topic: quickstart
1010
ms.devlang: javascript
11-
ms.custom: devx-track-js, mode-api, passwordless-js
11+
ms.custom: devx-track-js, mode-api, passwordless-js, devx-track-ts
12+
zone_pivot_groups: programming-languages-set-functions-nodejs
1213
---
1314

1415
# Quickstart: Azure Key Vault certificate client library for JavaScript
1516

16-
Get started with the Azure Key Vault certificate client library for JavaScript. [Azure Key Vault](../general/overview.md) is a cloud service that provides a secure store for certificates. You can securely store keys, passwords, certificates, and other secrets. Azure key vaults may be created and managed through the Azure portal. In this quickstart, you learn how to create, retrieve, and delete certificates from an Azure key vault using the JavaScript client library
17+
Get started with the Azure Key Vault certificate client library for JavaScript. [Azure Key Vault](../general/overview.md) is a cloud service that provides a secure store for certificates. You can securely store keys, passwords, certificates, and other secrets. Azure key vaults may be created and managed through the Azure portal. In this quickstart, you learn how to create, retrieve, and delete certificates from an Azure key vault using the JavaScript client library.
1718

1819
Key Vault client library resources:
1920

@@ -23,6 +24,8 @@ For more information about Key Vault and certificates, see:
2324
- [Key Vault Overview](../general/overview.md)
2425
- [Certificates Overview](about-certificates.md)
2526

27+
::: zone pivot="programming-language-javascript"
28+
2629
## Prerequisites
2730

2831
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
@@ -33,6 +36,19 @@ For more information about Key Vault and certificates, see:
3336
- [Azure portal](../general/quick-create-portal.md)
3437
- [Azure PowerShell](../general/quick-create-powershell.md)
3538

39+
::: zone-end
40+
41+
::: zone pivot="programming-language-typescript"
42+
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
43+
- Current [Node.js LTS](https://nodejs.org).
44+
- [TypeScript 5+](https://www.typescriptlang.org/download/)
45+
- [Azure CLI](/cli/azure/install-azure-cli)
46+
- An existing Key Vault - you can create one using:
47+
- [Azure CLI](../general/quick-create-cli.md)
48+
- [Azure portal](../general/quick-create-portal.md)
49+
- [Azure PowerShell](../general/quick-create-powershell.md)
50+
::: zone-end
51+
3652
This quickstart assumes you're running [Azure CLI](/cli/azure/install-azure-cli).
3753

3854
## Sign in to Azure
@@ -86,25 +102,25 @@ Create a Node.js application that uses your key vault.
86102
87103
## Set environment variables
88104
89-
This application is using key vault name as an environment variable called `KEY_VAULT_NAME`.
105+
This application is using key vault endpoint as an environment variable called `KEY_VAULT_URL`.
90106
91107
### [Windows](#tab/windows)
92108
93109
```cmd
94-
set KEY_VAULT_NAME=<your-key-vault-name>
110+
set KEY_VAULT_URL=<your-key-vault-endpoint>
95111
````
96112
97113
### [PowerShell](#tab/powershell)
98114
99115
Windows PowerShell
100116
```powershell
101-
$Env:KEY_VAULT_NAME="<your-key-vault-name>"
117+
$Env:KEY_VAULT_URL="<your-key-vault-endpoint>"
102118
```
103119

104120
### [macOS or Linux](#tab/linux)
105121

106122
```cmd
107-
export KEY_VAULT_NAME=<your-key-vault-name>
123+
export KEY_VAULT_URL=<your-key-vault-endpoint>
108124
```
109125
---
110126

@@ -114,7 +130,7 @@ Application requests to most Azure services must be authorized. Using the [Defau
114130

115131
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
116132

117-
In this code, the name of your key vault is used to create the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
133+
In this code, the endpoint of your key vault is used to create the key vault client. The endpoint format looks like `https://<your-key-vault-name>.vault.azure.net` but may change for sovereign clouds. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
118134

119135
## Code example
120136

@@ -134,7 +150,9 @@ This code uses the following [Key Vault Certificate classes and methods](/javasc
134150

135151
### Set up the app framework
136152

137-
1. Create new text file and paste the following code into the **index.js** file.
153+
::: zone pivot="programming-language-javascript"
154+
155+
* Create new text file and paste the following code into the **index.js** file.
138156

139157
```javascript
140158
const { CertificateClient, DefaultCertificatePolicy } = require("@azure/keyvault-certificates");
@@ -148,11 +166,10 @@ This code uses the following [Key Vault Certificate classes and methods](/javasc
148166
// - AZURE_CLIENT_SECRET: The client secret for the registered application
149167
const credential = new DefaultAzureCredential();
150168

151-
const keyVaultName = process.env["KEY_VAULT_NAME"];
152-
if(!keyVaultName) throw new Error("KEY_VAULT_NAME is empty");
153-
const url = "https://" + keyVaultName + ".vault.azure.net";
169+
const keyVaultUrl = process.env["KEY_VAULT_URL"];
170+
if(!keyVaultUrl) throw new Error("KEY_VAULT_URL is empty");
154171

155-
const client = new CertificateClient(url, credential);
172+
const client = new CertificateClient(keyVaultUrl, credential);
156173

157174
const uniqueString = new Date().getTime();
158175
const certificateName = `cert${uniqueString}`;
@@ -217,6 +234,60 @@ This code uses the following [Key Vault Certificate classes and methods](/javasc
217234
node index.js
218235
```
219236

237+
238+
1. The create and get methods return a full JSON object for the certificate:
239+
240+
```JSON
241+
{
242+
"keyId": undefined,
243+
"secretId": undefined,
244+
"name": "YOUR-CERTIFICATE-NAME",
245+
"reuseKey": false,
246+
"keyCurveName": undefined,
247+
"exportable": true,
248+
"issuerName": 'Self',
249+
"certificateType": undefined,
250+
"certificateTransparency": undefined
251+
},
252+
"properties": {
253+
"createdOn": 2021-11-29T20:17:45.000Z,
254+
"updatedOn": 2021-11-29T20:17:45.000Z,
255+
"expiresOn": 2022-11-29T20:17:45.000Z,
256+
"id": "https://YOUR-KEY-VAULT-NAME-ENDPOINT/certificates/YOUR-CERTIFICATE-NAME/YOUR-CERTIFICATE-VERSION",
257+
"enabled": false,
258+
"notBefore": 2021-11-29T20:07:45.000Z,
259+
"recoveryLevel": "Recoverable+Purgeable",
260+
"name": "YOUR-CERTIFICATE-NAME",
261+
"vaultUrl": "https://YOUR-KEY-VAULT-NAME-ENDPOINT",
262+
"version": "YOUR-CERTIFICATE-VERSION",
263+
"tags": undefined,
264+
"x509Thumbprint": undefined,
265+
"recoverableDays": 90
266+
}
267+
}
268+
```
269+
270+
271+
::: zone-end
272+
::: zone pivot="programming-language-typescript"
273+
* Create new text file and paste the following code into the **index.ts** file.
274+
275+
:::code language="typescript" source="~/azure-typescript-e2e-apps/quickstarts/key-vault/src/certificates.ts" :::
276+
277+
## Run the sample application
278+
279+
1. Build the TypeScript app:
280+
281+
```terminal
282+
tsc
283+
```
284+
285+
1. Run the app:
286+
287+
```terminal
288+
node index.js
289+
```
290+
220291
1. The create and get methods return a full JSON object for the certificate:
221292

222293
```JSON
@@ -235,12 +306,12 @@ This code uses the following [Key Vault Certificate classes and methods](/javasc
235306
"createdOn": 2021-11-29T20:17:45.000Z,
236307
"updatedOn": 2021-11-29T20:17:45.000Z,
237308
"expiresOn": 2022-11-29T20:17:45.000Z,
238-
"id": "https://YOUR-KEY-VAULT-NAME.vault.azure.net/certificates/YOUR-CERTIFICATE-NAME/YOUR-CERTIFICATE-VERSION",
309+
"id": "https://YOUR-KEY-VAULT-NAME-ENDPOINT/certificates/YOUR-CERTIFICATE-NAME/YOUR-CERTIFICATE-VERSION",
239310
"enabled": false,
240311
"notBefore": 2021-11-29T20:07:45.000Z,
241312
"recoveryLevel": "Recoverable+Purgeable",
242313
"name": "YOUR-CERTIFICATE-NAME",
243-
"vaultUrl": "https://YOUR-KEY-VAULT-NAME.vault.azure.net",
314+
"vaultUrl": "https://YOUR-KEY-VAULT-NAME-ENDPOINT",
244315
"version": "YOUR-CERTIFICATE-VERSION",
245316
"tags": undefined,
246317
"x509Thumbprint": undefined,
@@ -249,6 +320,8 @@ This code uses the following [Key Vault Certificate classes and methods](/javasc
249320
}
250321
```
251322

323+
::: zone-end
324+
252325

253326
## Integrating with App Configuration
254327

articles/key-vault/certificates/toc.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ items:
1919
href: quick-create-portal.md
2020
- name: .NET
2121
href: quick-create-net.md
22-
- name: Node.js
23-
href: quick-create-node.md
24-
displayName: az ad sp create-for-rbac, az keyvault set-policy
22+
- name: JavaScript
23+
href: quick-create-node.md?tabs=azure-cli%2Cwindows&pivots=programming-language-javascript
24+
displayName: az ad sp create-for-rbac, az keyvault set-policy
2525
- name: Python
2626
href: quick-create-python.md
2727
- name: Go
2828
href: quick-create-go.md
2929
- name: Java
3030
href: quick-create-java.md
31+
- name: TypeScript
32+
href: quick-create-node.md?tabs=azure-cli%2Cwindows&pivots=programming-language-typescript
33+
displayName: az ad sp create-for-rbac, az keyvault set-policy
3134
- name: Spring
3235
href: /azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-key-vault-certificates
3336
- name: Samples

0 commit comments

Comments
 (0)