Automating IP blocking in firewall rules

Is there any API which we can use to automate blocking of IPs in firewall rules.

Yes. There’s a separate “Filters” API for this:
https://developers.cloudflare.com/firewall/api/cf-filters