Programmatically merge contact records?

Is it possible to programmatically merge contacts together if they have the same email address?

We have a bunch of custom modules which feed data into suitecrm through API integrations and each contact entity is created separately instead of matching with an existing contact if there is one. We would like to clean this up. Firstly in the integration of the APIs to find the contact first, and secondly by cleaning up our existing contact records (~ 100k records) and merging contacts together.

Any help or insight is appreciated. Thanks!