How can I specify the branches for a merge request approval rule?

Problem to solve

I would like to create merge request approval rules via the API. Unfortunately, I’m only able to create rule for all branches, not for specific ones. the api docs do not mention any way to specify the branch which should be protected by the new rule:
https://archives.docs.gitlab.com/17.11/api/merge_request_approvals/#create-project-approval-rule

Versions

GitLab v17.11.4-ee

Please add an x whether options apply, and add the version information.

  • Self-managed
  • GitLab.com SaaS
  • Dedicated

I found a solution

project = gl.projects.get("my-project")
main_branch = project.protectedbranches.get("main")
project.approvalrules.create(
                {
                    "name": rule_name,
                    "approvals_required": 1,
                    "rule_type": "regular",
                    "protected_branch_ids": [main_branch_id.id],
                    "group_ids": approver_group_ids,
                }
            )