Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
#  App settings are trusted configuration. Keep a separate account_key/cache per local user.
set app [new_CkJsonObject]

CkJsonObject_put_Strict $app 1
set success [CkJsonObject_LoadFile $app "qa_data/cognito/app.json"]
if {$success != 1} then {
    puts "Could not load Cognito app settings."
    delete_CkJsonObject $app
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $app "region"] != 1]  ||  [[CkJsonObject_stringOf $app "region"] == ""]} then {
    puts "Missing or invalid region."
    delete_CkJsonObject $app
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $app "user_pool_id"] != 1]  ||  [[CkJsonObject_stringOf $app "user_pool_id"] == ""]} then {
    puts "Missing or invalid user_pool_id."
    delete_CkJsonObject $app
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $app "client_id"] != 1]  ||  [[CkJsonObject_stringOf $app "client_id"] == ""]} then {
    puts "Missing or invalid client_id."
    delete_CkJsonObject $app
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $app "account_key"] != 1]  ||  [[CkJsonObject_stringOf $app "account_key"] == ""]} then {
    puts "Missing or invalid account_key."
    delete_CkJsonObject $app
    exit
}

set clientId [CkJsonObject_stringOf $app "client_id"]
set endpoint [new_CkStringBuilder]

CkStringBuilder_Append $endpoint "https://cognito-idp."
CkStringBuilder_Append $endpoint [CkJsonObject_stringOf $app "region"]
CkStringBuilder_Append $endpoint ".amazonaws.com/"
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 20
CkHttp_put_ReadTimeout $http 60
set resp [new_CkHttpResponse]

set body [new_CkJsonObject]

set result [new_CkJsonObject]

CkJsonObject_put_Strict $result 1
#  Run one step per invocation; collect a real code from the recipient before "confirm".
set action "signup"
if {expr [$action != "signup"]  &&  [expr [$action != "confirm"]  &&  [$action != "resend"]]} then {
    puts "Choose signup, confirm, or resend."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    delete_CkJsonObject $result
    exit
}

set input [new_CkJsonObject]

CkJsonObject_put_Strict $input 1
set success [CkJsonObject_LoadFile $input "qa_data/cognito/signup-input.json"]
if {$success != 1} then {
    puts "Could not load qa_data/cognito/signup-input.json."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    delete_CkJsonObject $result
    delete_CkJsonObject $input
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $input "username"] != 1]  ||  [[CkJsonObject_stringOf $input "username"] == ""]} then {
    puts "Missing or invalid username."
    delete_CkJsonObject $app
    delete_CkStringBuilder $endpoint
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $body
    delete_CkJsonObject $result
    delete_CkJsonObject $input
    exit
}

CkJsonObject_UpdateString $body "ClientId" $clientId
CkJsonObject_UpdateString $body "Username" [CkJsonObject_stringOf $input "username"]
if {$action == "signup"} then {
    if {expr [[CkJsonObject_JsonTypeOf $input "password"] != 1]  ||  [[CkJsonObject_stringOf $input "password"] == ""]} then {
        puts "Missing or invalid password."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    if {expr [[CkJsonObject_JsonTypeOf $input "email"] != 1]  ||  [[CkJsonObject_stringOf $input "email"] == ""]} then {
        puts "Missing or invalid email."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    CkJsonObject_UpdateString $body "Password" [CkJsonObject_stringOf $input "password"]
    CkJsonObject_UpdateString $body "UserAttributes[0].Name" "email"
    CkJsonObject_UpdateString $body "UserAttributes[0].Value" [CkJsonObject_stringOf $input "email"]
    #  Add any additional attributes required by your pool before sending.
    CkHttp_SetRequestHeader $http "X-Amz-Target" "AWSCognitoIdentityProviderService.SignUp"
    set success [CkHttp_HttpJson $http "POST" [CkStringBuilder_getAsString $endpoint] $body "application/x-amz-json-1.0" $resp]
    if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
        puts "HTTP status: [CkHttpResponse_get_StatusCode $resp]"
        set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
        if {$success == 1} then {
            puts "Error type: [CkJsonObject_stringOf $result __type]"
            puts "OAuth error: [CkJsonObject_stringOf $result error]"
        }

        #  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    if {$success != 1} then {
        puts "The HTTP exchange did not complete."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
    if {$success != 1} then {
        puts "Expected a JSON response."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    if {expr [[CkJsonObject_JsonTypeOf $result "UserSub"] != 1]  ||  [[CkJsonObject_stringOf $result "UserSub"] == ""]} then {
        puts "Missing or invalid UserSub."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    puts "User sub: [CkJsonObject_stringOf $result UserSub]"
    puts "Confirmed: [CkJsonObject_BoolOf $result UserConfirmed]"
    puts "Delivery medium: [CkJsonObject_stringOf $result CodeDeliveryDetails.DeliveryMedium]"
}

if {$action == "confirm"} then {
    if {expr [[CkJsonObject_JsonTypeOf $input "code"] != 1]  ||  [[CkJsonObject_stringOf $input "code"] == ""]} then {
        puts "Missing or invalid code."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    CkJsonObject_UpdateString $body "ConfirmationCode" [CkJsonObject_stringOf $input "code"]
    CkJsonObject_UpdateBool $body "ForceAliasCreation" 0
    CkHttp_SetRequestHeader $http "X-Amz-Target" "AWSCognitoIdentityProviderService.ConfirmSignUp"
    set success [CkHttp_HttpJson $http "POST" [CkStringBuilder_getAsString $endpoint] $body "application/x-amz-json-1.0" $resp]
    if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
        puts "HTTP status: [CkHttpResponse_get_StatusCode $resp]"
        set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
        if {$success == 1} then {
            puts "Error type: [CkJsonObject_stringOf $result __type]"
            puts "OAuth error: [CkJsonObject_stringOf $result error]"
        }

        #  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    if {$success != 1} then {
        puts "The HTTP exchange did not complete."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    puts "Registration confirmed. The user can now sign in."
}

if {$action == "resend"} then {
    CkHttp_SetRequestHeader $http "X-Amz-Target" "AWSCognitoIdentityProviderService.ResendConfirmationCode"
    set success [CkHttp_HttpJson $http "POST" [CkStringBuilder_getAsString $endpoint] $body "application/x-amz-json-1.0" $resp]
    if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
        puts "HTTP status: [CkHttpResponse_get_StatusCode $resp]"
        set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
        if {$success == 1} then {
            puts "Error type: [CkJsonObject_stringOf $result __type]"
            puts "OAuth error: [CkJsonObject_stringOf $result error]"
        }

        #  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    if {$success != 1} then {
        puts "The HTTP exchange did not complete."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
    if {$success != 1} then {
        puts "Expected a JSON response."
        delete_CkJsonObject $app
        delete_CkStringBuilder $endpoint
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $body
        delete_CkJsonObject $result
        delete_CkJsonObject $input
        exit
    }

    puts "Confirmation code requested; use the most recent code."
}


delete_CkJsonObject $app
delete_CkStringBuilder $endpoint
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $body
delete_CkJsonObject $result
delete_CkJsonObject $input