# How do I create policies via API

**URL:** https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781
**Category:** Miscellanaeous
**Created:** [May 31, 2022, 3:54am UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781 "2022-05-31T03:54:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kaikiat](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/kaikiat/32/5923_2.png) [@kaikiat](https://forum.keycloak.org/u/kaikiat)
#### Post date: [May 31, 2022, 3:54am UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781/1 "2022-05-31T03:54:06Z")

</div>

Hello team,

How do I create policies (i.e User policies, group policies) programmatically via APIs.

I found a apis related to created polices, but none of them seems to create (POST) a policy.

GET /{realm}/client-policies/policies  
PUT /{realm}/client-policies/policies

---

<div class="post-metadata">

### Author: ![karthik.pon](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@karthik.pon](https://forum.keycloak.org/u/karthik.pon)
#### Post date: [May 31, 2022, 9:20am UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781/2 "2022-05-31T09:20:04Z")

</div>

Hi,

If you want to use Rest directly, check keycloak admin gui - it calls Rest api behind the scenes. Just do Inspect in browser and then check network traffic

refer this link [Keycloak Authorization APIs to create Policies, Permissions and Resources](http://forum.keycloak.org/t/keycloak-authorization-apis-to-create-policies-permissions-and-resources/15496)

---

<div class="post-metadata">

### Author: ![kostanos](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/kostanos/32/5982_2.png) [@kostanos](https://forum.keycloak.org/u/kostanos)
#### Post date: [October 11, 2022, 3:37pm UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781/3 "2022-10-11T15:37:42Z")

</div>

I’m looking for the same. I able to create a postman with multiple requests, like get list of users/logig etc.  
But straggling with the creating policy call.  
So far I found that I need to do a POST to:  
{{authUrl}}/admin/realms/tyk/clients/{{clientId}}/authz/resource-server/policy/role  
With JSON body:

```auto
{
    "type": "role",
    "logic": "POSITIVE",
    "decisionStrategy": "UNANIMOUS",
    "name": "Admin role policy",
    "description": "Only admin role policy",
    "roles": [
        {
            "id": "b026ef11-6a6d-44da-a477-12d483cc6ef4",
            "required": true
        }
    ]
}

```

At least this call I see from gui. But in postman I always getting: error: “unknow\_error” 403 Forbidden

Am I missing some previous call to obtain some extra authorization token apart of login?

---

<div class="post-metadata">

### Author: ![MuratKaragozgil](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/muratkaragozgil/32/7414_2.png) [@MuratKaragozgil](https://forum.keycloak.org/u/MuratKaragozgil)
#### Post date: [January 12, 2023, 7:46am UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781/4 "2023-01-12T07:46:56Z")

</div>

Is there any solution to this problem? Because I’m trying to use keycloak-admin-client library to create policies programmatically, but there is no way to do it…

---

<div class="post-metadata">

### Author: ![nabim777](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/nabim777/32/11093_2.png) [@nabim777](https://forum.keycloak.org/u/nabim777)
#### Post date: [September 3, 2024, 6:20am UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781/5 "2024-09-03T06:20:26Z")

</div>

For me below curl commands works:

**commands:**

```bash
curl -X POST https://keycloak.local/admin/realms/opendesk/clients/<realm-management-id>/authz/resource-server/policy/client \
--header "Authorization: Bearer ""$MASTER_TOKEN" \          
--header "Content-Type: application/json" \
 --data '{"clients":["<id-of-client>"],
"logic":"POSITIVE",                
"name":"<name-of-policy>",                                     
"description":"<description>"}'

```

**Output/Response:**

```bash
{"id":"baedXXXX-XXX-XXXX-XXXX-2c72b304f7a3","name":"<client-id>","description":"","type":"client","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","clients":["<id-of-client>"]}

```

---

<div class="post-metadata">

### Author: ![hassaanq](https://avatars.discourse-cdn.com/v4/letter/h/41988e/32.png) [@hassaanq](https://forum.keycloak.org/u/hassaanq)
#### Post date: [May 31, 2025, 6:41am UTC](https://forum.keycloak.org/t/how-do-i-create-policies-via-api/15781/6 "2025-05-31T06:41:19Z")

</div>

You need to set the appropriate permissions for the confidential client whose token you are using:

Under the Service Accounts Roles for that client, click on Assign Role and then select the relevant “client” roles. This should resolve the 403 error. I’m not sure exactly which roles are relevant, but as a starting point, you could assign it ALL the available roles (this is not a safe practice though, you would need to identify which roles are relevant.
