At some point during your application lifecycle you may need to create a customer transactions report. These reports will list out all transactions for a single customer and can be exported into a useful format such as a CSV file. CSVs are a widely used format and can be easily imported into an Excel spreadsheet or third-party tool for data manipulation and analysis. Some use cases of a customer transactions report includes getting better insight on how specific customers use your application or to better support customers.

Use this post to equip your team with two methods for generating customer transactions reports. We’ll walk you through how to generate reports using a small proof-of-concept transactions-csv-export-sample that includes a script and web application.

How To Generate Customer Transaction Reports

To get started, you will want to have a Dwolla Sandbox account. After signing up, Dwolla will automatically create an application for testing the Dwolla API and provide a Key(client_id) and Secret(client_secret) to use within the sample application.

Method One: Using a Script

Create a script that will iterate through the customers transactions and write them to a CSV file. This can be done in most programming languages. For this blog, I am going to use Python since it has a built-in CSV and files library that makes exporting the data easy.

You will need at least your Dwolla Client ID, Secret Key and a Customer ID. The GitHub repository that has the full code for this example can be found here.

Follow the README to get the project setup. This first example takes place in reports.py.

The first step is to create an instance of a client object by using Dwolla’s Python SDK. From this client object, we can call a method to get a token object which we will use for all further requests.

import csv
import os
 
import dwollav2
 
client = dwollav2.Client(
    key='REPLACE WITH CLIENT ID',
    secret='REPLACE WITH SECRET KEY',
    environment='sandbox' # Set to sandbox, change to production if needed
)
 
customer_id = 'REPLACE WITH CUSTOMER ID'
 
# Gets a token from the client object
token = client.Auth.client()

Note to change the key, secret and customer_id variables. The next step would be to make the request to get the transaction history for the customer whose ID was provided.

# Create URL to endpoint using customer_id
url = f'customers/{customer_id}/transfers'
 
# Make request
res = token.get(url)
transfers = res.body['_embedded']['transfers']

To do so, a url needs to be constructed using the customer_id variable. Follow this up with making the request and getting the transactions from the response and storing it in a variable named transfers.

Next, the transfer data needs to be processed.

transactions = []
while True:
    for transfer in transfers:
        obj = {}
        obj['ID'] = transfer['id']
        obj['Created'] = transfer['created']
        obj['Status'] = transfer['status']
        obj['Amount'] = transfer['amount']['value']
        transactions.append(obj)
 
    if not 'next' in res.body['_links']:
        break
 
    res = token.get(res.body['_links']['next']['href'])
    transfers = res.body['_embedded']['transfers']

This is where you can select the fields you want to be returned in the CSV file as rows.

In this example, the CSV file will have a row for ID, Created, Status and Amount. Note that when making a request to get transaction data to Dwolla’s API, the data is paginated. This is why the logic is wrapped in a loop and checking to see if there’s a next page to get more transactions for the customer.

The final step is to export the data to a CSV file. Python makes this easy with its built-in CSV library which was imported at the top.

fields = ['ID', 'Created', 'Status', 'Amount']
filename = f'{customer_id}.csv'
 
with open(filename, 'w') as f:
    writer = csv.DictWriter(f, fields)
    writer.writeheader()
    writer.writerows(transactions)

The fields variable will be the names of the rows in the CSV file. It needs to match object keys in order when the data is being processed in the loop. The filename variable allows you to use any nomenclature to name the CSV file that will be generated.

In this example, the file will be named by using the customer ID. Run the reports.py file to generate a CSV file with python reports.py. Note that reports.py doesn’t have any error handling in it. For a fuller example, check out app.py.

In the script method, the general flow will always be the same:

  • Create a client object and get a token to interact with Dwolla’s API.
  • Make the request to get the customer transaction data.
  • Process the requested data to look however you wish.
  • Export the data to CSV.

Method Two: Create a Web Application

If you’d like the ability to create customer transactions reports without running scripts, create a web application. This can be useful to non-technical employees who wish to generate reports. The transactions reports repo has an example of both a script, and a web app.

To run the web application version, follow the steps in the README.

The web app example uses a React client, to interact with a flask API. It also shows an example on how filtering can be used in transaction reports as well. Filling out the form in the React client and submitting will return a CSV file that can be downloaded. The web app example also takes care of error handling!

Equip Your Team With Better Insights

Once you’ve created your Sandbox account (or if you’re already working with our API in production) choose one of these methods to generate a customer transaction report. Find the details about how specific customers are using your application—and export that data into a CSV file. You can adjust the script to export data into different formats such as JSON.

Exporting transaction reports using this technique gives you a lot of flexibility. Everything from the export format (CSV, JSON, etc) as well as the fields (ID, Created At, etc) included can be customized.

Once you move beyond the sandbox and start working with real data from your customers’ transactions, you’ll want to carefully consider which data your team needs and how to protect the information you receive about your customers’ transaction activity.

Ready to get started?

 
 

Stay Updated