Getting Started with the Azure SDK for JavaScript

Welcome to the Azure SDK for JavaScript! This guide will help you set up your environment and start building applications that interact with Azure services using JavaScript.

Prerequisites

Before you begin, ensure you have the following installed:

Installation

You can install the Azure SDK for JavaScript packages individually or as part of a larger project. We'll use npm for installation.

1

Create a Project Directory

Create a new directory for your project and navigate into it using your terminal:

mkdir my-azure-app
cd my-azure-app
2

Initialize Your Project

Initialize your Node.js project with npm:

npm init -y

This command creates a package.json file to manage your project's dependencies and metadata.

3

Install an Azure SDK Package

Let's install the package for interacting with Azure Blob Storage as an example. You can replace this with any Azure service SDK you need.

Install Azure Blob Storage SDK:
npm install @azure/storage-blob

To install other SDKs, search for the relevant package on npmjs.com (e.g., @azure/cosmos for Azure Cosmos DB, @azure/keyvault-secrets for Azure Key Vault).

Authentication

Azure SDKs use various methods for authentication. The most common and recommended approach for server-side applications is using connection strings or service principals via environment variables or credential objects.

For Blob Storage, you can authenticate using a connection string:

  1. Navigate to your Storage Account in the Azure portal.
  2. Go to "Access keys" and copy one of the connection strings.
  3. Set it as an environment variable (e.g., AZURE_STORAGE_CONNECTION_STRING) or use it directly in your code (less secure for production).

Refer to the specific SDK documentation for detailed authentication options.

Your First Interaction (Example)

Here's a simple example using the Blob Storage SDK to list blobs in a container. Create a file named index.js and add the following code:


const { BlobServiceClient } = require("@azure/storage-blob");
const dotenv = require("dotenv"); // Recommended for managing environment variables

// Load environment variables from .env file
dotenv.config();

async function listBlobs(containerName) {
    const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
    if (!connectionString) {
        console.error("AZURE_STORAGE_CONNECTION_STRING environment variable not set.");
        process.exit(1);
    }

    const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
    const containerClient = blobServiceClient.getContainerClient(containerName);

    console.log(`Listing blobs in container: ${containerName}`);

    try {
        for await (const blob of containerClient.listBlobsFlat()) {
            console.log(`- ${blob.name}`);
        }
        console.log("Successfully listed blobs.");
    } catch (error) {
        console.error("Error listing blobs:", error);
    }
}

// Replace 'your-container-name' with the actual name of your blob container
listBlobs("your-container-name");
            

You'll need to install the dotenv package:

npm install dotenv

Create a .env file in your project root and add your connection string:

AZURE_STORAGE_CONNECTION_STRING="YOUR_CONNECTION_STRING_HERE"

Run your script:

node index.js

Next Steps

Explore the API Reference

View Code Samples

Visit Azure Developer Center