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

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = 0
    ;  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    app.i = CkJsonObject::ckCreate()
    If app.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(app, 1)
    success = CkJsonObject::ckLoadFile(app,"qa_data/cognito/app.json")
    If success <> 1
        Debug "Could not load Cognito app settings."
        CkJsonObject::ckDispose(app)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(app,"region") <> 1) OR (CkJsonObject::ckStringOf(app,"region") = "")
        Debug "Missing or invalid region."
        CkJsonObject::ckDispose(app)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(app,"user_pool_id") <> 1) OR (CkJsonObject::ckStringOf(app,"user_pool_id") = "")
        Debug "Missing or invalid user_pool_id."
        CkJsonObject::ckDispose(app)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(app,"client_id") <> 1) OR (CkJsonObject::ckStringOf(app,"client_id") = "")
        Debug "Missing or invalid client_id."
        CkJsonObject::ckDispose(app)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(app,"account_key") <> 1) OR (CkJsonObject::ckStringOf(app,"account_key") = "")
        Debug "Missing or invalid account_key."
        CkJsonObject::ckDispose(app)
        ProcedureReturn
    EndIf

    clientId.s = CkJsonObject::ckStringOf(app,"client_id")
    endpoint.i = CkStringBuilder::ckCreate()
    If endpoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(endpoint,"https://cognito-idp.")
    CkStringBuilder::ckAppend(endpoint,CkJsonObject::ckStringOf(app,"region"))
    CkStringBuilder::ckAppend(endpoint,".amazonaws.com/")
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkFollowRedirects(http, 0)
    CkHttp::setCkConnectTimeout(http, 20)
    CkHttp::setCkReadTimeout(http, 60)
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(result, 1)
    ;  Run one step per invocation; collect a real code from the recipient before "confirm".
    action.s = "signup"
    If (action <> "signup") AND (action <> "confirm") AND (action <> "resend")
        Debug "Choose signup, confirm, or resend."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    input.i = CkJsonObject::ckCreate()
    If input.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(input, 1)
    success = CkJsonObject::ckLoadFile(input,"qa_data/cognito/signup-input.json")
    If success <> 1
        Debug "Could not load qa_data/cognito/signup-input.json."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(input,"username") <> 1) OR (CkJsonObject::ckStringOf(input,"username") = "")
        Debug "Missing or invalid username."
        CkJsonObject::ckDispose(app)
        CkStringBuilder::ckDispose(endpoint)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(input)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"ClientId",clientId)
    CkJsonObject::ckUpdateString(body,"Username",CkJsonObject::ckStringOf(input,"username"))
    If action = "signup"
        If (CkJsonObject::ckJsonTypeOf(input,"password") <> 1) OR (CkJsonObject::ckStringOf(input,"password") = "")
            Debug "Missing or invalid password."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(input,"email") <> 1) OR (CkJsonObject::ckStringOf(input,"email") = "")
            Debug "Missing or invalid email."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        CkJsonObject::ckUpdateString(body,"Password",CkJsonObject::ckStringOf(input,"password"))
        CkJsonObject::ckUpdateString(body,"UserAttributes[0].Name","email")
        CkJsonObject::ckUpdateString(body,"UserAttributes[0].Value",CkJsonObject::ckStringOf(input,"email"))
        ;  Add any additional attributes required by your pool before sending.
        CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.SignUp")
        success = CkHttp::ckHttpJson(http,"POST",CkStringBuilder::ckGetAsString(endpoint),body,"application/x-amz-json-1.0",resp)
        If CkHttpResponse::ckStatusCode(resp) <> 200
            Debug "HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
            success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
            If success = 1
                Debug "Error type: " + CkJsonObject::ckStringOf(result,"__type")
                Debug "OAuth error: " + CkJsonObject::ckStringOf(result,"error")
            EndIf

            ;  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        If success <> 1
            Debug "The HTTP exchange did not complete."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
        If success <> 1
            Debug "Expected a JSON response."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        If (CkJsonObject::ckJsonTypeOf(result,"UserSub") <> 1) OR (CkJsonObject::ckStringOf(result,"UserSub") = "")
            Debug "Missing or invalid UserSub."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        Debug "User sub: " + CkJsonObject::ckStringOf(result,"UserSub")
        Debug "Confirmed: " + Str(CkJsonObject::ckBoolOf(result,"UserConfirmed"))
        Debug "Delivery medium: " + CkJsonObject::ckStringOf(result,"CodeDeliveryDetails.DeliveryMedium")
    EndIf

    If action = "confirm"
        If (CkJsonObject::ckJsonTypeOf(input,"code") <> 1) OR (CkJsonObject::ckStringOf(input,"code") = "")
            Debug "Missing or invalid code."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        CkJsonObject::ckUpdateString(body,"ConfirmationCode",CkJsonObject::ckStringOf(input,"code"))
        CkJsonObject::ckUpdateBool(body,"ForceAliasCreation",0)
        CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.ConfirmSignUp")
        success = CkHttp::ckHttpJson(http,"POST",CkStringBuilder::ckGetAsString(endpoint),body,"application/x-amz-json-1.0",resp)
        If CkHttpResponse::ckStatusCode(resp) <> 200
            Debug "HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
            success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
            If success = 1
                Debug "Error type: " + CkJsonObject::ckStringOf(result,"__type")
                Debug "OAuth error: " + CkJsonObject::ckStringOf(result,"error")
            EndIf

            ;  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        If success <> 1
            Debug "The HTTP exchange did not complete."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        Debug "Registration confirmed. The user can now sign in."
    EndIf

    If action = "resend"
        CkHttp::ckSetRequestHeader(http,"X-Amz-Target","AWSCognitoIdentityProviderService.ResendConfirmationCode")
        success = CkHttp::ckHttpJson(http,"POST",CkStringBuilder::ckGetAsString(endpoint),body,"application/x-amz-json-1.0",resp)
        If CkHttpResponse::ckStatusCode(resp) <> 200
            Debug "HTTP status: " + Str(CkHttpResponse::ckStatusCode(resp))
            success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
            If success = 1
                Debug "Error type: " + CkJsonObject::ckStringOf(result,"__type")
                Debug "OAuth error: " + CkJsonObject::ckStringOf(result,"error")
            EndIf

            ;  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        If success <> 1
            Debug "The HTTP exchange did not complete."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        success = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
        If success <> 1
            Debug "Expected a JSON response."
            CkJsonObject::ckDispose(app)
            CkStringBuilder::ckDispose(endpoint)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkJsonObject::ckDispose(input)
            ProcedureReturn
        EndIf

        Debug "Confirmation code requested; use the most recent code."
    EndIf



    CkJsonObject::ckDispose(app)
    CkStringBuilder::ckDispose(endpoint)
    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(body)
    CkJsonObject::ckDispose(result)
    CkJsonObject::ckDispose(input)


    ProcedureReturn
EndProcedure