# {{ clientDescriptiveName }} library for JavaScript

This package contains an isomorphic SDK (runs both in Node.js and in browsers) for {{ clientDescriptiveName }}.

{{ description }}

{{#if packageSourceURL}}
[Source code]({{ packageSourceURL }}) |
{{/if}}
{{#if packageNPMURL}}
[Package (NPM)]({{ packageNPMURL }}) |
{{/if}}
{{#if apiRefURL}}
[API reference documentation]({{ apiRefURL }}) |
{{/if}}
{{#if samplesURL}}
[Samples]({{samplesURL}})
{{/if}}

## Getting started

### Currently supported environments

- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
- Latest versions of Safari, Chrome, Edge and Firefox.

See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details.

{{#if azure}}
### Prerequisites

- An [Azure subscription][azure_sub].
{{/if}}

{{#if isReleasablePackage}}
### Install the `{{ clientPackageName }}` package

Install the {{ clientDescriptiveName }} library for JavaScript with `npm`:

```bash
npm install {{ clientPackageName }}
```
{{/if}}

{{#if azure}}
{{#if addCredentials}}
### Create and authenticate a `{{ clientClassName}}`

To create a client object to access the {{ serviceName }} API, you will need the `endpoint` of your {{ serviceName }} resource and a `credential`. The {{ clientDescriptiveName }} can use Azure Active Directory credentials to authenticate.
You can find the endpoint for your {{ serviceName }} resource in the [Azure Portal][azure_portal].

You can authenticate with Azure Active Directory using a credential from the [@azure/identity][azure_identity] library or [an existing AAD Token](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token).

To use the [DefaultAzureCredential][defaultazurecredential] provider shown below, or other credential providers provided with the Azure SDK, please install the `@azure/identity` package:

```bash
npm install @azure/identity
```

You will also need to **register a new AAD application and grant access to {{ serviceName}}** by assigning the suitable role to your service principal (note: roles such as `"Owner"` will not grant the necessary permissions).

For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).

{{#if azureArm}}
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Node{{/if}}
import { {{ clientClassName }} } from "{{ clientPackageName }}";
import { DefaultAzureCredential } from "@azure/identity";

{{#if hasClientSubscriptionId}}
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new {{ clientClassName }}(new DefaultAzureCredential(), subscriptionId);
```
{{else}}
const client = new {{ clientClassName }}(new DefaultAzureCredential());
```
{{/if}}

For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Browser{{/if}}
import { InteractiveBrowserCredential } from "@azure/identity";
import { {{ clientClassName }} } from "{{ clientPackageName }}";

const credential = new InteractiveBrowserCredential({
  tenantId: "<YOUR_TENANT_ID>",
  clientId: "<YOUR_CLIENT_ID>",
 });
{{#if hasClientSubscriptionId}}
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new {{ clientClassName }}(credential, subscriptionId);
```
{{else}}
const client = new {{ clientClassName }}(credential);
```
{{/if}}
{{else}}
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Node{{/if}}
import { InteractiveBrowserCredential } from "@azure/identity";
import { {{ clientClassName }} } from "{{ clientPackageName }}";

const client = new {{ clientClassName }}("<endpoint>", new DefaultAzureCredential());
```

For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Browser{{/if}}
import { InteractiveBrowserCredential } from "@azure/identity";
import { {{ clientClassName }} } from "{{ clientPackageName }}";

const credential = new InteractiveBrowserCredential({
  tenantId: "<YOUR_TENANT_ID>",
  clientId: "<YOUR_CLIENT_ID>"
 });
const client = new {{ clientClassName }}("<endpoint>", credential);
```
{{/if}}
{{/if}}{{/if}}
### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

## Key concepts

### {{ clientClassName }}

`{{ clientClassName }}` is the primary interface for developers using the {{ clientDescriptiveName }} library. Explore the methods on this client object to understand the different features of the {{ serviceName }} service that you can access.

{{#if azure}}
## Troubleshooting

### Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:

```ts {{#if azureSdkForJs}}snippet:SetLogLevel{{/if}}
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
```

For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs]({{ repoURL }}/tree/main/sdk/core/logger).

{{#if samplesURL}}
## Next steps

Please take a look at the [samples]({{ samplesURL }}) directory for detailed examples on how to use this library.
{{/if}}

## Contributing

If you'd like to contribute to this library, please read the [contributing guide]({{ contributingGuideURL }}) to learn more about how to build and test the code.

## Related projects

- [{{ projectName }}]({{ repoURL }})


[azure_cli]: https://learn.microsoft.com/cli/azure
[azure_sub]: https://azure.microsoft.com/free/
[azure_sub]: https://azure.microsoft.com/free/
[azure_portal]: https://portal.azure.com
{{#if identityPackageURL}}[azure_identity]: {{ identityPackageURL }}
{{/if}}[defaultazurecredential]: {{ identityPackageURL }}#defaultazurecredential
{{/if}}