This quickstart guide will walk you through the essential steps to authenticate your JavaScript applications with Azure using the Azure Identity client library.
Prerequisites
Before you begin, ensure you have the following:
- An Azure account. If you don't have one, sign up for a free trial.
- Node.js and npm installed on your machine. You can download it from nodejs.org.
- An Azure Active Directory (Azure AD) application registration.
Step 1: Set up your project
Create a new directory for your project and navigate into it:
mkdir azure-identity-js-quickstart
cd azure-identity-js-quickstart
Initialize a new Node.js project:
npm init -y
Install the Azure Identity client library for JavaScript:
npm install @azure/identity @azure/storage-blob
We're using `@azure/storage-blob` as an example to interact with Azure Blob Storage, which requires authentication.
Step 2: Authenticate with Azure
Create a file named index.js in your project directory and add the following code:
import { DefaultAzureCredential } from "@azure/identity";
import { BlobServiceClient } from "@azure/storage-blob";
async function main() {
// Define your Azure Blob Storage account name and container name
const accountName = "YOUR_STORAGE_ACCOUNT_NAME"; // Replace with your storage account name
const containerName = "YOUR_CONTAINER_NAME"; // Replace with your container name
// Create a credential object
// DefaultAzureCredential will try to authenticate using various methods,
// including environment variables, managed identity, Azure CLI, etc.
const credential = new DefaultAzureCredential();
// Create a BlobServiceClient
const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
credential
);
console.log("Azure Blob Storage client created.");
try {
// Example: List containers (requires appropriate permissions)
console.log("Listing containers...");
for await (const container of blobServiceClient.listContainers()) {
console.log(`- ${container.name}`);
}
// Example: Create a container
console.log(`Creating container: ${containerName}...`);
const containerClient = blobServiceClient.getContainerClient(containerName);
await containerClient.createIfNotExists();
console.log(`Container "${containerName}" created or already exists.`);
// Further operations like uploading/downloading blobs can be done here
// ...
} catch (error) {
console.error("Error during Azure Blob Storage operation:", error);
}
}
main().catch((err) => {
console.error("The following error occurred: ", err);
process.exit(1);
});
Remember to replace YOUR_STORAGE_ACCOUNT_NAME and YOUR_CONTAINER_NAME with your actual Azure Storage account name and desired container name.
Step 3: Running the Quickstart
To run this quickstart, you need to set up your environment for authentication. The DefaultAzureCredential supports several authentication methods. Here are a few common ones:
Option A: Environment Variables
Set the following environment variables:
AZURE_CLIENT_ID: Your Azure AD application's client ID.AZURE_TENANT_ID: Your Azure AD tenant ID.AZURE_CLIENT_SECRET: A secret generated for your Azure AD application.
You can set these variables in your terminal:
export AZURE_CLIENT_ID="YOUR_CLIENT_ID"
export AZURE_TENANT_ID="YOUR_TENANT_ID"
export AZURE_CLIENT_SECRET="YOUR_CLIENT_SECRET"
Option B: Azure CLI
If you have the Azure CLI installed and logged in (az login), DefaultAzureCredential can pick up your credentials automatically.
az login
Option C: Managed Identity (for Azure Services)
If your code is running on an Azure service (like Azure App Service, Azure Functions, or Azure Kubernetes Service) with a system-assigned or user-assigned managed identity enabled and configured, DefaultAzureCredential can leverage it without any explicit configuration.
Once your environment is set up, run the script:
node index.js
You should see output indicating the creation of the BlobServiceClient and a list of containers (or an error if permissions are insufficient).
Next Steps
Congratulations! You've successfully authenticated your JavaScript application with Azure using the Azure Identity library.
- Explore more authentication methods supported by
DefaultAzureCredential. - Learn how to interact with other Azure services like Azure Key Vault, Azure Cosmos DB, and Azure App Configuration using their respective SDKs and the same authentication credentials.
- Refer to the Azure Identity API Reference for detailed information on available classes and methods.