Limit custom button from API call

I created a custom button that gets disabled once a value reaches a certain threshold and this works when i use it from the UI. However when i call the button from and API the disablement rules never get implemented. Is there a way to have the API trigger the same rule so i can’t use the button once the threshold is met?

We work around the problem by doing all validations in code.
Everything in the UI is nice-to-have, but the real validation happens in the StateMachine