This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
To programmatically access your Azure services, use the Azure client libraries for JavaScript. Typically, these libraries are scoped with the@azure npm package scope published bymicrosoft1es.
Use the following information to understand when to use which type of access.
The Azure client libraryreleases are available as:
arm- in their package names. The termarm indicates the Azure Resource Manager.Azure client libraries are freely available fromNPM and Yarn. Install individual SDKs as needed. Each SDK provides TypeScript definitions.
For client/browser usage, Azure client libraries need to be added to yourbundling process.
Each package includes documentation to quickly get you started with the package. Refer to the specific package's documentation you use to learn how to use them.
The Azure client libraries require credentialsto authenticate to the Azure platform.Credential classes provided by@azure/identity provide several benefits:
Once you programmatically create a credential, pass the credential to your Azure client. The client may require additional information such as a subscription ID or service endpoint. These values are available in the Azure portal, for your resource.
The following code example uses the DefaultAzureCredential and thearm subscription client library to list subscriptions which this credential has access to read.
const { ClientSecretCredential, DefaultAzureCredential,} = require("@azure/identity");const { SubscriptionClient } = require("@azure/arm-subscriptions");require("dotenv").config();let credentials = null;const tenantId = process.env["AZURE_TENANT_ID"];const clientId = process.env["AZURE_CLIENT_ID"];const secret = process.env["AZURE_CLIENT_SECRET"];if (process.env.NODE_ENV && process.env.NODE_ENV === "production") { // production credentials = new DefaultAzureCredential();} else { // development if (tenantId && clientId && secret) { console.log("development"); credentials = new ClientSecretCredential(tenantId, clientId, secret); } else { credentials = new DefaultAzureCredential(); }}async function listSubscriptions() { try { // use credential to authenticate with Azure SDKs const client = new SubscriptionClient(credentials); // get details of each subscription for await (const item of client.subscriptions.list()) { const subscriptionDetails = await client.subscriptions.get( item.subscriptionId ); /* Each item looks like: { id: '/subscriptions/123456', subscriptionId: '123456', displayName: 'YOUR-SUBSCRIPTION-NAME', state: 'Enabled', subscriptionPolicies: { locationPlacementId: 'Internal_2014-09-01', quotaId: 'Internal_2014-09-01', spendingLimit: 'Off' }, authorizationSource: 'RoleBased' }, */ console.log(subscriptionDetails); } } catch (err) { console.error(JSON.stringify(err)); }}listSubscriptions() .then(() => { console.log("done"); }) .catch((ex) => { console.log(ex); });An SDK method can return an asynchronous iterator,PagedAsyncIterableIterator, to allow for asynchronous results. The results may use paging and continuation tokens to break up result sets.
The followingJavaScript example demonstrates asynchronous paging. The code sets an artificially short paging size of 2 in order to quickly and visually demonstrate the process when you run the sample code in debug.
const { BlobServiceClient } = require("@azure/storage-blob");const blobAccountConnectionString = "REPLACE-WITH-YOUR-STORAGE-CONNECTION-STRING";const blobAccountContainerName = "REPLACE-WITH-YOUR-STORAGE-CONTAINER-NAME";const pageSize = 2;const list = async () => { console.log(`List`); let continuationToken = ""; let currentPage = 1; let containerClient=null; let currentItem = 1; // Get Blob Container - need to have items in container before running this code const blobServiceClient = BlobServiceClient.fromConnectionString(blobAccountConnectionString); containerClient = blobServiceClient.getContainerClient(blobAccountContainerName); do { // Get Page of Blobs iterator = (continuationToken != "") ? containerClient.listBlobsFlat().byPage({ maxPageSize: pageSize, continuationToken }) : containerClient.listBlobsFlat().byPage({ maxPageSize: pageSize }); page = (await iterator.next()).value; // Display list if (page.segment?.blobItems) { console.log(`\tPage [${currentPage}] `); for (const blob of page.segment.blobItems) { console.log(`\t\tItem [${currentItem++}] ${blob.name}`); } }; // Move to next page continuationToken = page.continuationToken; if (continuationToken) { currentPage++; } } while (continuationToken != "")}list(() => { console.log("done");}).catch((ex) => console.log(ex));Learn more about paging and iterators on Azure:
An SDK method can return a long running operation (LRO)raw response. This response includes information including:
The followingJavaScript example demonstrates how to wait for an LRO to complete, with.pollUntildone(), before continuing.
const { BlobServiceClient } = require("@azure/storage-blob");const blobAccountConnectionString = "REPLACE-WITH-YOUR-STORAGE-CONNECTION-STRING";const blobAccountContainerName = `test-${Date.now().toString()}`;const files = [ { "url": "https://github.com/Azure/azure-sdk-for-js/blob/main/README.md", "fileName": "README.md" }, { "url": "https://github.com/Azure/azure-sdk-for-js/blob/main/gulpfile.ts", "fileName": "gulpfile.ts" }, { "url": "https://github.com/Azure/azure-sdk-for-js/blob/main/rush.json", "fileName": "rush.json" }, { "url": "https://github.com/Azure/azure-sdk-for-js/blob/main/package.json", "fileName": "package.json" }, { "url": "https://github.com/Azure/azure-sdk-for-js/blob/main/tsdoc.json", "fileName": "tsdoc.json" },];const upload = async() => { // get container client const blobServiceClient = BlobServiceClient.fromConnectionString(blobAccountConnectionString); // get container's directory client const containerClient = blobServiceClient.getContainerClient(blobAccountContainerName); files.forEach(async(file) =>{ await ( await containerClient .getBlobClient(file.fileName) .beginCopyFromURL(file.url) ).pollUntilDone(); })}upload(() => { console.log("done");}).catch((ex) => console.log(ex));Learn more about long running operations on Azure:
The@azure/abort-controller package provides AbortController and AbortSignal classes. Use the AbortController to create an AbortSignal, which can then be passed to Azure SDK operations to cancel pending work. Azure SDK operations can be:
Learn more:
When you're using the Azure SDK, there may be times when you need to debug your application.
To enable logging atbuild-time, set the AZURE_LOG_LEVEL environment variable toinfo.
To enable logging atrun-time, use the@azure/logger package:
import { setLogLevel } from "@azure/logger";setLogLevel("info");Learn about bundling with the Azure SDK:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?