Sample code for 30+ languages & platforms
Zig

Amazon Cognito - Describe User Pool

See more Amazon Cognito Examples

Returns the configuration information and metadata of the specified user pool.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("cognito-idp");
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(auth_aws) catch {};

    // URL: https://cognito-idp.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    // Use the same region as specified above.
    rest.connect("cognito-idp.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("ConnectFailReason: {d}\n", .{rest.getConnectFailReason()});
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Note: The above code does not need to be repeatedly called for each REST request.
    // The rest object can be setup once, and then many requests can be sent.  Chilkat will automatically
    // reconnect within a FullRequest* method as needed.  It is only the very first connection that is explicitly
    // made via the Connect method.

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("UserPoolId", "us-west-2_yt6WzO3SA") catch {};

    rest.addHeader("Content-Type", "application/x-amz-json-1.0") catch {};
    rest.addHeader("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPool") catch {};
    rest.addHeader("Accept-Encoding", "identity") catch {};

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    json.emitSb(sb_request_body) catch {};
    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestSb("POST", "/", sb_request_body, sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const resp_status_code = rest.getResponseStatusCode();
    std.debug.print("response status code = {d}\n", .{resp_status_code});
    if (resp_status_code != 200) {
        std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Response Body:\n", .{});
        std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
        return;
    }

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.loadSb(sb_response_body) catch {};

    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "UserPool": {
    //     "AccountRecoverySetting": {
    //       "RecoveryMechanisms": [
    //         {
    //           "Name": "verified_phone_number",
    //           "Priority": 1
    //         },
    //         {
    //           "Name": "verified_email",
    //           "Priority": 2
    //         }
    //       ]
    //     },
    //     "AdminCreateUserConfig": {
    //       "AllowAdminCreateUserOnly": false,
    //       "InviteMessageTemplate": {
    //         "EmailMessage": "Your username is {username} and temporary password is {####}. ",
    //         "EmailSubject": "Your temporary password",
    //         "SMSMessage": "Your username is {username} and temporary password is {####}. "
    //       },
    //       "UnusedAccountValidityDays": 7
    //     },
    //     "Arn": "arn:aws:cognito-idp:us-west-2:954491834127:userpool/us-west-2_yt6WzO3SA",
    //     "AutoVerifiedAttributes": [
    //       "email",
    //       "phone_number"
    //     ],
    //     "CreationDate": 1.636388909817E9,
    //     "EmailConfiguration": {
    //       "EmailSendingAccount": "DEVELOPER",
    //       "From": "admin@chilkatsoft.com",
    //       "ReplyToEmailAddress": "admin@chilkatsoft.com",
    //       "SourceArn": "arn:aws:ses:us-west-2:954491834127:identity/admin@chilkatsoft.com"
    //     },
    //     "EmailVerificationMessage": "Your verification code is {####}. ",
    //     "EmailVerificationSubject": "Your verification code",
    //     "EstimatedNumberOfUsers": 0,
    //     "Id": "us-west-2_yt6WzO3SA",
    //     "LambdaConfig": {},
    //     "LastModifiedDate": 1.636388910377E9,
    //     "MfaConfiguration": "ON",
    //     "Name": "chilkat",
    //     "Policies": {
    //       "PasswordPolicy": {
    //         "MinimumLength": 8,
    //         "RequireLowercase": true,
    //         "RequireNumbers": true,
    //         "RequireSymbols": true,
    //         "RequireUppercase": true,
    //         "TemporaryPasswordValidityDays": 7
    //       }
    //     },
    //     "SchemaAttributes": [
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": false,
    //         "Name": "sub",
    //         "Required": true,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "1"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "name",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "given_name",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "family_name",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "middle_name",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "nickname",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "preferred_username",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "profile",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "picture",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "website",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "email",
    //         "Required": true,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "Boolean",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "email_verified",
    //         "Required": false
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "gender",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "birthdate",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "10",
    //           "MinLength": "10"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "zoneinfo",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "locale",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "phone_number",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "Boolean",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "phone_number_verified",
    //         "Required": false
    //       },
    //       {
    //         "AttributeDataType": "String",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "address",
    //         "Required": false,
    //         "StringAttributeConstraints": {
    //           "MaxLength": "2048",
    //           "MinLength": "0"
    //         }
    //       },
    //       {
    //         "AttributeDataType": "Number",
    //         "DeveloperOnlyAttribute": false,
    //         "Mutable": true,
    //         "Name": "updated_at",
    //         "NumberAttributeConstraints": {
    //           "MinValue": "0"
    //         },
    //         "Required": false
    //       }
    //     ],
    //     "SmsAuthenticationMessage": "Your verification code is {####}. ",
    //     "SmsConfiguration": {
    //       "ExternalId": "94b17737-216b-4cf7-a29e-17e990e91300",
    //       "SnsCallerArn": "arn:aws:iam::954491834127:role/service-role/chilkat-SMS-Role"
    //     },
    //     "SmsVerificationMessage": "Your verification code is {####}. ",
    //     "UserPoolTags": {
    //       "sampleUserPoolTag": "test"
    //     },
    //     "UsernameConfiguration": {
    //       "CaseSensitive": false
    //     },
    //     "VerificationMessageTemplate": {
    //       "DefaultEmailOption": "CONFIRM_WITH_CODE",
    //       "EmailMessage": "Your verification code is {####}. ",
    //       "EmailSubject": "Your verification code",
    //       "SmsMessage": "Your verification code is {####}. "
    //     }
    //   }
    // }

    var name: [:0]const u8 = "";
    var priority: i32 = 0;
    var str_val: [:0]const u8 = "";
    var attribute_data_type: [:0]const u8 = "";
    var developer_only_attribute: bool = false;
    var mutable: bool = false;
    var required: bool = false;
    var string_attribute_constraints_max_length: [:0]const u8 = "";
    var string_attribute_constraints_min_length: [:0]const u8 = "";
    var number_attribute_constraints_min_value: [:0]const u8 = "";

    _ = json_response.boolOf("UserPool.AdminCreateUserConfig.AllowAdminCreateUserOnly");
    _ = try json_response.stringOf(alloc, "UserPool.AdminCreateUserConfig.InviteMessageTemplate.EmailMessage");
    _ = try json_response.stringOf(alloc, "UserPool.AdminCreateUserConfig.InviteMessageTemplate.EmailSubject");
    _ = try json_response.stringOf(alloc, "UserPool.AdminCreateUserConfig.InviteMessageTemplate.SMSMessage");
    _ = json_response.intOf("UserPool.AdminCreateUserConfig.UnusedAccountValidityDays");
    _ = try json_response.stringOf(alloc, "UserPool.Arn");
    _ = try json_response.stringOf(alloc, "UserPool.CreationDate");
    _ = try json_response.stringOf(alloc, "UserPool.EmailConfiguration.EmailSendingAccount");
    _ = try json_response.stringOf(alloc, "UserPool.EmailConfiguration.From");
    _ = try json_response.stringOf(alloc, "UserPool.EmailConfiguration.ReplyToEmailAddress");
    _ = try json_response.stringOf(alloc, "UserPool.EmailConfiguration.SourceArn");
    _ = try json_response.stringOf(alloc, "UserPool.EmailVerificationMessage");
    _ = try json_response.stringOf(alloc, "UserPool.EmailVerificationSubject");
    _ = json_response.intOf("UserPool.EstimatedNumberOfUsers");
    _ = try json_response.stringOf(alloc, "UserPool.Id");
    _ = try json_response.stringOf(alloc, "UserPool.LastModifiedDate");
    _ = try json_response.stringOf(alloc, "UserPool.MfaConfiguration");
    _ = try json_response.stringOf(alloc, "UserPool.Name");
    _ = json_response.intOf("UserPool.Policies.PasswordPolicy.MinimumLength");
    _ = json_response.boolOf("UserPool.Policies.PasswordPolicy.RequireLowercase");
    _ = json_response.boolOf("UserPool.Policies.PasswordPolicy.RequireNumbers");
    _ = json_response.boolOf("UserPool.Policies.PasswordPolicy.RequireSymbols");
    _ = json_response.boolOf("UserPool.Policies.PasswordPolicy.RequireUppercase");
    _ = json_response.intOf("UserPool.Policies.PasswordPolicy.TemporaryPasswordValidityDays");
    _ = try json_response.stringOf(alloc, "UserPool.SmsAuthenticationMessage");
    _ = try json_response.stringOf(alloc, "UserPool.SmsConfiguration.ExternalId");
    _ = try json_response.stringOf(alloc, "UserPool.SmsConfiguration.SnsCallerArn");
    _ = try json_response.stringOf(alloc, "UserPool.SmsVerificationMessage");
    _ = try json_response.stringOf(alloc, "UserPool.UserPoolTags.sampleUserPoolTag");
    _ = json_response.boolOf("UserPool.UsernameConfiguration.CaseSensitive");
    _ = try json_response.stringOf(alloc, "UserPool.VerificationMessageTemplate.DefaultEmailOption");
    _ = try json_response.stringOf(alloc, "UserPool.VerificationMessageTemplate.EmailMessage");
    _ = try json_response.stringOf(alloc, "UserPool.VerificationMessageTemplate.EmailSubject");
    _ = try json_response.stringOf(alloc, "UserPool.VerificationMessageTemplate.SmsMessage");

    var i: i32 = 0;
    var count_i: i32 = json_response.sizeOfArray("UserPool.AccountRecoverySetting.RecoveryMechanisms");
    while (i < count_i) {
        json_response.setI(i);
        name = try json_response.stringOf(alloc, "UserPool.AccountRecoverySetting.RecoveryMechanisms[i].Name");
        priority = json_response.intOf("UserPool.AccountRecoverySetting.RecoveryMechanisms[i].Priority");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("UserPool.AutoVerifiedAttributes");
    while (i < count_i) {
        json_response.setI(i);
        str_val = try json_response.stringOf(alloc, "UserPool.AutoVerifiedAttributes[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json_response.sizeOfArray("UserPool.SchemaAttributes");
    while (i < count_i) {
        json_response.setI(i);
        attribute_data_type = try json_response.stringOf(alloc, "UserPool.SchemaAttributes[i].AttributeDataType");
        developer_only_attribute = json_response.boolOf("UserPool.SchemaAttributes[i].DeveloperOnlyAttribute");
        mutable = json_response.boolOf("UserPool.SchemaAttributes[i].Mutable");
        name = try json_response.stringOf(alloc, "UserPool.SchemaAttributes[i].Name");
        required = json_response.boolOf("UserPool.SchemaAttributes[i].Required");
        string_attribute_constraints_max_length = try json_response.stringOf(alloc, "UserPool.SchemaAttributes[i].StringAttributeConstraints.MaxLength");
        string_attribute_constraints_min_length = try json_response.stringOf(alloc, "UserPool.SchemaAttributes[i].StringAttributeConstraints.MinLength");
        number_attribute_constraints_min_value = try json_response.stringOf(alloc, "UserPool.SchemaAttributes[i].NumberAttributeConstraints.MinValue");
        i = i + 1;
    }
}