api.attachWidgetAction is the way to go. You can read search the codebase and you’ll be gtg.
api.attachWidgetAction
Examples here