Error creating rule via API

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:

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:

Hi @Gabriel_Camara,

Welcome to the community!

I wonder if you're receiving a similar error to this issue. Can you try authorization via a token instead to see if that request is valid.

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.

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