title | description | ms.topic | ms.date | ms.custom |
---|---|---|---|---|
Azure SDK for JS/TS |
To programmatically access your Azure services, use the Azure SDKs for JavaScript or TypeScript development. |
how-to |
02/03/2022 |
devx-track-js |
To programmatically access your Azure services, use the Azure SDKs for JavaScript. Typically, these SDKs are scoped with the @azure npm package scope published by azure-sdk.
Use the following table to understand when to use which type of access mechanism.
Azure SDKs (recommended) | Azure REST APIs |
---|---|
The Azure SDKs are the preferred method of accessing your Azure service. The Azure SDKs abstract away the boilerplate code required to manage cloud-based Azure platform REST requests such as authentication, retries, and logging. | Azure REST APIs are the preferred method if you are:
|
The Azure SDK releases are available as:
- Management SDKs: Management libraries enable you to provision and manage Azure resources via the Azure Resource Manager (ARM). You can recognize these libraries by
@azure/arm-
in their package names. - Client SDKs: Given an Azure resource already exists, you would use the client libraries to consume it and interact with it.
- Each package README.md includes documentation and samples.
Azure SDKs are freely available from NPM. Install individual SDKs needed. Each SDK provides TypeScript definitions.
For client/browser usage, Azure SDKs need to be added to your bundling process.
Each package includes documentation to quickly get you started with the package. Refer to the specific NPM packages you use to learn how to use them.
The Azure SDKs require credentials to authenticate to the Azure platform. Credential classes provided by @azure/identity provide several benefits:
- Fast onboarding
- Most secure method
- Separate the authentication mechanism from the code. This allows you to use the same code locally and on the Azure platform while the credentials are different.
- Provide chained authentication so several mechanisms can be available
Once you programmatically create a credential, pass the credential to your Azure SDK's client. The client may require additional information such as a subscription ID or service URL. These values are available in the Azure portal, for your resource.
List subscriptions which this credential has access to read.
:::code language="JavaScript" source="~/../js-e2e/resources/subscriptions/list.js" highlight="28,33" :::
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 following JavaScript 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.
:::code language="JavaScript" source="~/../js-e2e/storage/blob-paging/blob-paging.js" highlight="21-41":::
Learn more about paging and iterators on Azure:
An SDK method can return a long running operation (LRO) response. This response includes information including:
- Your request completed
- Your request is still in process
The following JavaScript example demonstrates how to wait for an LRO to complete, with .pollUntildone()
, before continuing.
:::code language="JavaScript" source="~/../js-e2e/storage/upload-url-to-blob-poll-until-done/upload-url-to-blob-poll-until-done.js" highlight="38-44":::
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:
- Aborted based on your own logic
- Aborted based on a timeout limit
- Aborted based on a parent task's signal
- Aborted based on a parent task's signal or a timeout limit
Learn more:
When using an Azure SDK, there may be times when you need to debug your application.
-
To enable logging at build-time, set the AZURE_LOG_LEVEL environment variable to
info
. -
To enable logging at run-time, use the @azure/logger package:
import { setLogLevel } from "@azure/logger"; setLogLevel("info");
Learn about bundling with the Azure SDK: