Plaid Ruby SDK

repository·master·Indexed 42 days ago

https://github.com/plaid/plaid-ruby

Official Ruby bindings for the Plaid API, generated from the Plaid OpenAPI schema. Provides a type-safe way to integrate financial services, featuring support for Ruby 3.2.0+, Faraday for HTTPS connections, and comprehensive models for requests, responses, and webhooks. Includes tools for managing link_tokens, exchanging public_tokens for access_tokens, and retrieving paginated transactions via TransactionsSync.

Tokens
11.2K
Snippets
42
Records
51
Agent score
54%

What's inside Plaid

  1. Use Ruby Date and DateTime objects for API requests

    master

    In this version of the client, date and datetime fields must be passed as Ruby Date or DateTime objects rather than strings.

    For fields specifying format: date, use Date objects (e.g., Date.new(2022, 5, 5) or Date.parse('2022-05-05')).

    For fields specifying format: date-time, use DateTime objects with time zone information (e.g., Time.parse("2022-05-06T22:35:49Z").to_datetime). Failing to include time zone information or passing a string will result in an error.

    require 'date'
    require 'time'
    
    # For format: date
    date_val = Date.new(2022, 5, 5)
    
    # For format: date-time
    datetime_val = Time.parse("2022-05-06T22:35:49Z").to_datetime
  2. Access response object data

    master
    All API responses are returned as objects that must be accessed using dot notation (e.g., response.field_name). Bracket notation (e.g., response['field_name']) is not supported. Attempting to access a key that does not exist in the response will raise a NoMethodError.
  3. Install the plaid gem

    master

    Add gem 'plaid' to your application's Gemfile and run bundle, or install it directly via the command line. Note that this gem requires Ruby 3.2.0 or higher.

    ```ruby
    # In Gemfile
    gem 'plaid'

    Or via CLI

    $ gem install plaid

  4. Tune Faraday connection settings

    master

    The gem uses Faraday for HTTPS connections. You can tune the connection timeout via api_client.connection.options[:timeout] or provide custom middleware by using api_client.create_connection.

    # Set a 20-minute timeout
    api_client.connection.options[:timeout] = 60*20
    
    # Use custom middleware
    api_client.create_connection do |builder|
      builder.use Faraday::Response::Logger
    end
  5. Migrate User and Data methods from 1.x to 2.0.0

    master

    When upgrading from 1.x to 2.0.0, several core methods for user and data management have been renamed or restructured. Pay close attention to changes in parameter order and lists.

    # User Management
    # Use Plaid::User.create instead of Plaid.add_user (parameter order changed)
    # Use Plaid::User.load instead of Plaid.set_user (parameter order changed)
    # Use Plaid::User.exchange_token instead of Plaid.exchange_token (parameter list changed)
    # Use Plaid::User.delete instead of Plaid::User.delete_user
    
    # Data Access
    # Use Plaid::User.create or (.load) and Plaid::User#transactions instead of Plaid.transactions
    # Use Plaid::Institution.all and Plaid::Institution.get instead of Plaid.institution
    # Use Plaid::Category.all and Plaid::Category.get instead of Plaid.category
  6. Migrate from 1.x to 2.0.0

    master

    Upgrading to version 2.0.0 requires Ruby 2.0 or higher. This version introduced significant breaking changes to configuration, user management, and data models. Note that version 2.0.0 and later consistently use Symbols instead of Strings for product names and hash keys.

    # Update the Plaid.config block
    Plaid.config do |p|
      p.client_id = '<<< Plaid provided client ID >>>'  # WAS: customer_id
      p.secret = '<<< Plaid provided secret key >>>'    # No change
      p.env = :tartan  # or :api for production         # WAS: environment_location
    end
  7. Migrate from pre-14.0.0 to the latest version

    master

    If you are using a version of plaid-ruby older than 14.0.0 (released August 2021), you must update your integration to accommodate significant interface changes. Key changes include a new client initialization pattern using Plaid::Configuration, renamed endpoint methods that include underscores (e.g., auth_get instead of auth.get), and a change in how errors are handled via Plaid::ApiError.

    ### Client initialization
    
    From:
    
    ```ruby
    client = Plaid::Client.new(env: :sandbox,
                                client_id: client_id,
                                secret: secret)

    To:

    configuration = Plaid::Configuration.new
    configuration.server_index = Plaid::Configuration::Environment["sandbox"]
    configuration.api_key["PLAID-CLIENT-ID"] = ENV["PLAID_RUBY_CLIENT_ID"]
    configuration.api_key["PLAID-SECRET"] = ENV["PLAID_RUBY_SECRET"]
    configuration.api_key["Plaid-Version"] = "2020-09-14"
    
    api_client = Plaid::ApiClient.new(
      configuration
    )
    
    client = Plaid::PlaidApi.new(api_client)

    Endpoints

    From:

    response = client.auth.get(access_token)

    To:

    auth_get_request = Plaid::AuthGetRequest.new
    auth_get_request.access_token = access_token
    
    or
    
    auth_get_request = Plaid::AuthGetRequest.new({:access_token => access_token})
    
    response = client.auth_get(auth_get_request)

    Errors

    From:

    begin
      client.auth.get(auth_get_request)
    rescue Plaid::PlaidAPIError => e
      raise e if e.error_code != 'PRODUCT_NOT_READY'
      sleep 1
    end

    To:

    begin
      client.auth_get(auth_get_request)
    rescue Plaid::ApiError => e
      json_response = JSON.parse(e.response_body)
      if json_response["error_code"] != "PRODUCT_NOT_READY"
    end
  8. Migrate from 2.x.x to 3.0.0

    master

    In version 3.0.0, the Plaid::Institution class was updated to use Plaid's institutions/all endpoint, which combines 'native' and 'long tail' institutions. As a result, the Plaid::LongTailInstitution class has been removed. All functionality is now concentrated in Plaid::Institution.

    # Use Plaid::Institution.all instead of Plaid::LongTailInstitution.all
    Plaid::Institution.all
    
    # Use Plaid::Institution.search instead of Plaid::LongTailInstitution.search
    Plaid::Institution.search
    
    # Use Plaid::Institution.search_by_id instead of Plaid::LongTailInstitution.get
    Plaid::Institution.search_by_id
  9. Create a Plaid client

    master

    To interact with the Plaid API, you must initialize a Plaid::Configuration object with your client_id and secret, set the server_index to your desired environment, and then pass that configuration to a Plaid::ApiClient and finally a Plaid::PlaidApi instance.

    require 'plaid'
    
    configuration = Plaid::Configuration.new
    configuration.server_index = Plaid::Configuration::Environment["sandbox"]
    configuration.api_key["PLAID-CLIENT-ID"] = "***"
    configuration.api_key["PLAID-SECRET"] = "***"
    
    api_client = Plaid::ApiClient.new(
      configuration
    )
    
    client = Plaid::PlaidApi.new(api_client)
  10. Migrate Transaction and Account attributes from 1.x to 2.0.0

    master

    Several attribute names and types changed in version 2.0.0 to improve consistency and data accuracy.

    # Account Attributes
    # Plaid::Account#institution_type -> Plaid::Account#institution
    
    # Transaction Attributes
    # Plaid::Transaction#account -> Plaid::Transaction#account_id
    # Plaid::Transaction#date is now a Date object (was String)
    # Plaid::Transaction#cat (removed) -> Use Plaid::Transaction#category_hierarchy or Plaid::Transaction#category_id
    # Plaid::Transaction#category -> Plaid::Transaction#category_hierarchy
    # Plaid::Transaction#pending_transaction -> Plaid::Transaction#pending_transaction_id
  11. Understand the BankIncomeCompleteWebhook model

    master

    The Plaid::BankIncomeCompleteWebhook model represents the data payload sent via a webhook when a bank income report has finished generating or failed to generate. This webhook is typically triggered by calling the /credit/bank_income/get endpoint in a CRA-enabled client.

    Key attributes include:

    • webhook_type: The type of webhook (e.g., INCOME).
    • webhook_code: The specific webhook code (e.g., BANK_INCOME_COMPLETE).
    • user_id: The unique identifier for the user associated with the webhook.
    • result: A BankIncomeCompleteResult object containing the outcome of the report generation.
    • environment: The Plaid environment (e.g., sandbox, development, production).