# How to create a default index pattern via python api

**URL:** https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085
**Category:** Kibana
**Created:** [July 29, 2020, 3:16pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085 "2020-07-29T15:16:18Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![momora9809](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@momora9809](https://discuss.elastic.co/u/momora9809)
#### Post date: [July 29, 2020, 3:16pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/1 "2020-07-29T15:16:18Z")

</div>

Hi,  
I saw [How to create a default index pattern via kibana api](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-kibana-api/238905) that used Kibana API.  
I wanted to use python, but didn't know what uri should I use.

```auto
header = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
uri = ?
query = json.dumps({"attributes":{"defaultIndex": "myindex"}})
r = requests.post(uri, headers=header, data=query).json()

```

I'm using both Elastic and Kibana locally on Windows.  
Thanks

---

<div class="post-metadata">

### Author: ![Liza\_Katz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liza_katz/32/52358_2.png) [@Liza\_Katz](https://discuss.elastic.co/u/Liza_Katz)
#### Post date: [July 29, 2020, 4:21pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/2 "2020-07-29T16:21:31Z")

</div>

The API for creating an index pattern is described [here](https://www.elastic.co/guide/en/kibana/current/saved-objects-api-create.html).

So it would be something like `https://my-kibana:port/api/saved_objects/index-pattern/my-pattern`.

Let me know if this helps.

---

<div class="post-metadata">

### Author: ![damasu](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@damasu](https://discuss.elastic.co/u/damasu)
#### Post date: [July 29, 2020, 4:54pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/3 "2020-07-29T16:54:25Z")

</div>

all the endpoints that i presented in my post can easily be used directly with urlllib.

---

<div class="post-metadata">

### Author: ![momora9809](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@momora9809](https://discuss.elastic.co/u/momora9809)
#### Post date: [July 29, 2020, 6:03pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/4 "2020-07-29T18:03:56Z")

</div>

It didn't.  
(Just to clarify: I know how to create an index pattern; I want to set an index pattern as the default).

The header and query stayed same, just changing the uri:

```auto
HEADERS = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
query = json.dumps({'attributes':{'defaultIndex': 'my_index_pattern'}})
r = requests.post(uri, headers=HEADERS, data=query).json()

```

```auto
uri = "http://localhost:5601/s/api/saved_objects/index-pattern/my_index_pattern"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}

```

```auto
uri = "http://localhost:9200/s/api/saved_objects/index-pattern/my_index_pattern"
Error: {'error': 'no handler found for uri [/s/api/saved_objects/index-pattern/stock_tweets] and method [POST]'}

```

```auto
uri = "http://localhost:5601/s/api/saved_objects/config/7.8.0"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}

```

```auto
uri = "http://localhost:9200/s/api/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/s/api/saved_objects/config/7.8.0] and method [POST]'}

```

```auto
uri = "http://localhost:5601/api/saved_objects/config/7.8.0"
Error: {'statusCode': 409, 'error': 'Conflict', 'message': '[config:7.8.0]: version conflict, document already exists (current version [2]): [version_conflict_engine_exception] [config:7.8.0]: version conflict, document already exists (current version [2]), with { index_uuid="O0QpGH7BSIqBdpyq9r3vCA" & shard="0" & index=".kibana_2" }'}

```

```auto
uri = "http://localhost:9200/api/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/api/saved_objects/config/7.8.0] and method [POST]'}

```

```auto
uri = "http://localhost:5601/.kibana/api/saved_objects/config/7.8.0"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}

```

```auto
uri = "http://localhost:9200/.kibana/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/.kibana/saved_objects/config/7.8.0] and method [POST]'}

```

```auto
uri = "http://localhost:5601/.kibana/saved_objects/config/7.8.0"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}

```

```auto
uri = "http://localhost:9200/.kibana/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/.kibana/saved_objects/config/7.8.0] and method [POST]'}

```

```auto
uri = "http://localhost:9200/.kibana/config/7.8.0"
Error: {'error': {'root_cause': [{'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_2] as the final mapping would have more than 1 type: [_doc, config]'}], 'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_2] as the final mapping would have more than 1 type: [_doc, config]'}, 'status': 400}

```

```auto
uri = "http://localhost:9200/.kibana/api/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/.kibana/api/saved_objects/config/7.8.0] and method [POST]'}

```

---

<div class="post-metadata">

### Author: ![momora9809](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@momora9809](https://discuss.elastic.co/u/momora9809)
#### Post date: [July 29, 2020, 6:21pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/5 "2020-07-29T18:21:41Z")

</div>

Will u explain how should I use

```auto
/s/<some space>/api/saved_objects/config/7.8.0

```

in Python? (Similar to those I've written)

---

<div class="post-metadata">

### Author: ![damasu](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@damasu](https://discuss.elastic.co/u/damasu)
#### Post date: [July 29, 2020, 7:14pm UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/6 "2020-07-29T19:14:29Z")

</div>

if you re-read what i had written in the post you would see that you need to call

```auto
http://kibana:5601/s/<some space>/api/saved_objects/index-pattern/dummy_index_pattern
http://kibana:5601/s/<some space>/api/saved_objects/index-pattern/7.8.0

```

you do not need to call an elasticsearch endpoint.

regardless of the language you use to write an API call the above URL is what you should be using.  
`<some space>` is whatever space you have created. assuming you have created a space. if not do so or remove `/s/<some space>` from the URL. it all depends on what you are trying to do. In my case, i made a default index for specific spaces. As well, i am using 7.8.0 i do not know what version of kibana you have.

---

<div class="post-metadata">

### Author: ![momora9809](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@momora9809](https://discuss.elastic.co/u/momora9809)
#### Post date: [July 30, 2020, 2:45am UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/7 "2020-07-30T02:45:10Z")

</div>

I'm using 7.8.0 too.

I tried

```auto
HEADERS = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
query = json.dumps({'attributes':{'defaultIndex': 'my_index_pattern'}})
uri = "http://localhost:5601/api/saved_objects/index-pattern/7.8.0"
r = requests.post(uri, headers=HEADERS, data=query).json()

Error: {'statusCode': 400, 'error': 'Bad Request', 'message': 'mapping set to strict, dynamic introduction of [defaultIndex] within [index-pattern] is not allowed: [strict_dynamic_mapping_exception] mapping set to strict, dynamic introduction of [defaultIndex] within [index-pattern] is not allowed'}

```

But I have not set the `dynamic` field to `false` or `strict`.  
What should I do?

Btw, you said

```auto
/s/<some space>/api/saved_objects/config/7.8.0

```

not

```auto
/s/<some space>/api/saved_objects/index-pattern/7.8.0

```

(I don't know if the two are the same or not)

---

<div class="post-metadata">

### Author: ![momora9809](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@momora9809](https://discuss.elastic.co/u/momora9809)
#### Post date: [July 30, 2020, 3:00am UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/8 "2020-07-30T03:00:52Z")

</div>

I noticed something odd with the version when I exported my\_index\_pattern:

```auto
{"attributes": ... ,"migrationVersion":{"index-pattern":"7.6.0"},"references":[],"type":"index-pattern","version":"WzIxOTYsMV0="}

```

But I was using 7.8.0 all along! Is that the problem?!

---

<div class="post-metadata">

### Author: ![damasu](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@damasu](https://discuss.elastic.co/u/damasu)
#### Post date: [July 30, 2020, 10:31am UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/9 "2020-07-30T10:31:29Z")

</div>

sorry you are right try without index-pattern and i think you have version 7,6,0. please confirm that and use the correct version with the call.

---

<div class="post-metadata">

### Author: ![momora9809](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@momora9809](https://discuss.elastic.co/u/momora9809)
#### Post date: [July 30, 2020, 11:42am UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/10 "2020-07-30T11:42:36Z")

</div>

Elastic is 7.8.0, couldn't find anything about Kibana (I downloaded them at the same time around 3 weeks ago). The only thing I could find was " Welcome to Stack Management 7.8.0". Ok?

And about my question,  
I tried this:

```auto
HEADERS = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
query = json.dumps({'attributes':{'defaultIndex': 'my_index_pattern'}})
uri = "http://localhost:9200/.kibana_4/saved_objects/7.8.0"
r = requests.post(uri, headers=HEADERS, data=query).json()

```

And the response was:

```auto
{'_index': '.kibana_4', '_type': 'saved_objects', '_id': '7.8.0', '_version': 3, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}

```

But it's not the default one yet.  
What am I doing wrong?

I was getting this error when I used `.kibana`, so I switched to `.kibana_4`:

```auto
{'error': {'root_cause': [{'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_3] as the final mapping would have more than 1 type: [_doc, saved_objects]'}], 'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_3] as the final mapping would have more than 1 type: [_doc, saved_objects]'}, 'status': 400}

```

Why do I have different `.kibana` s?! (.kibana, .kibana\_1 to 4)

---

<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: [August 27, 2020, 11:42am UTC](https://discuss.elastic.co/t/how-to-create-a-default-index-pattern-via-python-api/243085/11 "2020-08-27T11:42:38Z")

</div>

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