Plaid Java SDK

repository·master·Indexed 41 days ago

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

Official Java client library for the Plaid API. Features include synchronous and asynchronous API calls using Retrofit and OkHttp, support for Java 8 LocalDate and OffsetDateTime, and integration with Maven. Version 41.0.0 supports the 2020-09-14 API version.

Tokens
1.8K
Snippets
5
Records
7
Agent score
50%

What's inside Plaid

  1. Use LocalDate and OffsetDateTime for API dates

    master

    The library represents API date strings as Java 8 LocalDate or OffsetDateTime objects.

    • For format: date, use java.time.LocalDate.
    • For format: date-time, use java.time.OffsetDateTime.

    Note: Time zone information is required for date-time fields. Failing to include time zone information or providing a plain string instead of an OffsetDateTime will result in an error.

    import java.time.LocalDate;
    import java.time.OffsetDateTime;
    
    // For format: date
    LocalDate myDate = LocalDate.parse("2019-12-06");
    
    // For format: date-time
    OffsetDateTime myDateTime = OffsetDateTime.parse("2019-12-06T22:35:49+00:00");
  2. Initialize the Plaid API client

    master

    To use the library, create an ApiClient with your clientId and secret, set the appropriate environment adapter (e.g., ApiClient.Sandbox), and then create the PlaidApi service.

    HashMap<String, String> apiKeys = new HashMap<String, String>();
    apiKeys.put("clientId", plaidClientId);
    apiKeys.put("secret", plaidSecret);
    ApiClient apiClient = new ApiClient(apiKeys);
    apiClient.setPlaidAdapter(ApiClient.Sandbox); // or equivalent
    PlaidApi plaidClient = apiClient.createService(PlaidApi.class);
  3. Install plaid-java via Maven

    master

    Add the plaid-java dependency to your pom.xml. You should check Maven Central or the GitHub tags for the latest version number to ensure you have support for the most recent Plaid API endpoints and fields.

    <dependency>
      <groupId>com.plaid</groupId>
      <artifactId>plaid-java</artifactId>
      <!--Replace this version number with the latest version, which can be found at https://github.com/plaid/plaid-java/tags-->
      <version>41.0.0</version>
    </dependency>
  4. Migrate from pre-9.0.0 to latest versions

    master

    Version 9.0.0 introduced significant breaking changes. If you are upgrading from a version older than 9.0.0, you must address the following:

    • Model Imports: Move from com.plaid.request.* and com.plaid.response.* to com.plaid.model.*.
    • Request Structure: Request models now use builder syntax (e.g., new Request().param(value)) instead of constructor arguments. All with* setters have been renamed to plain setters (e.g., .withEndDate(date) becomes .endDate(date)).
    • Enums: Country codes and products now use typed Enums (e.g., CountryCode.US, Products.AUTH) instead of Strings or singletons.
    • Error Model: ErrorResponse is now Error, and ErrorType is now Error.ErrorTypeEnum.
    • Account Model: Account is now AccountBase.
    • Client Methods: Remove .service() from endpoint queries. Endpoints starting with get are now named {Model}Get (e.g., accountsBalanceGet).
  5. Handle API error responses

    master

    When an API call fails, you can decode the error body into a PlaidError object using a JSON parser like Gson.

    try {
      Gson gson = new Gson();
      PlaidError error = gson.fromJson(response.errorBody().string(), PlaidError.class);
    } catch (Exception e) {
      throw new Exception(
        String.format(
          "Failed converting from API Response Error Body to Error %s",
          response.errorBody().string()
        )
      );
    }
  6. Make synchronous and asynchronous API calls

    master

    The library uses Retrofit and OkHttp. You can execute calls synchronously using .execute() or asynchronously using .enqueue() with a callback. Required request parameters are passed via the request object's builder methods.

    // Synchronously exchange a Link public_token for an API access_token
    ItemPublicTokenExchangeRequest request = new ItemPublicTokenExchangeRequest().publicToken("the_link_public_token");
    Response<ItemPublicTokenExchangeResponse> response = plaidClient
        .itemPublicTokenExchange(request).execute();
    
    if (response.isSuccessful()) {
      accessToken = response.body().getAccessToken();
    }
    
    // Asynchronously do the same thing
    ItemPublicTokenExchangeRequest request = new ItemPublicTokenExchangeRequest().publicToken(publicToken);
    plaidClient
        .itemPublicTokenExchange(request)
        .enqueue(new Callback<ItemPublicTokenExchangeResponse>() {
            @Override
            public void onResponse(Call<ItemPublicTokenExchangeResponse> call, Response<ItemPublicTokenExchangeResponse> response) {
              if (response.isSuccessful()) {
                accessToken = response.body().getAccessToken();
              }
            }
    
            @Override
            public void onFailure(Call<ItemPublicTokenExchangeResponse> call, Throwable t) {
              // handle the failure as needed
            }
        });
  7. Versioning and API compatibility

    master

    Each major version of plaid-java targets a specific version of the Plaid API. It is strongly recommended to use a recent version to ensure support for new endpoints and fields.

    API versionplaid-java release
    2020-09-14 (latest)8.x.x and higher
    2019-05-297.x.x
    2018-05-224.x.x (and 3.x.x)
    2017-03-082.x.x