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

Amazon SNS: List All Topics with Pagination

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.

Enumerates the requester's topics in one region, following NextToken through all pages, including empty pages with a continuation token. ListTopics returns at most 100 topics per page and has no MaxResults parameter. The example rebuilds the form parameters after each POST, detects repeated tokens, and reports incomplete enumeration at 10,000 pages. Absence or an empty NextToken ends the listing. Tokens are opaque and results are not an atomic snapshot.

Required IAM action: sns:ListTopics. 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 ListTopics reference

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @stringCompare int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @stringCompare OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    --  Private credentials; do not put this file in public web content or source control.
    DECLARE @credentials int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT

    EXEC sp_OASetProperty @credentials, 'Strict', 1
    EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/sns.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private SNS credentials.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch1 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch1 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@stringMatch1 = 1)
      BEGIN

        PRINT 'Missing or invalid access_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @stringMatch2 int
    EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch2 OUT, '', 1
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
    IF (@iTmp0 <> 1) or (@stringMatch2 = 1)
      BEGIN

        PRINT 'Missing or invalid secret_key.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    DECLARE @region nvarchar(4000)
    SELECT @region = 'us-west-2'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'sns.'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
    DECLARE @aws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT

    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
    EXEC sp_OASetProperty @aws, 'Region', @region
    EXEC sp_OASetProperty @aws, 'ServiceName', 'sns'
    EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
    EXEC sp_OASetProperty @rest, 'VerboseLogging', 0
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure AWS signing.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
        EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
        DECLARE @stringMatch3 int
        EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch3 OUT, '', 1
        EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
        IF (@iTmp0 <> 1) or (@stringMatch3 = 1)
          BEGIN

            PRINT 'session_token must be a nonempty string when supplied.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            RETURN
          END
      END
    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OASetProperty @xml, 'Strict', 1
    DECLARE @value nvarchar(4000)

    --  Explicit certificate verification; keep this Socket alive while Rest uses it.
    DECLARE @connection int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT

    EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
    EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
    IF @success <> 1
      BEGIN

        PRINT 'TLS connection failed; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
    IF @success <> 1
      BEGIN

        PRINT 'Could not attach TLS connection.'
        EXEC @hr = sp_OADestroy @stringCompare
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    DECLARE @seenTokens int
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenTokens OUT

    DECLARE @nextToken nvarchar(4000)
    SELECT @nextToken = ''
    DECLARE @more int
    SELECT @more = 1
    DECLARE @page int
    SELECT @page = 0
    WHILE (@more = 1)
      BEGIN
        IF @page = 10000
          BEGIN

            PRINT 'Page bound reached; topic enumeration is incomplete.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        SELECT @page = @page + 1
        EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
        IF @success <> 1
          BEGIN

            PRINT 'Could not clear parameters from the previous request.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        --  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.
        EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Action', 'ListTopics'
        IF @success <> 1
          BEGIN

            PRINT 'Could not set Action.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'Version', '2010-03-31'
        IF @success <> 1
          BEGIN

            PRINT 'Could not set Version.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        --  Include temporary credentials in the signed form body on every request.
        EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
            EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not set X-Amz-Security-Token.'
                EXEC @hr = sp_OADestroy @stringCompare
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @xml
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END
          END
        EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @nextToken
        DECLARE @stringMatch4 int
        EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch4 OUT, '', 1
        IF @stringMatch4 <> 1
          BEGIN
            EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'NextToken', @nextToken
            IF @success <> 1
              BEGIN

                PRINT 'Could not set NextToken.'
                EXEC @hr = sp_OADestroy @stringCompare
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @xml
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END
          END
        DECLARE @responseXml nvarchar(4000)
        EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @responseXml OUT, 'POST', '/'
        EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Request did not complete. Inspect diagnostics privately.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @responseXml
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            PRINT 'SNS HTTP status: ' + @iTmp0
            IF @success = 1
              BEGIN
                EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'Error|Code'
                EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
                IF @iTmp0 = 1
                  BEGIN

                    PRINT 'AWS error code: ' + @value
                  END
                EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'RequestId'
                EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
                IF @iTmp0 = 1
                  BEGIN

                    PRINT 'AWS request ID: ' + @value
                  END
              END
            --  Full messages, headers, and signing diagnostics may contain private data.
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        IF @success <> 1
          BEGIN

            PRINT 'Expected a well-formed XML response.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAGetProperty @xml, 'Tag', @sTmp0 OUT
        EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
        DECLARE @stringMatch5 int
        EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch5 OUT, 'ListTopicsResponse', 1
        IF @stringMatch5 <> 1
          BEGIN

            PRINT 'Unexpected SNS response element.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'ResponseMetadata|RequestId'
        EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @value
        DECLARE @stringMatch6 int
        EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch6 OUT, '', 1
        EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
        IF (@iTmp0 <> 1) or (@stringMatch6 = 1)
          BEGIN

            PRINT 'Missing or empty ResponseMetadata|RequestId.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END

        PRINT 'AWS request ID: ' + @value
        EXEC sp_OAMethod @xml, 'HasChildWithTag', @iTmp0 OUT, 'ListTopicsResult'
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Missing ListTopicsResult.'
            EXEC @hr = sp_OADestroy @stringCompare
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @xml
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        DECLARE @i int
        SELECT @i = 0
        EXEC sp_OAMethod @xml, 'NumChildrenAt', @iTmp0 OUT, 'ListTopicsResult|Topics'
        WHILE (@i < @iTmp0)
          BEGIN
            EXEC sp_OASetProperty @xml, 'I', @i
            EXEC sp_OAMethod @xml, 'GetChildContent', @value OUT, 'ListTopicsResult|Topics|member[i]|TopicArn'
            EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @value
            DECLARE @stringMatch7 int
            EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch7 OUT, '', 1
            EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
            IF (@iTmp0 <> 1) or (@stringMatch7 = 1)
              BEGIN

                PRINT 'Missing or empty ListTopicsResult|Topics|member[i]|TopicArn.'
                EXEC @hr = sp_OADestroy @stringCompare
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @xml
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END

            PRINT 'Topic ARN: ' + @value
            SELECT @i = @i + 1
          END
        EXEC sp_OAMethod @xml, 'GetChildContent', @nextToken OUT, 'ListTopicsResult|NextToken'
        EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 <> 1
          BEGIN
            SELECT @nextToken = ''
          END
        SELECT @more = 0
        EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @nextToken
        DECLARE @stringMatch8 int
        EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch8 OUT, '', 1
        IF @stringMatch8 <> 1
          BEGIN
            EXEC sp_OAMethod @seenTokens, 'Contains', @iTmp0 OUT, @nextToken
            IF @iTmp0 = 1
              BEGIN

                PRINT 'Repeated pagination token; topic enumeration is incomplete.'
                EXEC @hr = sp_OADestroy @stringCompare
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @xml
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END
            EXEC sp_OAMethod @seenTokens, 'AddStr', @success OUT, @nextToken, 'seen'
            IF @success <> 1
              BEGIN

                PRINT 'Could not track pagination.'
                EXEC @hr = sp_OADestroy @stringCompare
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @xml
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END
            SELECT @more = 1
          END
      END

    PRINT 'Topic enumeration complete.'

    EXEC @hr = sp_OADestroy @stringCompare
    EXEC @hr = sp_OADestroy @credentials
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @aws
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @seenTokens


END
GO