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

Azure Storage Accounts: Check Storage Account Name Availability

See more Azure Storage Accounts Examples

Checks a proposed globally unique account name before creation. Account names contain 3-24 lowercase letters or digits. HTTP 200 can mean either available or unavailable: inspect nameAvailable and, when false, reason and message. The check does not reserve a name or validate region/SKU availability.

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")

-- 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()
local accountName = "youruniquestorageaccount"
local request = chilkat.JsonObject.new()
request:update_string("name", accountName)
request:update_string("type", "Microsoft.Storage/storageAccounts")
success = http:http_json("POST", "https://management.azure.com/subscriptions/{$subscriptionId}/providers/Microsoft.Storage/checkNameAvailability?api-version=2026-09-01", request, "application/json", 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("nameAvailable") ~= 5 then
    print("Missing or invalid nameAvailable.")
    return
end

if result:bool_of("nameAvailable") then
    print("Name is currently available: " .. accountName)
else
    -- Invalid and AlreadyExists are application outcomes, not HTTP failures.
    print("Name is unavailable. Reason: " .. result:string_of("reason"))
    print("Details: " .. result:string_of("message"))
end

-- Availability is not a reservation. Creation can still race another caller.