# Error creating rule via API

**URL:** <https://discuss.elastic.co/t/error-creating-rule-via-api/342381>\
**Category:** Kibana\
**Created:** [September 5, 2023, 10:36pm UTC](https://discuss.elastic.co/t/error-creating-rule-via-api/342381 "2023-09-05T22:36:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel\_Camara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_camara/32/125359_2.png) [@Gabriel\_Camara](https://discuss.elastic.co/u/Gabriel_Camara)\
**Post date:** [September 5, 2023, 10:36pm UTC](https://discuss.elastic.co/t/error-creating-rule-via-api/342381/1 "2023-09-05T22:36:37Z")

</div>

Hello, I'm trying to explain the example of creating a rule via API that is in the documentation, but I've been getting this error:

`{"statusCode":400,"error":"Bad Request","message":"Error creating rule: could not create API key - Unsupported scheme \"ApiKey\" for granting API Key"}`

My code:

```auto
import requests

header = { 
    'Content-Type': 'application/json;charset=UTF-8',
    'kbn-xsrf': 'true',
    'Authorization': 'ApiKey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}

data = {
  "params":{
      "aggType":"avg",
      "termSize":6,
      "thresholdComparator":">",
      "timeWindowSize":5,
      "timeWindowUnit":"m",
      "groupBy":"top",
      "threshold":[
         1000
      ],
      "index":[
         ".test-index"
      ],
      "timeField":"@timestamp",
      "aggField":"sheet.version",
      "termField":"name.keyword"
   },
   "consumer":"alerts",
   "rule_type_id":".index-threshold",
   "schedule":{
      "interval":"1m"
   },
   "actions":[
      {
         "id":"dceeb5d0-6b41-11eb-802b-85b0c1bc8ba2",
         "group":"threshold met",
         "params":{
            "level":"info",
            "message":"alert '{{alertName}}' is active for group '{{context.group}}':\n\n- Value: {{context.value}}\n- Conditions Met: {{context.conditions}} over {{params.timeWindowSize}}{{params.timeWindowUnit}}\n- Timestamp: {{context.date}}"
         }
      }
   ],
   "tags":[
      "cpu"
   ],
   "notify_when":"onActionGroupChange",
   "name":"my alert"
}
resp = requests.post('http://XXX.XXX.XXX.XXX:5601/api/alerting/rule', headers=header, json=data)
print(resp.text)

```

The rule json was copied from the documentation:

> **[Create rule API | Kibana Guide \[7.17\] | Elastic](https://www.elastic.co/guide/en/kibana/7.17/create-rule-api.html)**
>
> Kibana provides you with several options to share \*Discover\* saved searches, dashboards, \*Visualize Library\* visualizations, and \*Canvas\* workpads with others, or on a website.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [September 6, 2023, 12:56pm UTC](https://discuss.elastic.co/t/error-creating-rule-via-api/342381/2 "2023-09-06T12:56:38Z")

</div>

Hi @Gabriel_Camara,

Welcome to the community!

> [@Logstash SSL/TLS error](https://discuss.elastic.co/t/logstash-ssl-tls-error/342368/1):
>
> `Elasticsearch Unreachable: [https://30.21.332.568:9200/][Manticore::ClientProtocolException] PKIX path validation failed: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors`

I wonder if you're receiving a [similar error to this issue](https://discuss.elastic.co/t/api-bug-error-enabling-rule-could-not-create-api-key-unsupported-scheme-apikey-for-granting-api-key/285407). Can you try [authorization via a token](https://www.elastic.co/guide/en/kibana/current/api.html#token-api-authentication) instead to see if that request is valid.

---

<div class="post-metadata">

**Author:** ![Gabriel\_Camara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_camara/32/125359_2.png) [@Gabriel\_Camara](https://discuss.elastic.co/u/Gabriel_Camara)\
**Post date:** [September 6, 2023, 11:45pm UTC](https://discuss.elastic.co/t/error-creating-rule-via-api/342381/4 "2023-09-06T23:45:00Z")

</div>

Hi, @carly.richmond thanks for the welcome and the response.

Searching for this problem I found this link you sent, but even though I received the same error, they are different situations.

As we can see above, I'm using the token for the request and I keep getting this error.  
ps: I don't have SSL configuration.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 4, 2023, 11:45pm UTC](https://discuss.elastic.co/t/error-creating-rule-via-api/342381/5 "2023-10-04T23:45:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
