Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

Azure Storage Accounts: List Accounts in a Subscription

See more Azure Storage Accounts Examples

Lists storage accounts in a subscription using the Storage Resource Provider API, version 2026-09-01. Displays each account's name, string resource ID, location, SKU, provisioning state, and available blob endpoint. No account keys are returned.

Replace the all-zero subscription ID. The identity needs Microsoft.Storage/storageAccounts/read across the subscription (for example, the Reader role there). The code follows each opaque nextLink, including after an empty page, and stops on malformed responses, repeated links, or a page limit. Partial output is not reported as a complete inventory. Continuation URLs are checked before sending the bearer token; redirects are disabled.

These calls manage Azure resources at management.azure.com. They do not upload or download stored data. Supply a current Microsoft Entra access token for Azure Resource Manager in private qa_data/tokens/azureToken.json, with a nonempty access_token string. For v2 client credentials, request https://management.azure.com/.default; obtain a new token when it expires. A storage account key, SAS, Microsoft Graph token, or Storage data-plane token is not an ARM token. Never print the token or commit its file.

Azure RBAC must grant the caller the required management actions at the appropriate scope. An app registration alone does not grant resource access. Management permissions and blob/file data permissions are distinct. These examples target public Azure; sovereign clouds require the corresponding authority, ARM endpoint, token audience, and continuation-host validation.

Microsoft API reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local stringCompare = chilkat.StringBuilder.new()
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global.UnlockBundle for sample code.

-- This is Azure Resource Manager (the management plane), not a blob data request.
-- Load an unexpired Microsoft Entra token whose audience is Azure Resource Manager.
-- For the v2 token endpoint, request scope https://management.azure.com/.default.
-- Keep the token file private and out of source control and public web directories.
local token = chilkat.JsonObject.new()
token.strict = true
local success = token:load_file("qa_data/tokens/azureToken.json")
if not success then
    print("Could not load the private Azure Resource Manager token file.")
    return
end

if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
    print("A nonempty access_token string is required.")
    return
end

-- Use your subscription ID as a string. The all-zero value is only a placeholder.
local subscriptionId = "00000000-0000-0000-0000-000000000000"
local http = chilkat.Http.new()
http.auth_token = token:string_of("access_token")
http.accept = "application/json"
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 60
success = http:set_url_var("subscriptionId", subscriptionId)
if not success then
    print("Could not encode subscription ID.")
    return
end

local response = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local parsed = chilkat.Url.new()

-- Http substitutes and URL-encodes {$subscriptionId} in this first request.
local requestUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/providers/Microsoft.Storage/storageAccounts?api-version=2026-09-01"
local seen = chilkat.Hashtable.new()
local accounts = chilkat.JsonArray.new()
local account = chilkat.JsonObject.new()
local morePages = true
local page = 0
local total = 0
while morePages do
    -- A limit and a repeated-link check protect against an endless continuation loop.
    if page >= 1000 then
        print("Page limit reached. Listing is incomplete; resume from: " .. requestUrl)
        return
    end

    if seen:contains(requestUrl) then
        print("Repeated continuation URL; listing is incomplete.")
        return
    end

    success = seen:add_str(requestUrl, "seen")
    if not success then
        print("Could not record the continuation URL.")
        return
    end

    success = http:http_no_body("GET", requestUrl, response)
    if response.status_code == 0 then
        print("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
        return
    end

    if response.status_code ~= 200 then
        print("HTTP status: " .. response.status_code)
        print("Request ID: " .. response:get_header_field("x-ms-request-id"))
        print("Retry-After: " .. response:get_header_field("Retry-After"))
        -- Inspect the error body privately. Do not log tokens, headers, or full responses.
        -- For 401 check the token/audience; for 403 check Azure RBAC and policy.
        -- Honor Retry-After for 429 and use bounded backoff for transient failures.
        return
    end

    if not success then
        print("The HTTP response could not be read completely.")
        return
    end

    success = result:load(response.body_str)
    if not success then
        print("Expected a well-formed JSON response.")
        return
    end

    if result:json_type_of("value") ~= 4 then
        print("Expected a value array of storage accounts.")
        return
    end

    success = result:array_of2("value", accounts)
    if not success then
        print("Could not read the storage account array.")
        return
    end

    local i = 0
    while i < accounts.size do
        success = accounts:object_at2(i, account)
        if not success then
            print("Invalid storage account entry.")
            return
        end

        if (account:json_type_of("id") ~= 1) or (account:json_type_of("name") ~= 1) then
            print("Expected a storage account ID and name.")
            return
        end

        -- ARM resource IDs are strings, not numbers. Save id for later management calls.
        print("Name: " .. account:string_of("name"))
        print("Resource ID: " .. account:string_of("id"))
        print("Location: " .. account:string_of("location"))
        print("Kind: " .. account:string_of("kind"))
        print("SKU: " .. account:string_of("sku.name"))
        print("Provisioning state: " .. account:string_of("properties.provisioningState"))
        -- Endpoint availability depends on account kind and provisioning state.
        if account:json_type_of("properties.primaryEndpoints.blob") == 1 then
            print("Blob endpoint: " .. account:string_of("properties.primaryEndpoints.blob"))
        end

        total = total + 1
        i = i + 1
    end

    page = page + 1
    morePages = false
    if result:has_member("nextLink") then
        if (not result:is_null_of("nextLink")) and (result:json_type_of("nextLink") ~= 1) then
            print("Invalid nextLink; listing is incomplete.")
            return
        end

        if (result:json_type_of("nextLink") == 1) and (not result:string_of_equals("nextLink", "", true)) then
            requestUrl = result:string_of("nextLink")
            success = parsed:parse_url(requestUrl)
            if not success then
                print("Missing or invalid continuation URL.")
                return
            end

            stringCompare:set_string(parsed.scheme)
            local stringMatch1 = stringCompare:contents_equal("https", true)
            stringCompare:set_string(parsed.host)
            local stringMatch2 = stringCompare:contents_equal("management.azure.com", true)
            stringCompare:set_string(parsed.login)
            local stringMatch3 = stringCompare:contents_equal("", true)
            stringCompare:set_string(parsed.password)
            local stringMatch4 = stringCompare:contents_equal("", true)
            stringCompare:set_string(parsed.frag)
            local stringMatch5 = stringCompare:contents_equal("", true)
            if (not stringMatch1) or ((not stringMatch2) or ((parsed.port ~= 443) or ((not stringMatch3) or ((not stringMatch4) or (not stringMatch5))))) then
                print("Unexpected continuation URL; refusing to send the access token.")
                return
            end

            -- The server owns nextLink. Keep its query intact; do not add api-version.
            -- Continue even if the current page contained no accounts.
            morePages = true
        end
    end
end

print("Listing complete. Storage accounts: " .. total)