• Stars
    star
    402
  • Rank 106,749 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python bindings for Plaid

plaid-python Circle CI PyPI version

The official python client library for the Plaid API, which is generated from our OpenAPI spec. For the last non-generated version of our library, go here.

Table of Contents

Install

This library only supports python3!

$ pip3 install plaid-python

Documentation

The module supports all Plaid API endpoints. For complete information about the API, head to the docs.

Getting Started

Calling Endpoints

To call an endpoint you must create a PlaidApi object.

import plaid
from plaid.api import plaid_api

# Available environments are
# 'Production'
# 'Development'
# 'Sandbox'
configuration = plaid.Configuration(
    host=plaid.Environment.Sandbox,
    api_key={
        'clientId': client_id,
        'secret': secret,
    }
)

api_client = plaid.ApiClient(configuration)
client = plaid_api.PlaidApi(api_client)

Each endpoint returns a dictionary which contains the parsed JSON from the HTTP response.

Versioning

This release only supports the latest Plaid API version, 2020-09-14.

For information about what has changed between versions and how to update your integration, head to the API upgrade guide.

The plaid-python client library is typically updated on a monthly basis. The canonical source for the latest version number is the client library changelog.

Errors

All non-200 responses will throw a plaid.ApiException.

import plaid
from plaid.model.asset_report_get_request import AssetReportGetRequest

try:
    request = AssetReportGetRequest(
        asset_report_token=asset_report_token,
    )
    return client.asset_report_get(request)
except plaid.ApiException as e:
    response = json.loads(e.body)
    # check the code attribute of the error to determine the specific error
    if response['error_code'] == 'ITEM_LOGIN_REQUIRED':
        # the users' login information has changed, generate a public_token
        # for the user and initialize Link in update mode to
        # restore access to this user's data
        # see https://plaid.com/docs/api/#updating-items-via-link
    else:
        ...

For more information on Plaid response codes, head to the docs.

Data type differences from API and from previous versions

Converting the response to a JSON

As this is a common question, we've included this in the README. plaid-python uses models like TransactionsSyncResponse to encapsulate API responses. If you want to convert this to a JSON, do something like this:

import json
...
response = ... # type TransactionsSyncResponse
# to_dict makes it first a python dictionary, and then we turn it into a string JSON.
json_string = json.dumps(response.to_dict(), default=str)

Dates

Dates and datetimes in requests, which are represented as strings in the API and in previous client library versions, are represented in this version of the Python client library as Python datetime.date or datetime.datetime objects. If you need to convert between dates and strings, you can use the datetime.strptime method. For an example, see the Retrieve Transactions sample code later in this Readme. For more information on the Python's datetime module, see Python's official documentation.

Note that the datetime.strptime method will silently remove time zone information. Time zone information is required for request fields that accept datetimes. Failing to include time zone information (or passing in a string, instead of a datetime.datetime object) will result in an error. See the following examples for guidance on datetime.date and datetime.datetime usage.

If the API reference documentation for a field specifies format: date, either of following are acceptable:

from datetime import date

a = date(2022, 5, 5)
b = date.fromisoformat('2022-05-05')

If the API reference documentation for a field specifies format: date-time, the following is acceptable:

from datetime import datetime

a = datetime(2022, 5, 5, 22, 35, 49, tzinfo=datetime.timezone.utc)

Enums

While the API and previous library versions represent enums using strings, this current library uses Python classes with restricted values.

Old:

'products': ['auth', 'transactions'],
'country_codes': ['US'],

Current:

products=[Products('auth'), Products('transactions')],
country_codes=[CountryCode('US')],

Examples

Create an Item using Link

Exchange a public_token from Plaid Link for a Plaid access token:

import plaid
from plaid.model.item_public_token_exchange_request import ItemPublicTokenExchangeRequest

# the public token is received from Plaid Link
exchange_request = ItemPublicTokenExchangeRequest(
    public_token=pt_response['public_token']
)
exchange_response = client.item_public_token_exchange(exchange_request)
access_token = exchange_response['access_token']

Create a Stripe bank account token

Exchange a Plaid Link public_token for an API access_token. Then exchange that access_token and the Plaid Link account_id (received along with the public_token) for a Stripe bank_account_token:

import plaid
from plaid.model.item_public_token_exchange_request import ItemPublicTokenExchangeRequest
from plaid.model.processor_stripe_bank_account_token_create_request import ProcessorStripeBankAccountTokenCreateRequest

exchange_request = ItemPublicTokenExchangeRequest(
    public_token=pt_response['public_token']
)
exchange_response = client.item_public_token_exchange(exchange_request)
access_token = exchange_response['access_token']

request = ProcessorStripeBankAccountTokenCreateRequest(
    access_token=access_token,
    account_id='[Account ID]',
)
stripe_response = client.processor_stripe_bank_account_token_create(request)
bank_account_token = stripe_response['stripe_bank_account_token']

Remove Item

import plaid
from plaid.model.item_remove_request import ItemRemoveRequest

# Provide the access token for the Item you want to remove
request = ItemRemoveRequest(
    access_token=accessToken
)
response = client.item_remove(request)

Retrieve Transactions (preferred method)

import plaid
from plaid.model.transactions_sync_request import TransactionsSyncRequest

request = TransactionsSyncRequest(
    access_token=access_token,
)
response = client.transactions_sync(request)
transactions = response['added']

# the transactions in the response are paginated, so make multiple calls while incrementing the cursor to
# retrieve all transactions
while (response['has_more']):
    request = TransactionsSyncRequest(
        access_token=access_token,
        cursor=response['next_cursor']
    )
    response = client.transactions_sync(request)
    transactions += response['added']

Retrieve Transactions (older method)

import plaid
from plaid.model.transactions_get_request_options import TransactionsGetRequestOptions
from plaid.model.transactions_get_request import TransactionsGetRequest

request = TransactionsGetRequest(
    access_token=access_token,
    start_date=datetime.strptime('2020-01-01', '%Y-%m-%d').date(),
    end_date=datetime.strptime('2021-01-01', '%Y-%m-%d').date(),
)
response = client.transactions_get(request)
transactions = response['transactions']

# the transactions in the response are paginated, so make multiple calls while increasing the offset to
# retrieve all transactions
while len(transactions) < response['total_transactions']:
    options = TransactionsGetRequestOptions()
    options.offset = len(transactions)

    request = TransactionsGetRequest(
        access_token=access_token,
        start_date=datetime.strptime('2020-01-01', '%Y-%m-%d').date(),
        end_date=datetime.strptime('2021-01-01', '%Y-%m-%d').date(),
        options=options
    )
    response = client.transactions_get(request)

Retrieve Asset Report PDF

from plaid.model.asset_report_pdf_get_request import AssetReportPDFGetRequest

pdf_request = AssetReportPDFGetRequest(asset_report_token=PDF_TOKEN)
pdf = client.asset_report_pdf_get(pdf_request)
FILE = open('asset_report.pdf', 'wb')
FILE.write(pdf.read())
FILE.close()

Authentication

Public endpoints (category information) require no authentication and can be accessed as follows:

categories = client.categories_get({})

Authenticated endpoints require a (client_id, secret) pair. You do not need to pass in authentication to individual endpoints once you have set it on the plaid.Configuration object.

Contributing

Please see Contributing for guidelines and instructions for local development.

License

MIT

More Repositories

1

quickstart

Get up and running with Plaid Link and the API in minutes
TypeScript
537
star
2

plaid-node

Node bindings for Plaid
TypeScript
518
star
3

pattern

An example end-to-end Plaid integration to create items and fetch transaction data
TypeScript
441
star
4

plaid-postman

Postman collection for the Plaid API
HTML
315
star
5

react-plaid-link

React bindings for Plaid Link
TypeScript
264
star
6

deprecated-link

This repository is now deprecated. To integrate with Plaid, visit the docs.
261
star
7

plaid-ruby

Ruby bindings for Plaid
Ruby
221
star
8

plaid-go

go bindings for Plaid
Go
194
star
9

deprecated-async-problem

🔀 Solutions and non-solutions to JavaScript's async problem
JavaScript
186
star
10

react-native-plaid-link-sdk

Plaid Link for React Native
TypeScript
170
star
11

plaid-java

Java bindings for Plaid
Java
124
star
12

plaid-link-ios

Plaid Link iOS SDK
Objective-C
116
star
13

plaid-link-android

Plaid Link Android SDK
Kotlin
100
star
14

tiny-quickstart

A minimal quickstart demonstrating Plaid Link, Balances, and OAuth
JavaScript
77
star
15

plaid-openapi

API version 2020-09-14
74
star
16

support

Plaid Support
66
star
17

plaid-link-examples

Plaid Link Webview, ReactNative examples
Objective-C
60
star
18

pattern-account-funding

Sample code to demonstrate Plaid-powered account funding use cases using the Auth, Identity, and Balance APIs. Includes examples of using Auth partners for end-to-end funds transfers.
TypeScript
44
star
19

envvar

Derive JavaScript values from environment variables
JavaScript
37
star
20

npmserve

fast npm installs for slow clients
Shell
36
star
21

npmserve-server

fast npm installs for slow clients
JavaScript
27
star
22

go-envvar

A go library for managing environment variables. Maps to typed values, supports required and optional vars with defaults.
Go
25
star
23

idv-quickstart

Get up and running with Plaid Identity Verification in minutes
CSS
22
star
24

tutorial-resources

Sample apps and material to go along with Plaid's tutorials
JavaScript
18
star
25

income-sample

Sample app for Income
TypeScript
15
star
26

account-funding-tutorial

TypeScript
13
star
27

sandbox-custom-users

JSON files specifying custom users suitable for testing Plaid integrations on Sandbox
Python
13
star
28

nockingbird

Declarative HTTP mocking (for use with Nock)
CoffeeScript
13
star
29

core-exchange

The Core Exchange spec and generated server code
MDX
12
star
30

pattern-transfers

TypeScript
11
star
31

payment-initiation-pattern-app

Payment Initiation demo app for Europe, based on Plaid Pattern
TypeScript
6
star
32

credit-attributes

Attributes that can be calculated using Plaid's credit products
Python
4
star
33

plaid-node-legacy

⚠️This library has been deprecated and archived. Our current libraries can be found at https://plaid.com/docs/libraries/ or https://github.com/plaid
JavaScript
4
star
34

assets-attributes

Attributes that can be calculated using a Plaid Asset Report
Python
3
star
35

plaid-java-legacy

⚠️This library has been deprecated and archived. Our current libraries can be found at https://plaid.com/docs/libraries/ or https://github.com/plaid
Java
3
star
36

plaid-python-legacy

⚠️This library has been deprecated and archived. Our current libraries can be found at https://plaid.com/docs/libraries/ or https://github.com/plaid
Python
3
star
37

plaid-ruby-legacy

⚠️This library has been deprecated and archived. Our current libraries can be found at https://plaid.com/docs/libraries/ or https://github.com/plaid
Ruby
2
star
38

.github

1
star
39

plaid-go-legacy

⚠️This library has been deprecated and archived. Our current libraries can be found at https://plaid.com/docs/libraries/ or https://github.com/plaid
1
star