Sample code for 30+ languages & platforms
Unicode C

AWS KMS List Keys

See more AWS KMS Examples

Gets a list of all KMS keys in the caller's AWS account and Region.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAwsW authAws;
    const wchar_t *strJson;
    HCkJsonObjectW json;
    const wchar_t *KeyArn;
    const wchar_t *KeyId;
    int KeyCount;
    BOOL Truncated;
    int i;
    int count_i;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRestW_Create();

    // Connect to the Amazon AWS REST server.
    // Make sure to use the region that is correct for you.
    // such as https://kms.us-west-2.amazonaws.com/
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"kms.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials for the REST call.
    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
    // the region should match our URL above..
    CkAuthAwsW_putRegion(authAws,L"us-west-2");
    CkAuthAwsW_putServiceName(authAws,L"kms");

    CkRestW_SetAuthAws(rest,authAws);

    CkRestW_AddHeader(rest,L"X-Amz-Target",L"TrentService.ListKeys");
    CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.1");

    strJson = CkRestW_fullRequestString(rest,L"POST",L"/",L"{}");
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        return;
    }

    // A successful response will have a status code equal to 200.
    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
        wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
        wprintf(L"response body: %s\n",strJson);
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        return;
    }

    // Examine the successful JSON response (shown below)
    json = CkJsonObjectW_Create();
    CkJsonObjectW_Load(json,strJson);
    CkJsonObjectW_putEmitCompact(json,FALSE);

    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // Sample output:

    // {
    //   "KeyCount": 4,
    //   "Keys": [
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/082f8520-7afc-4a09-b703-89b7243072e5",
    //       "KeyId": "082f8520-7afc-4a09-b703-89b7243072e5"
    //     },
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/17432483-ff08-4950-93d3-f46ebb5e17d1",
    //       "KeyId": "17432483-ff08-4950-93d3-f46ebb5e17d1"
    //     },
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/1b0e5b3c-0675-4510-adb6-a75b40a93da0",
    //       "KeyId": "1b0e5b3c-0675-4510-adb6-a75b40a93da0"
    //     },
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/265e3993-428b-4581-9466-b1030a53062f",
    //       "KeyId": "265e3993-428b-4581-9466-b1030a53062f"
    //     },
    //   ],
    //   "Truncated": false
    // }

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    KeyCount = CkJsonObjectW_IntOf(json,L"KeyCount");
    Truncated = CkJsonObjectW_BoolOf(json,L"Truncated");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"Keys");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        KeyArn = CkJsonObjectW_stringOf(json,L"Keys[i].KeyArn");
        KeyId = CkJsonObjectW_stringOf(json,L"Keys[i].KeyId");
        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkJsonObjectW_Dispose(json);

    }