Synchronize to any data store
A common use case for using SmoothIntegration is to synchronise data from your accounting system to your own data store. Whether that is a database, or a simple excel file, the implementation is very similar. This guide will show you how to sync data into an Excel file using Python, but this can very easily be adapted to other languages or other destinations like a database.
Pre requisites
This guide assumes you've setup your SmoothIntegration account, have an active connection. If this is not the case yet, then please follow the Getting Started and Create a Connection guides first.
Setting up the Python project
For this project we will need to install two dependencies
- openpyxl, a popular library to read and write Excel files
- smoothintegration, the SmoothIntegration Python SDK
pip install openpyxl smoothintegration
Next we will need to create a new Python file, in this example we will call it sync.py . Here we will setup the SmoothIntegration client.
import smoothintegration smoothintegration.client_id = "<your_client_id>" smoothintegration.client_secret = "<your_client_secret>"
Retrieving the Events
Next we will use the CDC API to pull in all invoice events. For now we just print them
for invoice_event in smoothintegration.data.cdc.stream( from_event_id='0', event=['invoice'], wait_for_new_events=False, ): print(invoice_event['document'])
We pass from_event_id as 0 to import all invoice events ever.
event is set to ['invoice'] so that we only receive invoices. You can add more events like invoice_credit bill or bill_credit as well if you want.
wait_for_new_events is set to False . This tells the SDK to stop polling when the last event is reached.
You can test the code so far by running the following command in your terminal. You should see invoices being printed when you do.
python sync.py
Creating the Excel file
Next let's create an Excel file to which we will write the data using the openpyxl library.
from openpyxl import Workbook wb = Workbook() ws = wb.active # Create the header row ws.append(['Number', 'Status', 'Total', 'Issue Date', 'Customer'])Instead of printing the incoming events, we will now add them to the excel document instead.
ws.append([ invoice_event['document']['number'], invoice_event['document']['status'], invoice_event['document']['total'], invoice_event['document']['issue_date'][:10], invoice_event['document']['customer']['name'], ])
And finally let's save the Excel file.
wb.save("invoices.xlsx")