TWRP API to aceess TWRP from the OS?
It's possible by OpenRecoveryScript

Official command guide: https://twrp.me/faq/openrecoveryscript.html

Official TWRP App, TWRP Manager and Lineage OS Updater use this approach.