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

Amazon SNS: Subscribe an Email Address (Confirmation Required)

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.

Subscribes an endpoint to a standard topic. The sample explicitly requests ReturnSubscriptionArn=false and distinguishes the literal pending confirmation from a returned ARN. Email recipients must follow the confirmation workflow; an accepted Subscribe request does not prove confirmation or delivery. Do not bypass endpoint-owner confirmation. Email/SMS are not FIFO-topic subscription protocols.

Use a recipient address you are authorized to subscribe. SNS sends a confirmation email; the confirmation token remains valid for two days. This example does not retrieve or automatically follow confirmation links. Cross-account subscriptions can also require endpoint-owner confirmation.

Required IAM action: sns:Subscribe. Grant only the intended resource access; direct SMS does not have a topic ARN resource. Topic policies and KMS key access can also affect requests.

AWS Subscribe reference

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

set topicArn "arn:aws:sns:us-west-2:123456789012:chilkat-standard-example"
set topic [new_CkStringBuilder]

CkStringBuilder_Append $topic $topicArn
set arnPrefix [new_CkStringBuilder]

CkStringBuilder_Append $arnPrefix "arn:aws:sns:"
CkStringBuilder_Append $arnPrefix $region
CkStringBuilder_Append $arnPrefix ":"
if {[CkStringBuilder_StartsWith $topic [CkStringBuilder_getAsString $arnPrefix] 1] != 1} then {
    puts "Select an SNS topic ARN in the configured region and commercial AWS partition."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    exit
}

if {[CkStringBuilder_EndsWith $topic ".fifo" 1] == 1} then {
    puts "This example uses a standard topic. FIFO publishing needs group/deduplication handling."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    exit
}

set emailAddress "recipient@example.com"
#  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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

#  Build a fresh form for each request; do not retain previous parameters.
#  AddQueryParam encodes raw values into the POST body. Do not pre-encode them.
set success [CkRest_AddQueryParam $rest "Action" "Subscribe"]
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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
        delete_CkStringBuilder $arnPrefix
        delete_CkSocket $connection
        exit
    }

}

set success [CkRest_AddQueryParam $rest "TopicArn" $topicArn]
if {$success != 1} then {
    puts "Could not set TopicArn."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

set success [CkRest_AddQueryParam $rest "Protocol" "email"]
if {$success != 1} then {
    puts "Could not set Protocol."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

set success [CkRest_AddQueryParam $rest "Endpoint" $emailAddress]
if {$success != 1} then {
    puts "Could not set Endpoint."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

set success [CkRest_AddQueryParam $rest "ReturnSubscriptionArn" "false"]
if {$success != 1} then {
    puts "Could not set ReturnSubscriptionArn."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

set responseXml [CkRest_fullRequestFormUrlEncoded $rest "POST" "/"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts "Request did not complete. Its outcome may be uncertain; reconcile before retrying."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

CkStringBuilder_SetString $stringCompare [CkXml_tag $xml]
set stringMatch4 [CkStringBuilder_ContentsEqual $stringCompare "SubscribeResponse" 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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    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_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

puts "AWS request ID: $value"
set value [CkXml_getChildContent $xml "SubscribeResult|SubscriptionArn"]
CkStringBuilder_SetString $stringCompare $value
set stringMatch6 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $xml] != 1]  ||  [$stringMatch6 == 1]} then {
    puts "Missing or empty SubscribeResult|SubscriptionArn."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkStringBuilder $endpoint
    delete_CkAuthAws $aws
    delete_CkRest $rest
    delete_CkXml $xml
    delete_CkStringBuilder $topic
    delete_CkStringBuilder $arnPrefix
    delete_CkSocket $connection
    exit
}

CkStringBuilder_SetString $stringCompare $value
set stringMatch7 [CkStringBuilder_ContentsEqual $stringCompare "pending confirmation" 1]
if {$stringMatch7 == 1} then {
    puts "Subscription is pending confirmation; delivery is not active yet."
} else {
    puts "Subscription ARN: $value"
    puts "Subscription request succeeded."
}


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