Get access to my bank account using Python….why not?!? 🐍

I recently opened an account with Starling Bank, they are a UK based challenger bank. I had read “Banking On It: How I Disrupted an Industry” by their founder Anne Boden (a fantastic book BTW!) and was really intrigued as to how they would stack up against my current “traditional” bank…..anyway, this isn’t a blog about finance! I noticed that they had an API available, which was the perfect excuse for me to geek out 😀.

It was straightforward to get started, all I had to do was create a developer account and link this to my Starling bank account, which took me less than 5 minutes. Once I’d done this I headed over to the “Getting Started” page within their developer site and create a Personal Access Token (PAT) that could be used to access my account – as I wasn’t planning to create any apps that access anything other than my own personal account, I didn’t need to bother registering an app. When creating a PAT you can specify the scope – for example you may only want the PAT to be able to read your balance (wouldn’t it be nice if you could write to the balance 💵???).

As I wasn’t entirely sure what I was going to do with this newfound access to my bank account, I selected everything.

The API and documentation provided by Starling is superb, there’s even a Sandbox to play around in. I played around with the API for a couple of hours and managed to create some Python code to print my balance and details of my most recent transaction.

I put together the Python code below, which does the following:

  • Imports the required modules – requests is used to make the calls to the API and datetime is used to calculate dates within the get_transactions() function.
  • Stores the Personal Access Token in the PAT variable (breaking every single security best practice in one fell swoop)
  • Defines four functions:
    • get_account() – Retrieves the unique identifier of a bank account, as a user can have more than one account it’s important that the correct account is selected. As I have a single account, I just return what’s at index 0. This function is used by the get_balance() and get_transactions() functions.
    • get_default_category() – This retrieves the default category for an account, this is required for the get_transactions() function. An account can have multiple categories defined; new categories are created when additional spaces are added to an account. A space is akin to a virtual account within an account, for example you could create a space specifically for holiday savings – a really cool feature and something that was new to me.
    • get_balance() Returns the current balance of the account – I used the “effectiveBalance” which considers pending transactions so is more accurate than the alternative “clearedBalance”, which does not.
    • get_transactions()Returns details of the most recent transaction within a specific date range, this takes a single argument (days). Which is the number of days from today to look backwards.
import requests
import datetime
PAT = "insert PAT here"
url = ""
headers = {"Authorization": "Bearer " + PAT}
def get_account():
    r = requests.get(url + "accounts", headers=headers)
    return r.json()["accounts"][0]["accountUid"]
def get_default_category():
    r = requests.get(url + "accounts", headers=headers)
    return r.json()["accounts"][0]["defaultCategory"]
def get_balance():
    balance = requests.get(url + "accounts/" + (get_account()) + "/balance", headers=headers)
    print(str("£") + str(balance.json()["effectiveBalance"]["minorUnits"] / 100))
def get_transactions(days):
    datefrom = ("%Y-%m-%d") + "T00:00:00Z"
    feeditems = requests.get(url + "feed/account/" + (get_account()) + "/category/" + (get_default_category()) + "?changesSince=" + datefrom, headers=headers)
    print(feeditems.json()["feedItems"][0]["source"] + "\n" + feeditems.json()["feedItems"][0]["direction"] + "\n" + feeditems.json()["feedItems"][0]["amount"]["currency"] \
    + ":" + str(feeditems.json()["feedItems"][0]["amount"]["minorUnits"] / 100))

I’ve upload this script to GitHub too.

Here’s the functions in action! The call to get_balance demonstrates how rich I am! For get_transactions() I passed the argument 30, which will return the most recent transaction in the previous 30 days. Including who the transaction was with, the direction (money in/out) and the amount.

As I continue to experiment with the API, I’m tempted to write an Alexa skill that I can use to query my account 🤔.

3 thoughts on “Get access to my bank account using Python….why not?!? 🐍

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s