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

Amazon SNS: Read SMS Account Settings

See more Amazon SNS Examples

Uses the current Amazon SNS Query API (Version=2010-03-31) through Chilkat.Rest and AuthAws, with HTTPS, Signature Version 4, and service sns. The API version date remains current. Action and input parameters are URL-encoded into a POST body, avoiding message text and recipient addresses in the URL. Set the actual region once; other AWS partitions need their documented endpoint suffix and ARN partition.

Load private qa_data/aws/sns.json with nonempty string fields access_key, secret_key, and session_token when using temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles, obtain credentials, or refresh them automatically. The session token is included in the signed form body on every request. A Socket explicitly enables TLS certificate verification before passing the connection to Rest.

Checks setup calls, transport completion, HTTP status, XML parsing, result root, and required fields. Normal output includes identifiers, not full messages or headers. Keep detailed diagnostics private. For retryable failures, use bounded backoff with jitter in the application; these examples do not automatically retry writes. A Publish timeout can leave an accepted message and retrying a standard-topic/SMS publish can duplicate it. A returned MessageId confirms SNS acceptance, not subscriber or handset delivery.

Reads the regional SMS settings map. Notice that the Query API response uses lowercase attributes. Missing optional keys are valid. Protect role, report-bucket and account configuration details in output.

Required IAM actions: sns:GetSMSAttributes. Related SQS/KMS permissions are separate.

AWS references: GetSMSAttributes

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set stringCompare [new_CkStringBuilder]

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
#  Private credentials; do not put this file in public web content or source control.
set credentials [new_CkJsonObject]

CkJsonObject_put_Strict $credentials 1
set success [CkJsonObject_LoadFile $credentials "qa_data/aws/sns.json"]
if {$success != 1} then {
    puts "Could not load private SNS credentials."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    exit
}

CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "access_key"]
set stringMatch1 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "access_key"] != 1]  ||  [$stringMatch1 == 1]} then {
    puts "Missing or invalid access_key."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    exit
}

CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "secret_key"]
set stringMatch2 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "secret_key"] != 1]  ||  [$stringMatch2 == 1]} then {
    puts "Missing or invalid secret_key."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    exit
}

set region "us-west-2"
set endpoint [new_CkStringBuilder]

CkStringBuilder_Append $endpoint "sns."
CkStringBuilder_Append $endpoint $region
CkStringBuilder_Append $endpoint ".amazonaws.com"
set aws [new_CkAuthAws]

CkAuthAws_put_AccessKey $aws [CkJsonObject_stringOf $credentials "access_key"]
CkAuthAws_put_SecretKey $aws [CkJsonObject_stringOf $credentials "secret_key"]
CkAuthAws_put_Region $aws $region
CkAuthAws_put_ServiceName $aws "sns"
CkAuthAws_put_SignatureVersion $aws 4
set rest [new_CkRest]

CkRest_put_ConnectTimeoutMs $rest 20000
CkRest_put_IdleTimeoutMs $rest 60000
CkRest_put_VerboseLogging $rest 0
set success [CkRest_SetAuthAws $rest $aws]
if {$success != 1} then {
    puts "Could not configure AWS signing."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    exit
}

if {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
    CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "session_token"]
    set stringMatch3 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
    if {expr [[CkJsonObject_JsonTypeOf $credentials "session_token"] != 1]  ||  [$stringMatch3 == 1]} then {
        puts "session_token must be a nonempty string when supplied."
        delete_CkStringBuilder $stringCompare
        delete_CkJsonObject $credentials
        delete_CkStringBuilder $endpoint
        delete_CkAuthAws $aws
        delete_CkRest $rest
        exit
    }

}

set xml [new_CkXml]

CkXml_put_Strict $xml 1

#  Explicit certificate verification; keep this Socket alive while Rest uses it.
set connection [new_CkSocket]

CkSocket_put_RequireSslCertVerify $connection 1
CkSocket_put_MaxReadIdleMs $connection 60000
CkSocket_put_MaxSendIdleMs $connection 60000
set success [CkSocket_Connect $connection [CkStringBuilder_getAsString $endpoint] 443 1 20000]
if {$success != 1} then {
    puts "TLS connection failed; inspect diagnostics privately."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

set success [CkRest_UseConnection $rest $connection 1]
if {$success != 1} then {
    puts "Could not attach TLS connection."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

set success [CkRest_ClearAllQueryParams $rest]
if {$success != 1} then {
    puts "Could not clear parameters from the previous request."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

#  Build a fresh form for each request; do not retain parameters from a previous action.
#  AddQueryParam encodes raw values into the POST body. Do not pre-encode them.
set success [CkRest_AddQueryParam $rest "Action" "GetSMSAttributes"]
if {$success != 1} then {
    puts "Could not set Action."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

set success [CkRest_AddQueryParam $rest "Version" "2010-03-31"]
if {$success != 1} then {
    puts "Could not set Version."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

#  Include temporary credentials in the signed form body on every request.
if {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
    set success [CkRest_AddQueryParam $rest "X-Amz-Security-Token" [CkJsonObject_stringOf $credentials "session_token"]]
    if {$success != 1} then {
        puts "Could not set X-Amz-Security-Token."
        delete_CkStringBuilder $stringCompare
        delete_CkJsonObject $credentials
        delete_CkStringBuilder $endpoint
        delete_CkAuthAws $aws
        delete_CkRest $rest
        delete_CkXml $xml
        delete_CkSocket $connection
        exit
    }

}

set responseXml [CkRest_fullRequestFormUrlEncoded $rest "POST" "/"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts "Request did not complete. Inspect diagnostics privately."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

set success [CkXml_LoadXml $xml $responseXml]
if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    puts "SNS HTTP status: [CkRest_get_ResponseStatusCode $rest]"
    if {$success == 1} then {
        set value [CkXml_getChildContent $xml "Error|Code"]
        if {[CkXml_get_LastMethodSuccess $xml] == 1} then {
            puts "AWS error code: $value"
        }

        set value [CkXml_getChildContent $xml "RequestId"]
        if {[CkXml_get_LastMethodSuccess $xml] == 1} then {
            puts "AWS request ID: $value"
        }

    }

    #  Full messages, headers, and signing diagnostics may contain private data.
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

if {$success != 1} then {
    puts "Expected a well-formed XML response."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

CkStringBuilder_SetString $stringCompare [CkXml_tag $xml]
set stringMatch4 [CkStringBuilder_ContentsEqual $stringCompare "GetSMSAttributesResponse" 1]
if {$stringMatch4 != 1} then {
    puts "Unexpected SNS response element."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

set value [CkXml_getChildContent $xml "ResponseMetadata|RequestId"]
CkStringBuilder_SetString $stringCompare $value
set stringMatch5 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $xml] != 1]  ||  [$stringMatch5 == 1]} then {
    puts "Missing or empty ResponseMetadata|RequestId."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

puts "AWS request ID: $value"
if {[CkXml_HasChildWithTag $xml "GetSMSAttributesResult"] != 1} then {
    puts "Missing result."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkSocket $connection
    exit
}

set attributes [new_CkJsonObject]

set mapIndex 0
while {$mapIndex < [CkXml_NumChildrenAt $xml "GetSMSAttributesResult|attributes"]} {
    CkXml_put_I $xml $mapIndex
    set value [CkXml_getChildContent $xml "GetSMSAttributesResult|attributes|entry[i]|key"]
    CkStringBuilder_SetString $stringCompare $value
    set stringMatch6 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
    if {expr [[CkXml_get_LastMethodSuccess $xml] != 1]  ||  [$stringMatch6 == 1]} then {
        puts "{Missing or empty GetSMSAttributesResult|attributes|entry[i]|key.}"
        delete_CkStringBuilder $stringCompare
        delete_CkJsonObject $credentials
        delete_CkStringBuilder $endpoint
        delete_CkAuthAws $aws
        delete_CkRest $rest
        delete_CkXml $xml
        delete_CkSocket $connection
        delete_CkJsonObject $attributes
        exit
    }

    set mapKey $value
    set value [CkXml_getChildContent $xml "GetSMSAttributesResult|attributes|entry[i]|value"]
    if {[CkXml_get_LastMethodSuccess $xml] != 1} then {
        puts "Missing map value."
        delete_CkStringBuilder $stringCompare
        delete_CkJsonObject $credentials
        delete_CkStringBuilder $endpoint
        delete_CkAuthAws $aws
        delete_CkRest $rest
        delete_CkXml $xml
        delete_CkSocket $connection
        delete_CkJsonObject $attributes
        exit
    }

    set success [CkJsonObject_AppendString $attributes $mapKey $value]
    if {$success != 1} then {
        puts "Invalid or duplicate map entry."
        delete_CkStringBuilder $stringCompare
        delete_CkJsonObject $credentials
        delete_CkStringBuilder $endpoint
        delete_CkAuthAws $aws
        delete_CkRest $rest
        delete_CkXml $xml
        delete_CkSocket $connection
        delete_CkJsonObject $attributes
        exit
    }

    set mapIndex [expr $mapIndex + 1]
}
puts [CkJsonObject_emit $attributes]
puts "Example completed."

delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkStringBuilder $endpoint
delete_CkAuthAws $aws
delete_CkRest $rest
delete_CkXml $xml
delete_CkSocket $connection
delete_CkJsonObject $attributes