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

Azure Key Vault: Restore a Protected Object Backup

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 destination vault_name, matching object_kind/object_name, and allow_restore=true. Copy the protected backup to private qa_data/azure/keyvault-backup.json. Requires the corresponding restore permission. Restores the original name/versions; it does not rename or merge into an existing object. The service enforces backup origin, subscription/geography, conflicts and compatibility. Metadata checks catch accidental file mix-ups but do not authenticate caller-edited metadata.

Microsoft reference

Chilkat B4X Downloads

B4X
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

stringCompare.SetString(config.StringOf("object_name"))
Dim stringMatch8 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("object_name") <> 1) Or (stringMatch8 = True) Then
    Log("Missing or invalid object_name.")
    Return
End If

Dim validate51 As ChilkatStringBuilder
validate51.Initialize
Dim alphabet51 As ChilkatStringBuilder
alphabet51.Initialize
success = validate51.Append(config.StringOf("object_name"))
If success <> True Then
    Log("Could not validate object_name.")
    Return
End If

success = alphabet51.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate51.Length < 1) Or (validate51.Length > 127) Then
    Log("Invalid object_name length.")
    Return
End If

Dim charIndex51 As Int = 0
Do While (charIndex51 < validate51.Length)
    If alphabet51.Contains(validate51.GetRange(charIndex51, 1, False), True) <> True Then
        Log("Invalid character in object_name.")
        Return
    End If

    charIndex51 = charIndex51 + 1
Loop
Dim resourceBase As ChilkatStringBuilder
resourceBase.Initialize
success = resourceBase.Append(origin.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = resourceBase.Append("/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = resourceBase.Append(config.StringOf("object_kind"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = resourceBase.Append("/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = resourceBase.Append(config.StringOf("object_name"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

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

If (config.JsonTypeOf("allow_restore") <> 5) Or (config.BoolOf("allow_restore") <> True) Then
    Log("Set explicit allow_restore=true only after reviewing the target and consequences.")
    Return
End If

Dim backup As ChilkatJsonObject
backup.Initialize
backup.Strict = True
success = backup.LoadFile("qa_data/azure/keyvault-backup.json")
If success <> True Then
    Log("Could not load backup.")
    Return
End If

stringCompare.SetString(backup.StringOf("kind"))
Dim stringMatch9 As Boolean = stringCompare.ContentsEqual(config.StringOf("object_kind"), True)
stringCompare.SetString(backup.StringOf("name"))
Dim stringMatch10 As Boolean = stringCompare.ContentsEqual(config.StringOf("object_name"), True)
If (stringMatch9 <> True) Or (stringMatch10 <> True) Then
    Log("Backup metadata does not match the explicitly selected object.")
    Return
End If

stringCompare.SetString(backup.StringOf("value"))
Dim stringMatch11 As Boolean = stringCompare.ContentsEqual("", True)
If (backup.JsonTypeOf("value") <> 1) Or (stringMatch11 = True) Then
    Log("Missing or invalid value.")
    Return
End If

Dim validate54 As ChilkatStringBuilder
validate54.Initialize
Dim alphabet54 As ChilkatStringBuilder
alphabet54.Initialize
success = validate54.Append(backup.StringOf("value"))
If success <> True Then
    Log("Could not validate backup blob.")
    Return
End If

success = alphabet54.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate54.Length < 1) Or (validate54.Length > 10485760) Then
    Log("Invalid backup blob length.")
    Return
End If

Dim charIndex54 As Int = 0
Do While (charIndex54 < validate54.Length)
    If alphabet54.Contains(validate54.GetRange(charIndex54, 1, False), True) <> True Then
        Log("Invalid character in backup blob.")
        Return
    End If

    charIndex54 = charIndex54 + 1
Loop
Dim body As ChilkatJsonObject
body.Initialize
success = body.UpdateString("value", backup.StringOf("value"))
If success <> True Then
    Log("Could not set value.")
    Return
End If

Dim restoreUrl As ChilkatStringBuilder
restoreUrl.Initialize
success = restoreUrl.Append(origin.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = restoreUrl.Append("/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = restoreUrl.Append(config.StringOf("object_kind"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = restoreUrl.Append("/restore?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

success = http.HttpJson("POST", restoreUrl.GetAsString, body, "application/json", response)
If success <> True Then
    Log("Request failed; reconcile uncertain writes before retrying.")
    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(config.StringOf("object_kind"))
Dim stringMatch12 As Boolean = stringCompare.ContentsEqual("keys", True)
If stringMatch12 = True Then
    stringCompare.SetString(result.StringOf("key.kid"))
    Dim stringMatch13 As Boolean = stringCompare.ContentsEqual("", True)
    If (result.JsonTypeOf("key.kid") <> 1) Or (stringMatch13 = True) Then
        Log("Missing or invalid key.kid.")
        Return
    End If

    Dim returnedVersion55Prefix As ChilkatStringBuilder
    returnedVersion55Prefix.Initialize
    success = returnedVersion55Prefix.Append(resourceBase.GetAsString)
    If success <> True Then
        Log("Could not construct URL or identifier.")
        Return
    End If

    success = returnedVersion55Prefix.Append("/")
    If success <> True Then
        Log("Could not construct URL or identifier.")
        Return
    End If

    Dim returnedVersion55Id As ChilkatStringBuilder
    returnedVersion55Id.Initialize
    success = returnedVersion55Id.Append(result.StringOf("key.kid"))
    If success <> True Then
        Log("Could not construct URL or identifier.")
        Return
    End If

    If returnedVersion55Id.StartsWith(returnedVersion55Prefix.GetAsString, True) <> True Then
        Log("Returned identifier is outside the expected vault/resource.")
        Return
    End If

    Dim returnedVersion55 As String = returnedVersion55Id.GetRange(returnedVersion55Prefix.Length, returnedVersion55Id.Length - returnedVersion55Prefix.Length, False)
    Dim validate56 As ChilkatStringBuilder
    validate56.Initialize
    Dim alphabet56 As ChilkatStringBuilder
    alphabet56.Initialize
    success = validate56.Append(returnedVersion55)
    If success <> True Then
        Log("Could not validate resource identifier.")
        Return
    End If

    success = alphabet56.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
    If success <> True Then
        Log("Could not initialize validation.")
        Return
    End If

    If (validate56.Length < 1) Or (validate56.Length > 127) Then
        Log("Invalid resource identifier length.")
        Return
    End If

    Dim charIndex56 As Int = 0
    Do While (charIndex56 < validate56.Length)
        If alphabet56.Contains(validate56.GetRange(charIndex56, 1, False), True) <> True Then
            Log("Invalid character in resource identifier.")
            Return
        End If

        charIndex56 = charIndex56 + 1
    Loop
Else
    stringCompare.SetString(result.StringOf("id"))
    Dim stringMatch14 As Boolean = stringCompare.ContentsEqual("", True)
    If (result.JsonTypeOf("id") <> 1) Or (stringMatch14 = True) Then
        Log("Missing or invalid id.")
        Return
    End If

    Dim returnedVersion57Prefix As ChilkatStringBuilder
    returnedVersion57Prefix.Initialize
    success = returnedVersion57Prefix.Append(resourceBase.GetAsString)
    If success <> True Then
        Log("Could not construct URL or identifier.")
        Return
    End If

    success = returnedVersion57Prefix.Append("/")
    If success <> True Then
        Log("Could not construct URL or identifier.")
        Return
    End If

    Dim returnedVersion57Id As ChilkatStringBuilder
    returnedVersion57Id.Initialize
    success = returnedVersion57Id.Append(result.StringOf("id"))
    If success <> True Then
        Log("Could not construct URL or identifier.")
        Return
    End If

    If returnedVersion57Id.StartsWith(returnedVersion57Prefix.GetAsString, True) <> True Then
        Log("Returned identifier is outside the expected vault/resource.")
        Return
    End If

    Dim returnedVersion57 As String = returnedVersion57Id.GetRange(returnedVersion57Prefix.Length, returnedVersion57Id.Length - returnedVersion57Prefix.Length, False)
    Dim validate58 As ChilkatStringBuilder
    validate58.Initialize
    Dim alphabet58 As ChilkatStringBuilder
    alphabet58.Initialize
    success = validate58.Append(returnedVersion57)
    If success <> True Then
        Log("Could not validate resource identifier.")
        Return
    End If

    success = alphabet58.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
    If success <> True Then
        Log("Could not initialize validation.")
        Return
    End If

    If (validate58.Length < 1) Or (validate58.Length > 127) Then
        Log("Invalid resource identifier length.")
        Return
    End If

    Dim charIndex58 As Int = 0
    Do While (charIndex58 < validate58.Length)
        If alphabet58.Contains(validate58.GetRange(charIndex58, 1, False), True) <> True Then
            Log("Invalid character in resource identifier.")
            Return
        End If

        charIndex58 = charIndex58 + 1
    Loop
End If

Log("Restore accepted. Verify restored versions and access before use.")
Log("Example completed.")