Azure SDK for JavaScript - Core Package

The Azure SDK for JavaScript's core package provides fundamental utilities and abstractions essential for interacting with Azure services. This package is the foundation upon which all other Azure SDK libraries for JavaScript are built. It handles authentication, credential management, HTTP pipeline configuration, and common error handling.

Key Features

Authentication & Credentials

Seamlessly manage authentication to Azure resources. Supports various credential types like DefaultAzureCredential, EnvironmentCredential, ManagedIdentityCredential, and more, making it easy to secure your applications.

HTTP Pipeline

A robust and extensible HTTP pipeline that allows for custom policies. This enables advanced features like retry logic, logging, request/response manipulation, and telemetry.

Common Utilities

Provides essential utilities for working with Azure, including date/time handling, string formatting, and other helper functions that simplify common development tasks.

Error Handling

Standardized error types and handling mechanisms to provide consistent and informative error messages across all Azure SDKs, simplifying debugging and error recovery.

Authentication and Credentials

The core package simplifies credential management. DefaultAzureCredential is the recommended credential type for most applications, as it attempts authentication from a range of common Azure identity options without requiring code changes.


import { DefaultAzureCredential } from "@azure/identity";

async function getToken() {
    const credential = new DefaultAzureCredential();
    const token = await credential.getToken("https://management.azure.com/.default");
    console.log("Access Token:", token.token);
}

getToken();
            
HTTP Pipeline

The pipeline is the mechanism through which all HTTP requests are made. It's composed of a series of policies that process requests and responses. You can add custom policies to intercept and modify requests or responses.


import { createPipelineClient } from "@azure/core-http";
import { RetryPolicy } from "@azure/core-http";

// Default pipeline includes retry, logging, etc.
const pipeline = createPipelineClient({
    // You can add custom policies here
    policies: [
        new RetryPolicy({ retryCount: 4 }),
        // ... other policies
    ]
});

// Use the pipeline to make requests...
            
Common Utilities

The core package includes a set of utilities to aid in development:

Error Handling

Azure SDKs standardize error reporting. Common error types include RestError, which provides detailed information about HTTP request failures, and specific errors for authentication failures.


import { RestError } from "@azure/core-http";

async function callAzureService() {
    try {
        // ... attempt to call an Azure service
    } catch (error) {
        if (error instanceof RestError) {
            console.error("Azure Rest Error:", error.message);
            console.error("Status Code:", error.statusCode);
            console.error("Request:", error.request);
            console.error("Response:", error.response);
        } else {
            console.error("An unexpected error occurred:", error);
        }
    }
}
            
Getting Started

To use the core package, install it via npm or yarn:


npm install @azure/core-client @azure/identity
# or
yarn add @azure/core-client @azure/identity
            

The @azure/core-client package is the primary entry point for the modern Azure SDK v2.x, while @azure/identity provides credential management. These are often used together.