Skip to main content

Crate enablebanking

Crate enablebanking 

Source
Expand description

§enablebanking

CI codecov crates.io docs.rs License: MIT

Async Rust client for the Enable Banking API, generated from its OpenAPI description. Unofficial.

use enablebanking::types::{Access, Aspsp, AuthorizeSessionRequest, StartAuthorizationRequest};
use enablebanking::{API_URL, Client, Signer};

// The application id and the private key of the certificate uploaded when registering the application.
let key = std::fs::read("tests/fixtures/test-key.pem")?;
let client = Client::new(API_URL, Signer::new(APPLICATION_ID, &key)?);

let url = link(&client).await?;
println!("approve access at {url}");
// The bank then sends the user to the redirect URL with `?code=…`.
transactions(&client, code).await?;

/// Sends the user to their bank to grant 90 days of access.
async fn link(client: &Client) -> Result<String, Box<dyn std::error::Error>> {
    let valid_until = jiff::Timestamp::now() + jiff::SignedDuration::from_hours(90 * 24);
    let started = client
        .start_authorization()
        .body(
            StartAuthorizationRequest::builder()
                .access(Access::builder().valid_until(valid_until))
                .aspsp(Aspsp::builder().name("CrediFriuli").country("IT"))
                .redirect_url("https://example.com/callback")
                .state("any opaque value"),
        )
        .send()
        .await?;
    Ok(started.url.to_string())
}

/// Reads every account the user granted, after the bank redirected back with `code`.
async fn transactions(client: &Client, code: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = client
        .authorize_session()
        .body(AuthorizeSessionRequest::builder().code(code))
        .send()
        .await?;
    for account in session.accounts.iter().filter_map(|account| account.uid) {
        for transaction in client.all_transactions(account, |request| request).await? {
            println!(
                "{:?} {} {:?}",
                transaction.booking_date,
                transaction.transaction_amount,
                transaction.remittance_information
            );
        }
    }
    Ok(())
}

Every endpoint is a method named after the API, with optional parameters as builder calls. Amounts are rust_decimal::Decimal, dates are jiff types, and enumerations keep codes the description does not list as Unknown. Client::new gives up on a request after 15 seconds, so slower banks need a reqwest::Client of your own through Client::new_with_client.

Weekly jobs regenerate the client from Enable Banking’s description and check sandbox responses, accepting null for optional fields. Until 1.0, a release that follows a changed description is a new minor version. The release workflow publishes v* tags to crates.io and GitHub using Trusted Publishing (release.yml, environment crates-io), and accepts existing tags through manual dispatch.

Re-exports§

pub use auth::SignError;
pub use auth::Signer;

Modules§

auth
Signing the JSON Web Tokens that authenticate an application to the API.
builder
Types for composing operation parameters.
types
Types used as operation parameters and responses.

Structs§

Amount
A monetary amount in one currency, such as a balance or a transaction amount.
Client
Client for API reference
ResponseValue
Typed value returned by generated client methods.

Enums§

Error
Error produced by generated client methods.

Constants§

API_URL
Base URL of the API, shared by sandbox and production applications.

Traits§

ClientInfo
Interface for which an implementation is generated for all clients.