Package: ynab-api-import
ynab-api-importThis library enables importing YNAB transactions via theGocardless Bank Account Data API (formerly Nordigen).
It can be helpful for cases in which your bank is not covered by YNABs native import functionality.PreparationsGocardless Bank Account API (formerly Nordigen)Checkif your bank is supported by the API.Create an account with Gocardless for the Bank Account Data API (They have a separate Login for it which you can
get to by clicking on 'Get API Keys' or clicking the link at the bottom of their standard login page)Go to Developers -> User Secrets and create a new pair of secret_id and secret_keyYNABCreate a personal access token for YNAB as describedhereBasic Usage1. Install library from PyPIpipinstallynab-api-import2. Initiate LibraryProvide a unique reference (e.g.'mycheckingaccount')  per bank connection to identify the grant later on.
You can find the IDs of your budget and the account if you go tohttps://app.ynab.com/and open the target account by clicking on the name on the left hand side menu. The URL does now contain both IDshttps://app.ynab.com/<budget_id>/accounts/<account_id>fromynabapiimportimportYnabApiImportynab_api_import=YnabApiImport(secret_id='<secret_id>',secret_key='<secret_key>',reference='<reference>',token='<ynab_token>',budget_id='<budget_id>',account_id='<account_id>')Optionally you can initiate an object from aconfig.yamlfile. To do that create a YAML file with the following content:secret_id:<secret_id>secret_key:<secret_key>reference:<reference>token:<ynab_token>budget_id:<budget_id>account_id:<account_id>Save the file and provide the path to the library when initializingynab_api_import=YnabApiImport.from_yaml('path/to/config.yaml')2. Find the institution_id of your bankCountrycode is ISO 3166 two-character country code.ynab_api_import.fetch_institutions(countrycode='<countrycode>')You get back a dictionary with all available banks in that country and their institution_ids.
Find and save the institution_id of your bank.[{'name':'<name>','institution_id':'<institution_id>'}]3. Create Auth Link and authenticate with your bankProvide the institution_id. You get back a link which you need to copy to your browser and go through authentication flow with your bankynab_api_import.create_auth_link(institution_id='<institution_id>')4. Run import with your reference and YNAB identifiersOptionally you can provide astartdateargument in form of adatetime.dateobject to only import transactions from a specific date onwards. Equally optionally you can provide amemo_regexargument in from of a regex string to the call to clean the memo string before importing into YNAB. A good helper to write your regex ishttps://regex101.comynab_api_import.import_transactions()Advanced UsageHandling of multiple accounts in your bank connection (MultipleAccountsError)The library assumes that you have one active account in your bank connection. It will raise an error if there are no accounts in your connection or more than one. In the latter case you need to provide the correctresource_idwhen initializing the library. You can find theresource_idby looking into the available options in the error message.fromynabapiimportimportYnabApiImportynab_api_import=YnabApiImport(resource_id='<resource_id>',secret_id='<secret_id>',secret_key='<secret_key>',reference='<reference>',token='<ynab_token>',budget_id='<budget_id>',account_id='<account_id>')Show LogsThe library logs information about the result of the methods on the 'INFO' level. If you want to see these logs import
the logging module and set it to the levelINFO. You can also access the logger for advanced configuration via theloggerattribute of yourYnabApiImportinstance.importlogginglogging.basicConfig(level='INFO')Testing yourmemo_regexYou can test yourmemo_regexwith a call totest_memo_regex(). The function will fetch transactions from your bank account, apply the regex and output the old and new memo strings in a dict for inspectionynab_api_import.test_memo_regex(memo_regex=r'<memo_regex')returns a list ofdictwith following content[{original_memo: cleaned_memo}]