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

Amazon Cognito: Register and Confirm a User

See more Amazon Cognito Examples

Choose signup, confirm, or resend. A private signup-input.json supplies username, password and email for signup, or username and code for confirmation. Confirmation is a later user action; do not reuse a placeholder code or automatically confirm the user as an administrator. Resend requests another delivery. Self-registration must be enabled, and this password-based example must supply all required schema attributes.

Live operation: creates an account and can send email/SMS. Confirmation can establish a verified alias, so ForceAliasCreation=false prevents moving an existing alias from someone else. SignUp can return LimitExceeded even after creating an UNCONFIRMED user; investigate/resend rather than repeatedly creating accounts. Successful submission does not prove inbox delivery.

Uses direct Chilkat.Http requests to the regional Cognito User Pools JSON API. Public app-client operations do not use IAM signing. These examples use a public app client without a client secret; do not add administrative AWS credentials. For a confidential app client, implement the operation-specific SecretHash/SECRET_HASH or ClientSecret requirements on a trusted system.

Create qa_data/cognito/app.json with string fields region (for example us-west-2), user_pool_id, client_id, and account_key (your local account identifier). OAuth examples also require domain, the trusted HTTPS base URL of your Cognito domain, without a trailing slash. The region and user pool must match the app client. These examples use commercial AWS endpoints.

Sample cache/input files contain secrets. Use an access-controlled directory outside web content and source control, separate each local user/account, and serialize operations that modify the same cache. In a real application obtain passwords and codes through private UI instead of leaving them in files. Cache writes retain a .bak copy; protect and remove obsolete backups and challenge files after the flow. Production storage should provide atomic replacement and OS-backed secret protection. Never restore an old refresh token automatically after rotation. These examples do not require a hosted web server.

Checks transport completion, HTTP status, and required response fields before using results. Cognito often reports throttling, invalid codes, and authentication failures as HTTP 400. Present generic sign-in/recovery errors to users; keep detailed diagnostics private. Retry throttled reads with bounded backoff, but reconcile uncertain writes and refresh rotations before repeating them.

AWS documentation 1 | AWS documentation 2 | AWS documentation 3

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
    --  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    DECLARE @app int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @app OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

        PRINT 'Could not load Cognito app settings.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'region'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

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

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

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

        PRINT 'Missing or invalid account_key.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    DECLARE @clientId nvarchar(4000)
    EXEC sp_OAMethod @app, 'StringOf', @clientId OUT, 'client_id'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://cognito-idp.'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com/'
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'FollowRedirects', 0
    EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
    EXEC sp_OASetProperty @http, 'ReadTimeout', 60
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    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
    --  Run one step per invocation; collect a real code from the recipient before "confirm".
    DECLARE @action nvarchar(4000)
    SELECT @action = 'signup'
    IF (@action <> 'signup') and (@action <> 'confirm') and (@action <> 'resend')
      BEGIN

        PRINT 'Choose signup, confirm, or resend.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    DECLARE @input int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @input OUT

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

        PRINT 'Could not load qa_data/cognito/signup-input.json.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @input
        RETURN
      END
    EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'username'
    EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'username'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid username.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @input
        RETURN
      END
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ClientId', @clientId
    EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'username'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Username', @sTmp0
    IF @action = 'signup'
      BEGIN
        EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'password'
        EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'password'
        IF (@iTmp0 <> 1) or (@sTmp0 = '')
          BEGIN

            PRINT 'Missing or invalid password.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'email'
        EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'email'
        IF (@iTmp0 <> 1) or (@sTmp0 = '')
          BEGIN

            PRINT 'Missing or invalid email.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'password'
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Password', @sTmp0
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserAttributes[0].Name', 'email'
        EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'email'
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserAttributes[0].Value', @sTmp0
        --  Add any additional attributes required by your pool before sending.
        EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.SignUp'
        EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @resp
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'HTTP status: ' + @iTmp0
            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
            IF @success = 1
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                PRINT 'Error type: ' + @sTmp0

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
                PRINT 'OAuth error: ' + @sTmp0
              END
            --  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        IF @success <> 1
          BEGIN

            PRINT 'The HTTP exchange did not complete.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
        IF @success <> 1
          BEGIN

            PRINT 'Expected a JSON response.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'UserSub'
        EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'UserSub'
        IF (@iTmp0 <> 1) or (@sTmp0 = '')
          BEGIN

            PRINT 'Missing or invalid UserSub.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END

        EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'UserSub'
        PRINT 'User sub: ' + @sTmp0

        EXEC sp_OAMethod @result, 'BoolOf', @iTmp0 OUT, 'UserConfirmed'
        PRINT 'Confirmed: ' + @iTmp0

        EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'CodeDeliveryDetails.DeliveryMedium'
        PRINT 'Delivery medium: ' + @sTmp0
      END
    IF @action = 'confirm'
      BEGIN
        EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'code'
        EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'code'
        IF (@iTmp0 <> 1) or (@sTmp0 = '')
          BEGIN

            PRINT 'Missing or invalid code.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'code'
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ConfirmationCode', @sTmp0
        EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'ForceAliasCreation', 0
        EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.ConfirmSignUp'
        EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @resp
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'HTTP status: ' + @iTmp0
            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
            IF @success = 1
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                PRINT 'Error type: ' + @sTmp0

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
                PRINT 'OAuth error: ' + @sTmp0
              END
            --  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        IF @success <> 1
          BEGIN

            PRINT 'The HTTP exchange did not complete.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END

        PRINT 'Registration confirmed. The user can now sign in.'
      END
    IF @action = 'resend'
      BEGIN
        EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.ResendConfirmationCode'
        EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @resp
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'HTTP status: ' + @iTmp0
            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
            IF @success = 1
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                PRINT 'Error type: ' + @sTmp0

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
                PRINT 'OAuth error: ' + @sTmp0
              END
            --  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        IF @success <> 1
          BEGIN

            PRINT 'The HTTP exchange did not complete.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
        IF @success <> 1
          BEGIN

            PRINT 'Expected a JSON response.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @input
            RETURN
          END

        PRINT 'Confirmation code requested; use the most recent code.'
      END

    EXEC @hr = sp_OADestroy @app
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @input


END
GO