Get transactions
Tags: #plaid #bank #transactions #snippet
Author: Martin Donadieu

Input

Install packages

1
pip install plaid-python
Copied!

Create account here :

https://plaid.com/

Import libraries

1
import os
2
import plaid
3
import naas
4
import IPython.core.display
5
import uuid
6
import json
Copied!

Config your variables

1
PLAID_CLIENT_ID = "*************"
2
PLAID_SECRET = "*************"
3
PLAID_ENV = 'sandbox'
4
5
PLAID_PRODUCTS = ['transactions']
6
PLAID_COUNTRY_CODES = ['FR']
7
start_transaction = "2020-09-01"
8
end_transaction = "2020-10-01"
Copied!

Model

Connect to plaid

1
client = plaid.Client(client_id=PLAID_CLIENT_ID,
2
secret=PLAID_SECRET,
3
environment=PLAID_ENV)
Copied!
1
def create_link_token():
2
response = client.LinkToken.create(
3
{
4
'user': {
5
# This should correspond to a unique id for the current user.
6
'client_user_id': 'user-id',
7
},
8
'client_name': "Plaid Quickstart",
9
'products': PLAID_PRODUCTS,
10
'country_codes': PLAID_COUNTRY_CODES,
11
'language': "en",
12
'redirect_uri': None,
13
}
14
)
15
return response
Copied!
1
token = create_link_token()
2
token
Copied!

Use Naas callback to get the plaid OAuth token

1
cb_url = naas.callback.add()
Copied!

Select Bank connection

1
uid = uuid.uuid4().hex
2
iframe = """
3
<head>
4
<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>
5
</head>
6
<script>
7
const handler_{uid} = Plaid.create({
8
token: '{GENERATED_LINK_TOKEN}',
9
onSuccess: (public_token, metadata) => {
10
const xhr = new XMLHttpRequest();
11
xhr.open("POST", "{CALLBACK_URL}", true);
12
xhr.setRequestHeader('Content-Type', 'application/json');
13
xhr.send(JSON.stringify({
14
public_token: public_token
15
}));
16
}
17
});
18
handler_{uid}.open();
19
</script>
20
"""
21
iframe = iframe.replace('{uid}', uid)
22
iframe = iframe.replace('{CALLBACK_URL}', cb_url.get('url'))
23
iframe = iframe.replace('{GENERATED_LINK_TOKEN}', token.get('link_token'))
24
IPython.core.display.display(IPython.core.display.HTML(iframe))
Copied!

Get back plaid token

1
cb_data = naas.callback.get(cb_url.get('uuid'))
2
cb_data = json.loads(cb_data)
3
public_token = cb_data.get("public_token")
4
public_token
Copied!

Exange token

1
exchange_response = client.Item.public_token.exchange(public_token)
2
access_token = exchange_response['access_token']
3
item_id = exchange_response['item_id']
Copied!

Output

Show transactions

1
response = client.Transactions.get(access_token,
2
start_date=start_transaction,
3
end_date=end_transaction)
4
transactions = response['transactions']
5
6
while len(transactions) < response['total_transactions']:
7
response = client.Transactions.get(access_token,
8
start_date=start,
9
end_date=end,
10
offset=len(transactions)
11
)
12
transactions.extend(response['transactions'])
13
transaction_df = pd.DataFrame.from_records(transactions)
14
transaction_df
Copied!

Save as csv

1
transaction_df.to_csv('transactions.csv')
Copied!
If you need more data check the api doc
https://plaid.com/docs/
Last modified 2mo ago
Copy link
Edit on GitHub