Pythonista Shortcuts


I’m excited about the new possibilities and created a small python script to automate daily entered transactions. The script runs locally on my iOS device (see Pythonista app).

Just wanted to share this with you if anyone has a similar use case.

Base script:

# coding: utf-8
import console, requests, json, datetime

def get_id_for_name(collection, name):
    for item in collection:
        if item['name']==name:
            return item['id']
    raise ValueError('name not found in collection!', name, collection)

def get_initdata():
        with open("ynab_data_file.json") as data_file:
            data = json.load(data_file)
            print('Data loaded:', data)
            return data

    except FileNotFoundError:
        print('Data file not found. Getting new data.')
        pa_token = console.input_alert('YNAB Personal Access Token needed')
        header = {'Authorization': 'Bearer ' + pa_token}

        budgets = requests.get('', headers=header)
        budgets = budgets.json()['data']['budgets']
        budget_id = get_id_for_name(budgets, 'W Budget')

        accounts = requests.get('' + budget_id + '/accounts', headers=header)
        accounts = accounts.json()['data']['accounts']
        account_id = get_id_for_name(accounts, 'Company')

        category_groups = requests.get('' + budget_id + '/categories', headers=header)
        category_groups = category_groups.json()['data']['category_groups']
        for category_group in category_groups:
            if category_group['name']=='Everyday Expenses':
                for category in category_group['categories']:
                    if category['name']=='Aramark':
                        category_id = category['id']
        if not category_id:
            raise ValueError('Category not found!')

        payees = requests.get('' + budget_id + '/payees', headers=header)
        payees = payees.json()['data']['payees']
        payee_name = 'Aramark'
        payee_id = get_id_for_name(payees, payee_name)

        data = {
            'header': {
                'Authorization': 'Bearer ' + pa_token
            'data_cache': {
                'budget_id': budget_id,
                'account_id': account_id,
                'category_id': category_id,
                'payee_id': payee_id,
                'payee_name': payee_name,
        with open("ynab_data_file.json", "w") as write_file:
            write_file.write(json.dumps(data, indent=4))

        return get_initdata()

def enter_aramark_tx(amount, memo):
    initdata = get_initdata()
    txdata = {
        'transaction': {
            'account_id': initdata['data_cache']['account_id'],
            'amount': amount,
            'payee_id': initdata['data_cache']['payee_id'],
            'payee_name': initdata['data_cache']['payee_name'],
            'category_id': initdata['data_cache']['category_id'],
            'memo': memo + ' (API-Eintrag)',
            'cleared': 'cleared',
            'approved': True,
            'flag_color': None,
            'import_id': None

    txresponse ='' + initdata['data_cache']['budget_id'] + '/transactions', headers=initdata['header'], json=txdata).json()
    print(json.dumps(txresponse, indent=4))

    account_balance = requests.get('' + initdata['data_cache']['budget_id'] + '/accounts/' + initdata['data_cache']['account_id'], headers=initdata['header']).json()['data']['account']['balance']
    print('Success. New Account balance: ', account_balance/1000)

if __name__ == '__main__':
    enter_aramark_tx(-50, 'Test')

Example shortcut (gets placed on the homescreen):

from YNAB_API_Aramark import enter_aramark_tx
enter_aramark_tx(-850, 'Wasserkauf')
6replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • whustedt This is cool!  Thanks for sharing.

  • For better readability you can surround the balance statement with font declarations like this: 

        console.set_color(0, 0.5, 0) # green font
        console.set_font('Menlo', 20) # bigger font
        print('Success.\nNew Account balance: ', account_balance/1000)
        console.set_color() # reset font color
        console.set_font() # reset font size

    To show the practicality of my use case I uploaded a sample video to YouTube.

    Like 1
    • whustedt Nice!  That's a handy script, there.  Thanks for sharing!

      • whustedt
      • whustedt
      • 1 mth ago
      • 1
      • Reported - view

      Brady Being able to scan a physical receipt or using a screenshot as input for manual tx entry would be a great general feature of the mobile app in my opinion.

      Is there an easy way to promote that idea?

      Like 1
  • This is incredible! Exactly what I needed. Thanks whustedt !

    I wanted a simple way to add Apple Card transactions to YNAB.

    I was able to create a shortcut that takes a screenshot as input. I take a screenshot of a transaction in the Wallet app, the shortcut detects the text (using Toolbox Pro), then sends all the transaction details over to Pythonista where I used your script to connect to the YNAB API.

      • whustedt
      • whustedt
      • 1 mth ago
      • Reported - view

      Blue Packet (8e71cb739e1c) 

      Wow, that’s a great idea, congrats!

Like1 Follow
  • 1 Likes|like
  • 1 mth agoLast active
  • 6Replies
  • 1000Views
  • 3 Following