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

Amazon Cognito: List and Filter Users with Pagination

See more Amazon Cognito Examples

Enumerate users in a selected pool with PaginationToken and a page size of 60. Continue while a token is returned, even after a short or empty page. The example prints username, status, enabled state, and the stable sub identifier without dumping email/phone attributes. Stops explicitly at 1000 pages.

ListUsers is eventually consistent; use AdminGetUser to check a specific user. Its server-side filter supports one supported standard attribute at a time, using exact (=) or prefix (^=) matching. Custom attributes are not searchable. The optional userFilter expression is encoded by JsonObject, including embedded quotes. Omitting AttributesToGet avoids errors caused by requesting an attribute absent on some matching users. Save the pool ID with each user sub.

Uses the Amazon Cognito User Pools regional JSON API with Chilkat.Rest and AuthAws (Signature Version 4, service cognito-idp). It does not use Identity Pools, the OAuth domain, or a Cognito bearer token. Set the region to match the pool; this example uses the standard AWS commercial endpoint. Other partitions have their own endpoint suffixes.

Supply a private qa_data/aws/cognito.json with string fields access_key, secret_key, and, for temporary IAM/STS credentials, session_token. Prefer short-lived role credentials and refresh them before expiry. The example reads this file explicitly; it does not automatically discover an AWS profile or role. The STS session token is sent in the documented X-Amz-Security-Token query parameter and included in the signature; keep request URLs and diagnostics private. Keep credentials outside public web content and source control. IAM administration can run in a controlled backoffice desktop application or a backend; never embed administrative AWS keys in publicly distributed software.

Required IAM action: cognito-idp:ListUsers. Scope access to the intended user-pool ARN.

Checks both transport completion and HTTP 200, and validates JSON when response data is expected. AWS error details use __type and message; inspect sensitive diagnostics privately. An HTTP 400 TooManyRequestsException needs bounded exponential backoff with jitter; expired temporary credentials need renewal. Check IAM permission and region for authorization/resource errors. For an uncertain write, inspect the existing resource before retrying. This sample does not automatically retry writes.

AWS ListUsers 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

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    --  Private configuration, not a file in public web content or source control.
    --  Load current IAM credentials; these are NOT Cognito app-client credentials or JWTs.
    DECLARE @credentials int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

        PRINT 'Could not load private AWS credentials.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

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

        PRINT 'Missing or invalid secret_key.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    --  Set this once to the region containing the user pool.
    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, 'cognito-idp.'
    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', 'cognito-idp'
    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_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure AWS signing.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    --  Temporary IAM/STS credentials require their session token on every signed request.
    --  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
    --  Leave session_token absent or empty only for credentials that do not have one.
    EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'session_token must be a string.'
            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, 'StringOf', @sTmp0 OUT, 'session_token'
        IF @sTmp0 <> ''
          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 the session-token parameter.'
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                RETURN
              END
          END
      END
    --  Supply a Socket to explicitly enable TLS certificate verification.
    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 @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
    IF @success <> 1
      BEGIN

        PRINT 'Could not attach the TLS connection.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.0'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the content type.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END

    DECLARE @body int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT

    DECLARE @result int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT

    EXEC sp_OASetProperty @result, 'Strict', 1
    DECLARE @requestBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @requestBody OUT

    DECLARE @responseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @responseBody OUT

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.ListUsers'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the Cognito operation.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        RETURN
      END
    DECLARE @userPoolId nvarchar(4000)
    SELECT @userPoolId = 'us-west-2_EXAMPLE'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserPoolId', @userPoolId
    EXEC sp_OAMethod @body, 'UpdateInt', @success OUT, 'Limit', 60
    --  Optional filter expression, for example: email = "user@example.com"
    --  Leave empty to list all users. JsonObject handles JSON escaping.
    DECLARE @userFilter nvarchar(4000)
    SELECT @userFilter = ''
    IF @userFilter <> ''
      BEGIN
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Filter', @userFilter
      END
    DECLARE @j int
    SELECT @j = 0
    DECLARE @attributeCount int
    SELECT @attributeCount = 0
    --  Page size is not a total-result limit. Continue even after an empty page.
    DECLARE @nextToken nvarchar(4000)
    SELECT @nextToken = ''
    DECLARE @more int
    SELECT @more = 1
    DECLARE @page int
    SELECT @page = 0
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count int
    SELECT @count = 0
    DECLARE @seenTokens int
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenTokens OUT

    WHILE (@more = 1)
      BEGIN
        --  Demonstration bound: report partial results instead of silently truncating.
        IF @page = 1000
          BEGIN

            PRINT 'Stopped at 1000 pages; enumeration is incomplete. Resume with nextToken.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        --  EmitSb appends, so clear the buffer before each page/request.
        EXEC sp_OAMethod @requestBody, 'Clear', NULL
        EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
        IF @success <> 1
          BEGIN

            PRINT 'Could not encode the request.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        --  Chilkat signs this POST, including the current headers and JSON body.
        EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
        IF @success <> 1
          BEGIN
            --  A failed exchange can leave the outcome of a write unknown. Do not blindly retry it.

            PRINT 'Request did not complete; inspect LastErrorText privately.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            PRINT 'Cognito HTTP status: ' + @iTmp0
            --  AWS commonly reports throttling and expired credentials as HTTP 400, not 429/401.
            EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
            IF @success = 1
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                PRINT 'AWS error type: ' + @sTmp0
                --  Inspect message and response headers privately; they can contain account/user data.
              END
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
        IF @success <> 1
          BEGIN

            PRINT 'Expected a well-formed JSON response.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Users'
        IF @iTmp0 <> 4
          BEGIN

            PRINT 'Expected Users array.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @result, 'SizeOfArray', @count OUT, 'Users'
        SELECT @i = 0
        WHILE @i < @count
          BEGIN
            EXEC sp_OASetProperty @result, 'I', @i
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Users[i].Username'
            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Users[i].Username'
            IF (@iTmp0 <> 1) or (@sTmp0 = '')
              BEGIN

                PRINT 'Missing or invalid Users[i].Username.'
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @body
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @requestBody
                EXEC @hr = sp_OADestroy @responseBody
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Users[i].Username'
            PRINT 'Username: ' + @sTmp0

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Users[i].UserStatus'
            PRINT 'Status: ' + @sTmp0

            EXEC sp_OAMethod @result, 'BoolOf', @iTmp0 OUT, 'Users[i].Enabled'
            PRINT 'Enabled: ' + @iTmp0
            --  Attributes are a name/value array, not an object keyed by attribute name.
            SELECT @j = 0
            EXEC sp_OAMethod @result, 'SizeOfArray', @attributeCount OUT, 'Users[i].Attributes'
            WHILE @j < @attributeCount
              BEGIN
                EXEC sp_OASetProperty @result, 'J', @j
                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Users[i].Attributes[j].Name'
                IF @sTmp0 = 'sub'
                  BEGIN

                    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Users[i].Attributes[j].Value'
                    PRINT 'User sub: ' + @sTmp0
                  END
                SELECT @j = @j + 1
              END
            SELECT @i = @i + 1
          END
        SELECT @page = @page + 1
        SELECT @more = 0
        EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'PaginationToken'
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'PaginationToken'
            IF @iTmp0 <> 6
              BEGIN
                EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'PaginationToken'
                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'PaginationToken'
                IF (@iTmp0 <> 1) or (@sTmp0 = '')
                  BEGIN

                    PRINT 'Missing or invalid PaginationToken.'
                    EXEC @hr = sp_OADestroy @credentials
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @aws
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @body
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @requestBody
                    EXEC @hr = sp_OADestroy @responseBody
                    EXEC @hr = sp_OADestroy @seenTokens
                    RETURN
                  END
                EXEC sp_OAMethod @result, 'StringOf', @nextToken OUT, 'PaginationToken'
                EXEC sp_OAMethod @seenTokens, 'Contains', @iTmp0 OUT, @nextToken
                IF @iTmp0 = 1
                  BEGIN

                    PRINT 'Repeated pagination token; enumeration is incomplete.'
                    EXEC @hr = sp_OADestroy @credentials
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @aws
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @body
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @requestBody
                    EXEC @hr = sp_OADestroy @responseBody
                    EXEC @hr = sp_OADestroy @seenTokens
                    RETURN
                  END
                EXEC sp_OAMethod @seenTokens, 'AddInt', @success OUT, @nextToken, 1
                EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'PaginationToken', @nextToken
                SELECT @more = 1
              END
          END
      END

    PRINT 'Enumeration complete.'

    EXEC @hr = sp_OADestroy @credentials
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @aws
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @requestBody
    EXEC @hr = sp_OADestroy @responseBody
    EXEC @hr = sp_OADestroy @seenTokens


END
GO