Azure Key Vault: List Deleted Objects and Recover One
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.
Choose object_kind and object_name. Paginates deleted objects, then inspects that object. Set allow_recover=true to recover it; otherwise the example only reads. Requires the corresponding list/get permissions and recover when enabled. A not-yet-visible deleted object can return 404; retry the read later without repeating delete. Purged objects cannot be recovered, and restored availability can be delayed.
Chilkat B4X Downloads
Dim success As Boolean = False
Dim stringCompare As ChilkatStringBuilder
stringCompare.Initialize
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
Dim config As ChilkatJsonObject
config.Initialize
config.Strict = True
success = config.LoadFile("qa_data/azure/keyvault.json")
If success <> True Then
Log("Could not load private Key Vault configuration.")
Return
End If
stringCompare.SetString(config.StringOf("vault_name"))
Dim stringMatch1 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = True) Then
Log("Missing or invalid vault_name.")
Return
End If
Dim validate1 As ChilkatStringBuilder
validate1.Initialize
Dim alphabet1 As ChilkatStringBuilder
alphabet1.Initialize
success = validate1.Append(config.StringOf("vault_name"))
If success <> True Then
Log("Could not validate vault_name.")
Return
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If success <> True Then
Log("Could not initialize validation.")
Return
End If
If (validate1.Length < 3) Or (validate1.Length > 24) Then
Log("Invalid vault_name length.")
Return
End If
Dim charIndex1 As Int = 0
Do While (charIndex1 < validate1.Length)
If alphabet1.Contains(validate1.GetRange(charIndex1, 1, False), True) <> True Then
Log("Invalid character in vault_name.")
Return
End If
charIndex1 = charIndex1 + 1
Loop
Dim vaultName As ChilkatStringBuilder
vaultName.Initialize
vaultName.Append(config.StringOf("vault_name"))
Dim letters As ChilkatStringBuilder
letters.Initialize
letters.Append("abcdefghijklmnopqrstuvwxyz")
If (letters.Contains(vaultName.GetRange(0, 1, False), True) <> True) Or (vaultName.EndsWith("-", True) = True) Or (vaultName.Contains("--", True) = True) Then
Log("Invalid vault_name.")
Return
End If
Dim origin As ChilkatStringBuilder
origin.Initialize
success = origin.Append("https://")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = origin.Append(config.StringOf("vault_name"))
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = origin.Append(".vault.azure.net")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = False
Dim request As ChilkatHttpRequest
request.Initialize
request.HttpVerb = "GET"
Dim response As ChilkatHttpResponse
response.Initialize
Dim result As ChilkatJsonObject
result.Initialize
result.Strict = True
' First run Get a Key Vault access token.
Dim token As ChilkatJsonObject
token.Initialize
token.Strict = True
success = token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
If success <> True Then
Log("Could not read the private token file.")
Return
End If
stringCompare.SetString(token.StringOf("access_token"))
Dim stringMatch2 As Boolean = stringCompare.ContentsEqual("", True)
If (token.JsonTypeOf("access_token") <> 1) Or (stringMatch2 = True) Then
Log("Missing or invalid access_token.")
Return
End If
stringCompare.SetString(token.StringOf("expires_at"))
Dim stringMatch3 As Boolean = stringCompare.ContentsEqual("", True)
If (token.JsonTypeOf("expires_at") <> 1) Or (stringMatch3 = True) Then
Log("Missing or invalid expires_at.")
Return
End If
Dim expires As ChilkatDateTime
expires.Initialize
Dim now As ChilkatDateTime
now.Initialize
success = expires.SetFromTimestamp(token.StringOf("expires_at"))
If success <> True Then
Log("Invalid token expiration.")
Return
End If
http.AuthToken = token.StringOf("access_token")
stringCompare.SetString(config.StringOf("object_kind"))
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("object_kind") <> 1) Or (stringMatch4 = True) Then
Log("Missing or invalid object_kind.")
Return
End If
stringCompare.SetString(config.StringOf("object_kind"))
Dim stringMatch5 As Boolean = stringCompare.ContentsEqual("keys", True)
stringCompare.SetString(config.StringOf("object_kind"))
Dim stringMatch6 As Boolean = stringCompare.ContentsEqual("secrets", True)
stringCompare.SetString(config.StringOf("object_kind"))
Dim stringMatch7 As Boolean = stringCompare.ContentsEqual("certificates", True)
If (stringMatch5 <> True) And (stringMatch6 <> True) And (stringMatch7 <> True) Then
Log("object_kind must be keys, secrets, or certificates.")
Return
End If
Dim collection As ChilkatStringBuilder
collection.Initialize
success = collection.Append(origin.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = collection.Append("/deleted")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = collection.Append(config.StringOf("object_kind"))
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim pagePrefix As ChilkatStringBuilder
pagePrefix.Initialize
success = pagePrefix.Append(collection.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = pagePrefix.Append("?")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim firstPage As ChilkatStringBuilder
firstPage.Initialize
success = firstPage.Append(pagePrefix.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = firstPage.Append("api-version=2026-04-01&maxresults=25")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim itemsSeen As ChilkatHashtable
itemsSeen.Initialize
Dim itemsItems As ChilkatJsonArray
itemsItems.Initialize
Dim itemsView As ChilkatJsonArray
itemsView.Initialize
Dim itemsEntry As ChilkatJsonObject
itemsEntry.Initialize
Dim itemsCurrentUrl As String = firstPage.GetAsString
Dim itemsPages As Int = 0
Dim itemsIndex As Int
Dim itemsMore As Boolean = True
Do While (itemsMore = True)
If itemsPages = 10000 Then
Log("Page bound reached; enumeration is incomplete.")
Return
End If
If itemsSeen.Contains(itemsCurrentUrl) = True Then
Log("Repeated nextLink; enumeration is incomplete.")
Return
End If
success = itemsSeen.AddStr(itemsCurrentUrl, "seen")
If success <> True Then
Log("Could not track pagination.")
Return
End If
itemsPages = itemsPages + 1
success = now.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read current time.")
Return
End If
If expires.DiffSeconds(now) <= 60 Then
Log("Token is expired or near expiration. Obtain a new token before continuing.")
Return
End If
success = http.HttpReq(itemsCurrentUrl, request, response)
If success <> True Then
Log("Request did not complete; inspect diagnostics privately.")
Return
End If
If response.StatusCode <> 200 Then
Log("HTTP status: " & response.StatusCode)
Log("Request ID: " & response.GetHeaderField("x-ms-request-id"))
' Do not print full response bodies, tokens, or diagnostic logs.
Log("Request failed. Inspect private diagnostics; reconcile writes before retrying.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Expected a well-formed JSON response.")
Return
End If
If result.JsonTypeOf("value") <> 4 Then
Log("Expected a value array.")
Return
End If
success = result.ArrayOf2("value", itemsView)
If success <> True Then
Log("Could not read certificate array.")
Return
End If
success = itemsItems.Load(itemsView.Emit)
If success <> True Then
Log("Could not detach the page before nested requests.")
Return
End If
itemsMore = False
If result.HasMember("nextLink") = True Then
If result.JsonTypeOf("nextLink") <> 6 Then
stringCompare.SetString(result.StringOf("nextLink"))
Dim stringMatch8 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("nextLink") <> 1) Or (stringMatch8 = True) Then
Log("Missing or invalid nextLink.")
Return
End If
itemsCurrentUrl = result.StringOf("nextLink")
Dim itemsNext As ChilkatStringBuilder
itemsNext.Initialize
success = itemsNext.Append(itemsCurrentUrl)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
If (itemsNext.StartsWith(pagePrefix.GetAsString, True) <> True) Or (itemsNext.Contains("#", True) = True) Or (itemsNext.Contains("\", True) = True) Or (itemsNext.Contains(Chr(13), True) = True) Or (itemsNext.Contains(Chr(10), True) = True) Then
Log("Unsafe pagination URL; stopped before sending a token.")
Return
End If
itemsMore = True
End If
End If
itemsIndex = 0
Do While (itemsIndex < itemsItems.Size)
success = itemsItems.ObjectAt2(itemsIndex, itemsEntry)
If success <> True Then
Log("Invalid item.")
Return
End If
stringCompare.SetString(itemsEntry.StringOf("recoveryId"))
Dim stringMatch9 As Boolean = stringCompare.ContentsEqual("", True)
If (itemsEntry.JsonTypeOf("recoveryId") <> 1) Or (stringMatch9 = True) Then
Log("Missing or invalid recoveryId.")
Return
End If
Log("Resource ID: " & itemsEntry.StringOf("recoveryId"))
itemsIndex = itemsIndex + 1
Loop
Loop
stringCompare.SetString(config.StringOf("object_name"))
Dim stringMatch10 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("object_name") <> 1) Or (stringMatch10 = True) Then
Log("Missing or invalid object_name.")
Return
End If
Dim validate52 As ChilkatStringBuilder
validate52.Initialize
Dim alphabet52 As ChilkatStringBuilder
alphabet52.Initialize
success = validate52.Append(config.StringOf("object_name"))
If success <> True Then
Log("Could not validate object_name.")
Return
End If
success = alphabet52.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
Log("Could not initialize validation.")
Return
End If
If (validate52.Length < 1) Or (validate52.Length > 127) Then
Log("Invalid object_name length.")
Return
End If
Dim charIndex52 As Int = 0
Do While (charIndex52 < validate52.Length)
If alphabet52.Contains(validate52.GetRange(charIndex52, 1, False), True) <> True Then
Log("Invalid character in object_name.")
Return
End If
charIndex52 = charIndex52 + 1
Loop
Dim deletedBase As ChilkatStringBuilder
deletedBase.Initialize
success = deletedBase.Append(origin.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = deletedBase.Append("/deleted")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = deletedBase.Append(config.StringOf("object_kind"))
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = deletedBase.Append("/")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = deletedBase.Append(config.StringOf("object_name"))
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
Dim getDeletedUrl As ChilkatStringBuilder
getDeletedUrl.Initialize
success = getDeletedUrl.Append(deletedBase.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = getDeletedUrl.Append("?api-version=2026-04-01")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = now.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read current time.")
Return
End If
If expires.DiffSeconds(now) <= 60 Then
Log("Token is expired or near expiration. Obtain a new token before continuing.")
Return
End If
request.HttpVerb = "GET"
success = http.HttpReq(getDeletedUrl.GetAsString, request, response)
If success <> True Then
Log("Request did not complete; inspect diagnostics privately.")
Return
End If
If response.StatusCode <> 200 Then
Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Invalid JSON response.")
Return
End If
stringCompare.SetString(result.StringOf("recoveryId"))
Dim stringMatch11 As Boolean = stringCompare.ContentsEqual("", True)
If (result.JsonTypeOf("recoveryId") <> 1) Or (stringMatch11 = True) Then
Log("Missing or invalid recoveryId.")
Return
End If
stringCompare.SetString(result.StringOf("recoveryId"))
Dim stringMatch12 As Boolean = stringCompare.ContentsEqual(deletedBase.GetAsString, True)
If stringMatch12 <> True Then
Log("Unexpected recovery identifier.")
Return
End If
Log("Recovery ID: " & result.StringOf("recoveryId"))
If config.JsonTypeOf("allow_recover") = 5 Then
If config.BoolOf("allow_recover") = True Then
Dim recoverUrl As ChilkatStringBuilder
recoverUrl.Initialize
success = recoverUrl.Append(deletedBase.GetAsString)
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = recoverUrl.Append("/recover?api-version=2026-04-01")
If success <> True Then
Log("Could not construct URL or identifier.")
Return
End If
success = now.SetFromCurrentSystemTime
If success <> True Then
Log("Could not read current time.")
Return
End If
If expires.DiffSeconds(now) <= 60 Then
Log("Token is expired or near expiration. Obtain a new token before continuing.")
Return
End If
request.HttpVerb = "POST"
success = http.HttpReq(recoverUrl.GetAsString, request, response)
If success <> True Then
Log("Request did not complete; inspect diagnostics privately.")
Return
End If
If response.StatusCode <> 200 Then
Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Invalid JSON response.")
Return
End If
Log("Recovery accepted; verify availability before resuming consumers.")
End If
End If
Log("Example completed.")