Azure Key Vault: List Certificates with Pagination
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.
Lists certificate identities and latest-version metadata, following every non-null nextLink, including after empty pages. This does not list historical versions or download certificates. Requests 25 entries per page, checks for cycles, and permits only the original vault and certificate-list path before forwarding the bearer token. Requires certificates/list. Enumeration is not a snapshot; concurrent updates can change results.
Chilkat Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
config: HCkJsonObject;
stringMatch1: Boolean;
validate1: HCkStringBuilder;
alphabet1: HCkStringBuilder;
charIndex1: Integer;
vaultName: HCkStringBuilder;
letters: HCkStringBuilder;
origin: HCkStringBuilder;
http: HCkHttp;
request: HCkHttpRequest;
response: HCkHttpResponse;
result: HCkJsonObject;
token: HCkJsonObject;
stringMatch2: Boolean;
stringMatch3: Boolean;
expires: HCkDateTime;
now: HCkDateTime;
certPrefix: HCkStringBuilder;
listPrefix: HCkStringBuilder;
listUrl: HCkStringBuilder;
listSeen: HCkHashtable;
listItems: HCkJsonArray;
listView: HCkJsonArray;
listEntry: HCkJsonObject;
listCurrentUrl: PWideChar;
listPages: Integer;
listIndex: Integer;
listMore: Boolean;
stringMatch4: Boolean;
listNext: HCkStringBuilder;
stringMatch5: Boolean;
listedNameId: HCkStringBuilder;
listedName: PWideChar;
validate6: HCkStringBuilder;
alphabet6: HCkStringBuilder;
charIndex6: Integer;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this configuration private, outside public web content and source control.
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/keyvault.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private Key Vault configuration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'vault_name'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'vault_name') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid vault_name.');
Exit;
end;
validate1 := CkStringBuilder_Create();
alphabet1 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate1,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate vault_name.');
Exit;
end;
success := CkStringBuilder_Append(alphabet1,'abcdefghijklmnopqrstuvwxyz0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate1) < 3) or (CkStringBuilder_getLength(validate1) > 24)) then
begin
Memo1.Lines.Add('Invalid vault_name length.');
Exit;
end;
charIndex1 := 0;
while (charIndex1 < CkStringBuilder_getLength(validate1)) do
begin
if (CkStringBuilder_Contains(alphabet1,CkStringBuilder__getRange(validate1,charIndex1,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in vault_name.');
Exit;
end;
charIndex1 := charIndex1 + 1;
end;
vaultName := CkStringBuilder_Create();
CkStringBuilder_Append(vaultName,CkJsonObject__stringOf(config,'vault_name'));
letters := CkStringBuilder_Create();
CkStringBuilder_Append(letters,'abcdefghijklmnopqrstuvwxyz');
if ((CkStringBuilder_Contains(letters,CkStringBuilder__getRange(vaultName,0,1,False),True) <> True) or (CkStringBuilder_EndsWith(vaultName,'-',True) = True) or (CkStringBuilder_Contains(vaultName,'--',True) = True)) then
begin
Memo1.Lines.Add('Invalid vault_name.');
Exit;
end;
origin := CkStringBuilder_Create();
success := CkStringBuilder_Append(origin,'https://');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(origin,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(origin,'.vault.azure.net');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
request := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(request,'GET');
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
// First run Get a Key Vault access token.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/azureKeyVaultToken.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the private token file.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'access_token'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_token.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'expires_at'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'expires_at') <> 1) or (stringMatch3 = True)) then
begin
Memo1.Lines.Add('Missing or invalid expires_at.');
Exit;
end;
expires := CkDateTime_Create();
now := CkDateTime_Create();
success := CkDateTime_SetFromTimestamp(expires,CkJsonObject__stringOf(token,'expires_at'));
if (success <> True) then
begin
Memo1.Lines.Add('Invalid token expiration.');
Exit;
end;
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
certPrefix := CkStringBuilder_Create();
success := CkStringBuilder_Append(certPrefix,CkStringBuilder__getAsString(origin));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(certPrefix,'/certificates/');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
listPrefix := CkStringBuilder_Create();
success := CkStringBuilder_Append(listPrefix,CkStringBuilder__getAsString(origin));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(listPrefix,'/certificates?');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
listUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(listUrl,CkStringBuilder__getAsString(listPrefix));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(listUrl,'api-version=2026-04-01&maxresults=25');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
listSeen := CkHashtable_Create();
listItems := CkJsonArray_Create();
listView := CkJsonArray_Create();
listEntry := CkJsonObject_Create();
listCurrentUrl := CkStringBuilder__getAsString(listUrl);
listPages := 0;
listMore := True;
while (listMore = True) do
begin
if (listPages = 10000) then
begin
Memo1.Lines.Add('Page bound reached; enumeration is incomplete.');
Exit;
end;
if (CkHashtable_Contains(listSeen,listCurrentUrl) = True) then
begin
Memo1.Lines.Add('Repeated nextLink; enumeration is incomplete.');
Exit;
end;
success := CkHashtable_AddStr(listSeen,listCurrentUrl,'seen');
if (success <> True) then
begin
Memo1.Lines.Add('Could not track pagination.');
Exit;
end;
listPages := listPages + 1;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Token is expired or near expiration. Obtain a new token before continuing.');
Exit;
end;
success := CkHttp_HttpReq(http,listCurrentUrl,request,response);
if (success <> True) then
begin
Memo1.Lines.Add('Request did not complete; inspect diagnostics privately.');
Exit;
end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
// Do not print full response bodies, tokens, or diagnostic logs.
Memo1.Lines.Add('Request failed. Inspect private diagnostics; reconcile writes before retrying.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(result,'value') <> 4) then
begin
Memo1.Lines.Add('Expected a value array.');
Exit;
end;
success := CkJsonObject_ArrayOf2(result,'value',listView);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read certificate array.');
Exit;
end;
success := CkJsonArray_Load(listItems,CkJsonArray__emit(listView));
if (success <> True) then
begin
Memo1.Lines.Add('Could not detach the page before nested requests.');
Exit;
end;
listMore := False;
if (CkJsonObject_HasMember(result,'nextLink') = True) then
begin
if (CkJsonObject_JsonTypeOf(result,'nextLink') <> 6) then
begin
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'nextLink'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'nextLink') <> 1) or (stringMatch4 = True)) then
begin
Memo1.Lines.Add('Missing or invalid nextLink.');
Exit;
end;
listCurrentUrl := CkJsonObject__stringOf(result,'nextLink');
listNext := CkStringBuilder_Create();
success := CkStringBuilder_Append(listNext,listCurrentUrl);
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
if ((CkStringBuilder_StartsWith(listNext,CkStringBuilder__getAsString(listPrefix),True) <> True) or (CkStringBuilder_Contains(listNext,'#',True) = True) or (CkStringBuilder_Contains(listNext,'\',True) = True) or (CkStringBuilder_Contains(listNext,#13,True) = True) or (CkStringBuilder_Contains(listNext,#10,True) = True)) then
begin
Memo1.Lines.Add('Unsafe pagination URL; stopped before sending a token.');
Exit;
end;
listMore := True;
end;
end;
listIndex := 0;
while (listIndex < CkJsonArray_getSize(listItems)) do
begin
success := CkJsonArray_ObjectAt2(listItems,listIndex,listEntry);
if (success <> True) then
begin
Memo1.Lines.Add('Expected a certificate object.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(listEntry,'id'));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(listEntry,'id') <> 1) or (stringMatch5 = True)) then
begin
Memo1.Lines.Add('Missing or invalid id.');
Exit;
end;
listedNameId := CkStringBuilder_Create();
success := CkStringBuilder_Append(listedNameId,CkJsonObject__stringOf(listEntry,'id'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
if (CkStringBuilder_StartsWith(listedNameId,CkStringBuilder__getAsString(certPrefix),True) <> True) then
begin
Memo1.Lines.Add('Returned identifier is outside the expected vault/resource.');
Exit;
end;
listedName := CkStringBuilder__getRange(listedNameId,CkStringBuilder_getLength(certPrefix),CkStringBuilder_getLength(listedNameId) - CkStringBuilder_getLength(certPrefix),False);
validate6 := CkStringBuilder_Create();
alphabet6 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate6,listedName);
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate resource identifier.');
Exit;
end;
success := CkStringBuilder_Append(alphabet6,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate6) < 1) or (CkStringBuilder_getLength(validate6) > 127)) then
begin
Memo1.Lines.Add('Invalid resource identifier length.');
Exit;
end;
charIndex6 := 0;
while (charIndex6 < CkStringBuilder_getLength(validate6)) do
begin
if (CkStringBuilder_Contains(alphabet6,CkStringBuilder__getRange(validate6,charIndex6,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in resource identifier.');
Exit;
end;
charIndex6 := charIndex6 + 1;
end;
Memo1.Lines.Add('Certificate ID: ' + CkJsonObject__stringOf(listEntry,'id'));
if (CkJsonObject_JsonTypeOf(listEntry,'attributes.enabled') = 5) then
begin
Memo1.Lines.Add('Enabled: ' + IntToStr(Ord(CkJsonObject_BoolOf(listEntry,'attributes.enabled'))));
end;
// Attributes are optional; preserve Unix-time values as strings (not 32-bit integers).
if (CkJsonObject_JsonTypeOf(listEntry,'attributes.exp') = 2) then
begin
Memo1.Lines.Add('Expires (Unix seconds): ' + CkJsonObject__stringOf(listEntry,'attributes.exp'));
end;
listIndex := listIndex + 1;
end;
end;
Memo1.Lines.Add('Certificate enumeration complete.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(validate1);
CkStringBuilder_Dispose(alphabet1);
CkStringBuilder_Dispose(vaultName);
CkStringBuilder_Dispose(letters);
CkStringBuilder_Dispose(origin);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(request);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(token);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkStringBuilder_Dispose(certPrefix);
CkStringBuilder_Dispose(listPrefix);
CkStringBuilder_Dispose(listUrl);
CkHashtable_Dispose(listSeen);
CkJsonArray_Dispose(listItems);
CkJsonArray_Dispose(listView);
CkJsonObject_Dispose(listEntry);
CkStringBuilder_Dispose(listNext);
CkStringBuilder_Dispose(listedNameId);
CkStringBuilder_Dispose(validate6);
CkStringBuilder_Dispose(alphabet6);