Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Ibanity XS2A List Financial Institutions

See more Ibanity Examples

Demonstrates how to send a request to get a list of financial institutions.

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.

    //  Send the following request:

    // $ curl -X GET https://api.ibanity.com/xs2a/financial-institutions \
    // --cert certificate.pem \
    // --key private_key.pem \
    // -H 'Signature: keyId="75b5d796-de5c-400a-81ce-e72371b01cbc",created=1599659223,algorithm="hs2019",headers="(request-target) digest (created) host",signature="BASE64(RSA-SHA256(SIGNING_STRING))"' \
    // -H 'Digest: SHA-512=beDaRguyEb8fhh5wnl37bOTDtvhuYZyZNkTZ9LiC9Wc='

    // Ibanity provides the certificate + private key in PFX format.  This example will use the .pfx instead of the pair of PEM files.
    // (It is also possible to implement using Chilkat with the PEM files, but PFX is easier.)
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("qa_data/pfx/my_ibanity_certificate.pfx", "my_pfx_password") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // We need to calculate the Digest and Signature header fields.
    // For a detailed explanation, see Calculate Ibanity HTTP Signature Example

    // We'll just write the code here as briefly as possible.

    const dt_now = try chilkat.DateTime.init();
    defer dt_now.deinit();
    dt_now.setFromCurrentSystemTime() catch {};
    const created = try dt_now.getAsUnixTimeStr(alloc, false);

    const crypt2 = try chilkat.Crypt2.init();
    defer crypt2.deinit();
    crypt2.setHashAlgorithm("sha512");
    crypt2.setEncodingMode("base64");

    const sb_digest_hdr_value = try chilkat.StringBuilder.init();
    defer sb_digest_hdr_value.deinit();
    sb_digest_hdr_value.append("SHA-512=") catch {};
    // GET requests have empty payloads.  The SHA-512 hash of the empty string is the same for all GET requests.
    // Therefore all GET requests will use "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg=="
    // You can eliminate the explicit hash computation (for GET requests) and simply use the above literal string.
    sb_digest_hdr_value.append(try crypt2.hashStringENC(alloc, "")) catch {};

    std.debug.print("Generated Digest\n", .{});
    std.debug.print("{s}\n", .{try sb_digest_hdr_value.getAsString(alloc)});

    const request_target = "get /xs2a/financial-institutions";

    const sb_signing_string = try chilkat.StringBuilder.init();
    defer sb_signing_string.deinit();
    sb_signing_string.append("(request-target): ") catch {};
    sb_signing_string.appendLine(request_target, false) catch {};
    sb_signing_string.append("host: ") catch {};
    sb_signing_string.appendLine("api.ibanity.com", false) catch {};
    sb_signing_string.append("digest: ") catch {};
    sb_signing_string.appendLine(try sb_digest_hdr_value.getAsString(alloc), false) catch {};
    sb_signing_string.append("(created): ") catch {};
    sb_signing_string.append(created) catch {};
    // ibanity-idempotency-key is not used with GET requests.

    std.debug.print("Signing String:\n", .{});
    std.debug.print("{s}\n", .{try sb_signing_string.getAsString(alloc)});

    const signed_headers_list = "(request-target) host digest (created)";

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadEncryptedPemFile("my_ibanity_signature_private_key.pem", "pem_password") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();
    rsa.setPssSaltLen(32);
    rsa.setEncodingMode("base64");
    // Use the RSASSA-PSS signature algorithm
    rsa.setPkcsPadding(false);

    rsa.usePrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Sign the signing string.
    const sig_base64 = rsa.signStringENC(alloc, try sb_signing_string.getAsString(alloc), "sha-256") catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Signature:\n", .{});
    std.debug.print("{s}\n", .{sig_base64});

    // Build the signature header value.
    const sb_sig_header_value = try chilkat.StringBuilder.init();
    defer sb_sig_header_value.deinit();
    sb_sig_header_value.append("keyId=\"") catch {};
    // Use your identifier for the application's signature certificate, obtained from the Developer Portal
    sb_sig_header_value.append("a0ce296d-84c8-4bd5-8eb4-de0339950cfa") catch {};
    sb_sig_header_value.append("\",created=") catch {};
    sb_sig_header_value.append(created) catch {};
    sb_sig_header_value.append(",algorithm=\"hs2019\",headers=\"") catch {};
    sb_sig_header_value.append(signed_headers_list) catch {};
    sb_sig_header_value.append("\",signature=\"") catch {};
    sb_sig_header_value.append(sig_base64) catch {};
    sb_sig_header_value.append("\"") catch {};

    // Send the GET request..
    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setSslClientCert(cert) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    http.setRequestHeader("Signature", try sb_sig_header_value.getAsString(alloc));
    http.setRequestHeader("Digest", try sb_digest_hdr_value.getAsString(alloc));

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.ibanity.com/xs2a/financial-institutions", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

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

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

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

    // {
    //   "data": [
    //     {
    //       "attributes": {
    //         "authorizationModels": [
    //           "detailed",
    //           "financialInstitutionOffered"
    //         ],
    //         "bic": "NBBEBEBB203",
    //         "bulkPaymentsEnabled": true,
    //         "bulkPaymentsProductTypes": [
    //           "sepaCreditTransfer"
    //         ],
    //         "country": null,
    //         "financialInstitutionCustomerReferenceRequired": false,
    //         "futureDatedPaymentsAllowed": true,
    //         "logoUrl": "https://s3.eu-central-1.amazonaws.com/ibanity-production-financial-institution-assets/sandbox.png",
    //         "maintenanceFrom": null,
    //         "maintenanceTo": null,
    //         "maintenanceType": null,
    //         "maxRequestedAccountReferences": null,
    //         "minRequestedAccountReferences": 0,
    //         "name": "Bogus Financial",
    //         "paymentsEnabled": true,
    //         "paymentsProductTypes": [
    //           "sepaCreditTransfer"
    //         ],
    //         "periodicPaymentsEnabled": true,
    //         "periodicPaymentsProductTypes": [
    //           "sepaCreditTransfer"
    //         ],
    //         "primaryColor": "#7d39ff",
    //         "requiresCredentialStorage": false,
    //         "requiresCustomerIpAddress": false,
    //         "sandbox": true,
    //         "secondaryColor": "#3DF2C2",
    //         "sharedBrandName": null,
    //         "sharedBrandReference": null,
    //         "status": "beta"
    //       },
    //       "id": "2d3d70a4-cb3c-477c-97e1-cbe495b82841",
    //       "links": {
    //         "self": "https://api.ibanity.com/xs2a/financial-institutions/2d3d70a4-cb3c-477c-97e1-cbe495b82841"
    //       },
    //       "type": "financialInstitution"
    //     },
    //     {
    //       "attributes": {
    //         "authorizationModels": [
    //           "detailed",
    //           "financialInstitutionOffered"
    //         ],
    //         "bic": "NBBEBEBB203",
    //         "bulkPaymentsEnabled": true,
    //         "bulkPaymentsProductTypes": [
    //           "sepaCreditTransfer"
    //         ],
    //         "country": null,
    //         "financialInstitutionCustomerReferenceRequired": false,
    //         "futureDatedPaymentsAllowed": true,
    //         "logoUrl": "https://s3.eu-central-1.amazonaws.com/ibanity-production-financial-institution-assets/sandbox.png",
    //         "maintenanceFrom": null,
    //         "maintenanceTo": null,
    //         "maintenanceType": null,
    //         "maxRequestedAccountReferences": null,
    //         "minRequestedAccountReferences": 0,
    //         "name": "XYZ Trust",
    //         "paymentsEnabled": true,
    //         "paymentsProductTypes": [
    //           "sepaCreditTransfer"
    //         ],
    //         "periodicPaymentsEnabled": true,
    //         "periodicPaymentsProductTypes": [
    //           "sepaCreditTransfer"
    //         ],
    //         "primaryColor": "#7d39ff",
    //         "requiresCredentialStorage": false,
    //         "requiresCustomerIpAddress": false,
    //         "sandbox": true,
    //         "secondaryColor": "#3DF2C2",
    //         "sharedBrandName": null,
    //         "sharedBrandReference": null,
    //         "status": "beta"
    //       },
    //       "id": "d4100f28-936b-4379-a3f8-86314a2014fb",
    //       "links": {
    //         "self": "https://api.ibanity.com/xs2a/financial-institutions/d4100f28-936b-4379-a3f8-86314a2014fb"
    //       },
    //       "type": "financialInstitution"
    //     }
    //   ],
    //   "links": {
    //     "first": "https://api.ibanity.com/xs2a/financial-institutions"
    //   },
    //   "meta": {
    //     "paging": {
    //       "limit": 10
    //     }
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var attributes_bic: [:0]const u8 = "";
    var attributes_bulk_payments_enabled: bool = false;
    var attributes_country: [:0]const u8 = "";
    var attributes_financial_institution_customer_reference_required: bool = false;
    var attributes_future_dated_payments_allowed: bool = false;
    var attributes_logo_url: [:0]const u8 = "";
    var attributes_maintenance_from: [:0]const u8 = "";
    var attributes_maintenance_to: [:0]const u8 = "";
    var attributes_maintenance_type: [:0]const u8 = "";
    var attributes_max_requested_account_references: [:0]const u8 = "";
    var attributes_min_requested_account_references: i32 = 0;
    var attributes_name: [:0]const u8 = "";
    var attributes_payments_enabled: bool = false;
    var attributes_periodic_payments_enabled: bool = false;
    var attributes_primary_color: [:0]const u8 = "";
    var attributes_requires_credential_storage: bool = false;
    var attributes_requires_customer_ip_address: bool = false;
    var attributes_sandbox: bool = false;
    var attributes_secondary_color: [:0]const u8 = "";
    var attributes_shared_brand_name: [:0]const u8 = "";
    var attributes_shared_brand_reference: [:0]const u8 = "";
    var attributes_status: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var links_self: [:0]const u8 = "";
    var v_type: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var str_val: [:0]const u8 = "";

    _ = try j_resp.stringOf(alloc, "links.first");
    _ = j_resp.intOf("meta.paging.limit");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("data");
    while (i < count_i) {
        j_resp.setI(i);
        attributes_bic = try j_resp.stringOf(alloc, "data[i].attributes.bic");
        attributes_bulk_payments_enabled = j_resp.boolOf("data[i].attributes.bulkPaymentsEnabled");
        attributes_country = try j_resp.stringOf(alloc, "data[i].attributes.country");
        attributes_financial_institution_customer_reference_required = j_resp.boolOf("data[i].attributes.financialInstitutionCustomerReferenceRequired");
        attributes_future_dated_payments_allowed = j_resp.boolOf("data[i].attributes.futureDatedPaymentsAllowed");
        attributes_logo_url = try j_resp.stringOf(alloc, "data[i].attributes.logoUrl");
        attributes_maintenance_from = try j_resp.stringOf(alloc, "data[i].attributes.maintenanceFrom");
        attributes_maintenance_to = try j_resp.stringOf(alloc, "data[i].attributes.maintenanceTo");
        attributes_maintenance_type = try j_resp.stringOf(alloc, "data[i].attributes.maintenanceType");
        attributes_max_requested_account_references = try j_resp.stringOf(alloc, "data[i].attributes.maxRequestedAccountReferences");
        attributes_min_requested_account_references = j_resp.intOf("data[i].attributes.minRequestedAccountReferences");
        attributes_name = try j_resp.stringOf(alloc, "data[i].attributes.name");
        attributes_payments_enabled = j_resp.boolOf("data[i].attributes.paymentsEnabled");
        attributes_periodic_payments_enabled = j_resp.boolOf("data[i].attributes.periodicPaymentsEnabled");
        attributes_primary_color = try j_resp.stringOf(alloc, "data[i].attributes.primaryColor");
        attributes_requires_credential_storage = j_resp.boolOf("data[i].attributes.requiresCredentialStorage");
        attributes_requires_customer_ip_address = j_resp.boolOf("data[i].attributes.requiresCustomerIpAddress");
        attributes_sandbox = j_resp.boolOf("data[i].attributes.sandbox");
        attributes_secondary_color = try j_resp.stringOf(alloc, "data[i].attributes.secondaryColor");
        attributes_shared_brand_name = try j_resp.stringOf(alloc, "data[i].attributes.sharedBrandName");
        attributes_shared_brand_reference = try j_resp.stringOf(alloc, "data[i].attributes.sharedBrandReference");
        attributes_status = try j_resp.stringOf(alloc, "data[i].attributes.status");
        id = try j_resp.stringOf(alloc, "data[i].id");
        links_self = try j_resp.stringOf(alloc, "data[i].links.self");
        v_type = try j_resp.stringOf(alloc, "data[i].type");
        j = 0;
        count_j = j_resp.sizeOfArray("data[i].attributes.authorizationModels");
        while (j < count_j) {
            j_resp.setJ(j);
            str_val = try j_resp.stringOf(alloc, "data[i].attributes.authorizationModels[j]");
            j = j + 1;
        }

        j = 0;
        count_j = j_resp.sizeOfArray("data[i].attributes.bulkPaymentsProductTypes");
        while (j < count_j) {
            j_resp.setJ(j);
            str_val = try j_resp.stringOf(alloc, "data[i].attributes.bulkPaymentsProductTypes[j]");
            j = j + 1;
        }

        j = 0;
        count_j = j_resp.sizeOfArray("data[i].attributes.paymentsProductTypes");
        while (j < count_j) {
            j_resp.setJ(j);
            str_val = try j_resp.stringOf(alloc, "data[i].attributes.paymentsProductTypes[j]");
            j = j + 1;
        }

        j = 0;
        count_j = j_resp.sizeOfArray("data[i].attributes.periodicPaymentsProductTypes");
        while (j < count_j) {
            j_resp.setJ(j);
            str_val = try j_resp.stringOf(alloc, "data[i].attributes.periodicPaymentsProductTypes[j]");
            j = j + 1;
        }

        i = i + 1;
    }
}