diff --git a/api_endpoints/Dolfin_Basiq_API.ipynb b/api_endpoints/Dolfin_Basiq_API.ipynb new file mode 100644 index 0000000..115d284 --- /dev/null +++ b/api_endpoints/Dolfin_Basiq_API.ipynb @@ -0,0 +1,394 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "API Key:\n", + "NDNmNTczMGQtZjJhZC00ODQ4LTkxZDUtN2VhMDMyNzgyZmIyOjNiM2MwMjFlLTUzZDMtNDg3MC04MjU3LTk1YzRkNWI2MmU5Mg==" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the server side request, to generate an auth token for this session" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXJ0bmVyaWQiOiI4MWIzYTMwMC0zNzUwLTQzMzItOThjNi00ODU4NTgyMWFjM2EiLCJhcHBsaWNhdGlvbmlkIjoiNDNmNTczMGQtZjJhZC00ODQ4LTkxZDUtN2VhMDMyNzgyZmIyIiwic2NvcGUiOiJTRVJWRVJfQUNDRVNTIiwic2FuZGJveF9hY2NvdW50Ijp0cnVlLCJjb25uZWN0X3N0YXRlbWVudHMiOmZhbHNlLCJlbnJpY2giOiJkaXNhYmxlZCIsImVucmljaF9lbnRpdHkiOmZhbHNlLCJlbnJpY2hfbG9jYXRpb24iOmZhbHNlLCJlbnJpY2hfY2F0ZWdvcnkiOmZhbHNlLCJhZmZvcmRhYmlsaXR5Ijoic2FuZGJveCIsImluY29tZSI6InNhbmRib3giLCJleHBlbnNlcyI6InNhbmRib3giLCJleHAiOjE2ODE5MDQ4NjMsImlhdCI6MTY4MTkwMTI2MywidmVyc2lvbiI6IjMuMCIsImRlbmllZF9wZXJtaXNzaW9ucyI6W119.ezG8BiWMK_dRX4mVC34_LoZzjGjaPx3lpCOeLf1rhoA\n" + ] + } + ], + "source": [ + "import requests\n", + "\n", + "url = \"https://au-api.basiq.io/token\"\n", + "\n", + "payload = \"scope=SERVER_ACCESS\"\n", + "headers = {\n", + " \"accept\": \"application/json\",\n", + " \"basiq-version\": \"3.0\",\n", + " \"content-type\": \"application/x-www-form-urlencoded\",\n", + " \"Authorization\": \"Basic NDNmNTczMGQtZjJhZC00ODQ4LTkxZDUtN2VhMDMyNzgyZmIyOjNiM2MwMjFlLTUzZDMtNDg3MC04MjU3LTk1YzRkNWI2MmU5Mg==\"\n", + "}\n", + "\n", + "response = requests.post(url, data=payload, headers=headers)\n", + "json_response = response.json()\n", + "access_token = \"Bearer \" + json_response[\"access_token\"]\n", + "\n", + "print(access_token)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This section is to create a user, API will not check for duplicated user, will need to retrieve user using unique userid before creating in the future.\n", + "\n", + "Backend will need a table to retrieve Basiq User ID based on DolFin's website user logins." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# url = \"https://au-api.basiq.io/users\"\n", + "\n", + "# payload = {\n", + "# \"email\": \"John@Wentworth-Smith.com\",\n", + "# \"firstName\": \"John\",\n", + "# \"lastName\": \"Wentworth-Smith\"\n", + "# }\n", + "# headers = {\n", + "# \"accept\": \"application/json\",\n", + "# \"content-type\": \"application/json\",\n", + "# \"authorization\": access_token,\n", + "# }\n", + "\n", + "# response = requests.post(url, json=payload, headers=headers)\n", + "# json_response = response.json()\n", + "# userid = json_response[\"id\"]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I have imported sample users to this account, User ID 11103cba-4a08-4397-84a5-22ac125ed2f6 (John Wentworth-Smith), and will be used from now on.\n", + "\n", + "Data description as per link: https://api.basiq.io/reference/testing. I have used Wentworth-Smith for ease of development for now.\n", + "\n", + "\n", + "Code below connects to our API as a client, in this case John, and prints out an URL for user to manually click on.\n", + "\n", + "Once connected to front end, the website should open the URL for user to provide bank detail and consent agreement.\n", + "\n", + "Consent UI link: https://consent.basiq.io/home?token={{client_token_bound_to_userId}}, The token must not be enclosed in parenthesis '{{' '}}'" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "https://consent.basiq.io/home?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXJ0bmVyaWQiOiI4MWIzYTMwMC0zNzUwLTQzMzItOThjNi00ODU4NTgyMWFjM2EiLCJhcHBsaWNhdGlvbmlkIjoiNDNmNTczMGQtZjJhZC00ODQ4LTkxZDUtN2VhMDMyNzgyZmIyIiwidXNlcmlkIjoiMTExMDNjYmEtNGEwOC00Mzk3LTg0YTUtMjJhYzEyNWVkMmY2Iiwic2NvcGUiOiJDTElFTlRfQUNDRVNTIiwic2FuZGJveF9hY2NvdW50Ijp0cnVlLCJjb25uZWN0X3N0YXRlbWVudHMiOmZhbHNlLCJlbnJpY2giOiJkaXNhYmxlZCIsImVucmljaF9lbnRpdHkiOmZhbHNlLCJlbnJpY2hfbG9jYXRpb24iOmZhbHNlLCJlbnJpY2hfY2F0ZWdvcnkiOmZhbHNlLCJhZmZvcmRhYmlsaXR5Ijoic2FuZGJveCIsImluY29tZSI6InNhbmRib3giLCJleHBlbnNlcyI6InNhbmRib3giLCJleHAiOjE2ODE4OTU1MTEsImlhdCI6MTY4MTg5MTkxMSwidmVyc2lvbiI6IjMuMCIsImRlbmllZF9wZXJtaXNzaW9ucyI6W119.2bppPmHfwGUBoyTR3-LIRDD2lZOqVRtapDaP90kHSrw\n" + ] + } + ], + "source": [ + "import requests\n", + "\n", + "url = \"https://au-api.basiq.io/token\"\n", + "\n", + "payload = \"scope=CLIENT_ACCESS&userId=11103cba-4a08-4397-84a5-22ac125ed2f6\"\n", + "headers = {\n", + " \"accept\": \"application/json\",\n", + " \"basiq-version\": \"3.0\",\n", + " \"content-type\": \"application/x-www-form-urlencoded\",\n", + " \"Authorization\": \"Basic NDNmNTczMGQtZjJhZC00ODQ4LTkxZDUtN2VhMDMyNzgyZmIyOjNiM2MwMjFlLTUzZDMtNDg3MC04MjU3LTk1YzRkNWI2MmU5Mg==\"\n", + "}\n", + "\n", + "response = requests.post(url, data=payload, headers=headers)\n", + "json_response = response.json()\n", + "access_token = \"Bearer \" + json_response[\"access_token\"]\n", + "\n", + "print(\"https://consent.basiq.io/home?token=\" + json_response[\"access_token\"])\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "All the API calls below requires server side access token, pleas re-run the first block of code\n", + "\n", + "API calls to retrieve linked accounts and top 500 transactions" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " type id accountNo \\\n", + "0 account 2df58481-c77a-40b1-8249-949cb3ea9670 12345612345678 \n", + "1 account 49544383-c19c-4c97-b8d1-5322d0faa6af 23456723456789 \n", + "2 account b2360b69-e3f3-49f3-bbc4-65d04865db02 34567834567890 \n", + "3 account 0b107c33-e858-4bbd-abba-f757ae4da1fc 45678945678901 \n", + "\n", + " name currency balance availableFunds lastUpdated \\\n", + "0 Mortgage 746833 AUD 7723.25 7723.25 2023-04-18T14:00:00Z \n", + "1 Credit Card 13000 AUD 3874.74 3874.74 2023-04-18T14:00:00Z \n", + "2 Savings 123890 AUD 14340.24 14340.24 2023-04-18T14:00:00Z \n", + "3 Transaction 14000 AUD -7409.35 -7409.35 2023-04-18T14:00:00Z \n", + "\n", + " institution creditLimit ... accountHolder \\\n", + "0 AU00000 400000.00 ... Max Wentworth-Smith & Paul Wentworth-Smith \n", + "1 AU00000 20000.00 ... Max Wentworth-Smith \n", + "2 AU00000 ... Max Wentworth-Smith \n", + "3 AU00000 ... Max Wentworth-Smith \n", + "\n", + " connection status class.type \\\n", + "0 5453f0a3-083c-43c6-9086-adfe94df9918 available mortgage \n", + "1 5453f0a3-083c-43c6-9086-adfe94df9918 available credit-card \n", + "2 5453f0a3-083c-43c6-9086-adfe94df9918 available savings \n", + "3 5453f0a3-083c-43c6-9086-adfe94df9918 available transaction \n", + "\n", + " class.product links.self \\\n", + "0 Hooli Home Loan /users/11103cba-4a08-4397-84a5-22ac125ed2f6/ac... \n", + "1 Hooli Visa /users/11103cba-4a08-4397-84a5-22ac125ed2f6/ac... \n", + "2 Hooli Saver /users/11103cba-4a08-4397-84a5-22ac125ed2f6/ac... \n", + "3 Hooli Transaction /users/11103cba-4a08-4397-84a5-22ac125ed2f6/ac... \n", + "\n", + " links.transactions links.connection \\\n", + "0 /users/11103cba-4a08-4397-84a5-22ac125ed2f6/tr... None \n", + "1 /users/11103cba-4a08-4397-84a5-22ac125ed2f6/tr... None \n", + "2 /users/11103cba-4a08-4397-84a5-22ac125ed2f6/tr... None \n", + "3 /users/11103cba-4a08-4397-84a5-22ac125ed2f6/tr... None \n", + "\n", + " links.institution links.statements \n", + "0 /institutions/AU00000 [] \n", + "1 /institutions/AU00000 [] \n", + "2 /institutions/AU00000 [] \n", + "3 /institutions/AU00000 [] \n", + "\n", + "[4 rows x 21 columns]\n" + ] + } + ], + "source": [ + "import pandas as pd\n", + "import json\n", + "\n", + "userid = \"11103cba-4a08-4397-84a5-22ac125ed2f6\"\n", + "\n", + "url = \"https://au-api.basiq.io/users/\"+userid+\"/accounts\"\n", + "\n", + "headers = {\n", + " \"accept\": \"application/json\",\n", + " \"authorization\": access_token,\n", + " }\n", + "\n", + "response = requests.get(url, headers=headers)\n", + "data = json.loads(response.text)\n", + "\n", + "Accounts = pd.json_normalize(data, record_path=['data'])\n", + "print(Accounts)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " type id status \\\n", + "0 transaction 5dc1cce7-a89d-45df-a069-0fb8ef4f9c8b posted \n", + "1 transaction a0a4a39f-62e0-4d7d-b0cc-f395601cb247 posted \n", + "2 transaction 95644266-e12a-4b9d-a40f-d0d0673ddad0 posted \n", + "3 transaction 6f39334c-988b-4d91-ad20-d7251a6b8ce7 posted \n", + "4 transaction 57ee3775-5aa8-42fc-9264-911a7b53b3d4 posted \n", + ".. ... ... ... \n", + "304 transaction b151aa25-3c29-4308-8dd9-b2bf998f10af posted \n", + "305 transaction 947fa446-1f36-417a-9fdd-fe6e3f02d499 posted \n", + "306 transaction b8c37fc4-21af-455f-a865-4f35cb7e74b0 posted \n", + "307 transaction 9b46141d-4ee2-4067-ab28-7aa7cc94f8a4 posted \n", + "308 transaction d31e41fe-f2f9-4a98-8cf4-0d88d318ee19 posted \n", + "\n", + " description amount \\\n", + "0 Transfer Platnm Homeloan 346454 -3927.50 \n", + "1 TFR From Transaction to CC Acc 13 -9331.00 \n", + "2 Payroll WFRMS 15439393 15985.00 \n", + "3 Wdl ATM BOQ BP Connect BalgowlaBalgowlahNS AU -900.00 \n", + "4 Non Hooli ATM Withdrawal Fee -2.50 \n", + ".. ... ... \n", + "304 TFR Acc14000 TO 12389 500.00 \n", + "305 Savings Interest CR Bal @0%% 106.15 \n", + "306 TFR Acc14000 TO 12389 500.00 \n", + "307 Savings Interest CR Bal @0%% 89.63 \n", + "308 TFR Acc14000 TO 12389 500.00 \n", + "\n", + " account balance direction \\\n", + "0 0b107c33-e858-4bbd-abba-f757ae4da1fc 6631.56 debit \n", + "1 0b107c33-e858-4bbd-abba-f757ae4da1fc 10559.06 debit \n", + "2 0b107c33-e858-4bbd-abba-f757ae4da1fc 19890.06 credit \n", + "3 0b107c33-e858-4bbd-abba-f757ae4da1fc 3905.06 debit \n", + "4 0b107c33-e858-4bbd-abba-f757ae4da1fc 4805.06 debit \n", + ".. ... ... ... \n", + "304 b2360b69-e3f3-49f3-bbc4-65d04865db02 25059.40 credit \n", + "305 b2360b69-e3f3-49f3-bbc4-65d04865db02 24559.40 credit \n", + "306 b2360b69-e3f3-49f3-bbc4-65d04865db02 24453.25 credit \n", + "307 b2360b69-e3f3-49f3-bbc4-65d04865db02 23953.25 credit \n", + "308 b2360b69-e3f3-49f3-bbc4-65d04865db02 23863.62 credit \n", + "\n", + " class institution ... enrich transactionDate \\\n", + "0 transfer AU00000 ... None \n", + "1 transfer AU00000 ... None \n", + "2 transfer AU00000 ... None \n", + "3 cash-withdrawal AU00000 ... None \n", + "4 bank-fee AU00000 ... None \n", + ".. ... ... ... ... ... \n", + "304 transfer AU00000 ... None \n", + "305 interest AU00000 ... None \n", + "306 transfer AU00000 ... None \n", + "307 interest AU00000 ... None \n", + "308 transfer AU00000 ... None \n", + "\n", + " postDate subClass \\\n", + "0 2022-09-28T00:00:00Z NaN \n", + "1 2022-09-27T00:00:00Z NaN \n", + "2 2022-09-27T00:00:00Z NaN \n", + "3 2022-09-25T00:00:00Z NaN \n", + "4 2022-09-25T00:00:00Z NaN \n", + ".. ... ... \n", + "304 2022-09-11T00:00:00Z NaN \n", + "305 2022-08-28T00:00:00Z NaN \n", + "306 2022-08-12T00:00:00Z NaN \n", + "307 2022-07-29T00:00:00Z NaN \n", + "308 2022-07-12T00:00:00Z NaN \n", + "\n", + " links.self \\\n", + "0 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "1 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "2 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "3 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "4 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + ".. ... \n", + "304 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "305 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "306 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "307 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "308 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "\n", + " links.account \\\n", + "0 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "1 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "2 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "3 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "4 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + ".. ... \n", + "304 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "305 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "306 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "307 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "308 https://au-api.basiq.io/users/11103cba-4a08-43... \n", + "\n", + " links.institution links.connection \\\n", + "0 https://au-api.basiq.io/institutions/AU00000 None \n", + "1 https://au-api.basiq.io/institutions/AU00000 None \n", + "2 https://au-api.basiq.io/institutions/AU00000 None \n", + "3 https://au-api.basiq.io/institutions/AU00000 None \n", + "4 https://au-api.basiq.io/institutions/AU00000 None \n", + ".. ... ... \n", + "304 https://au-api.basiq.io/institutions/AU00000 None \n", + "305 https://au-api.basiq.io/institutions/AU00000 None \n", + "306 https://au-api.basiq.io/institutions/AU00000 None \n", + "307 https://au-api.basiq.io/institutions/AU00000 None \n", + "308 https://au-api.basiq.io/institutions/AU00000 None \n", + "\n", + " subClass.title subClass.code \n", + "0 NaN NaN \n", + "1 NaN NaN \n", + "2 NaN NaN \n", + "3 NaN NaN \n", + "4 NaN NaN \n", + ".. ... ... \n", + "304 NaN NaN \n", + "305 NaN NaN \n", + "306 NaN NaN \n", + "307 NaN NaN \n", + "308 NaN NaN \n", + "\n", + "[309 rows x 21 columns]\n" + ] + } + ], + "source": [ + "userid = \"11103cba-4a08-4397-84a5-22ac125ed2f6\"\n", + "\n", + "url = \"https://au-api.basiq.io/users/\"+userid+\"/transactions?filter=transaction.postDate.bt('2022-07-01','2022-09-30')\"\n", + "\n", + "headers = {\n", + " \"accept\": \"application/json\",\n", + " \"authorization\": access_token,\n", + " }\n", + "\n", + "response = requests.get(url, headers=headers)\n", + "data = json.loads(response.text)\n", + "\n", + "Transactions = pd.json_normalize(data, record_path=['data'])\n", + "print(Transactions)\n", + "\n", + "Transactions.to_csv('Transactions_2022Q3', index = None, header=True)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}