# Saved objects API use example

**URL:** <https://discuss.elastic.co/t/saved-objects-api-use-example/168742>\
**Category:** Kibana\
**Created:** [February 17, 2019, 5:15pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742 "2019-02-17T17:15:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pup\_seba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pup_seba/32/42988_2.png) [@pup\_seba](https://discuss.elastic.co/u/pup_seba)\
**Post date:** [February 17, 2019, 5:15pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742/1 "2019-02-17T17:15:21Z")

</div>

Hi,

I'm trying to restore an export but I can't get it right. The export was done via webUI, and the result is 1 file in .json format with all the info about dashboards, visualizations, etc.

I'm trying to use the saved object API to restore it, but I don't know how to use it. I tried things like  
using the dev tools in Kibana and excecuting this in there:  
POST api/saved\_objects/\_bulk\_create  
(copy/paste of the content of the previously exported json)

But I keep getting these errors:  
{  
"error": {  
"root\_cause": [  
{  
"type": "parse\_exception",  
"reason": "request body is required"  
}  
],  
"type": "parse\_exception",  
"reason": "request body is required"  
},  
"status": 400  
}

What am I doing wrong? Could you please show me some example of how to do this right?

Thank you.  
edit: I just saw this in the front page of the API documentation "  
You cannot access these endpoints via the Console in Kibana." So I guess that my error is trying to use the dev tools. Could you tell me how to do this with any other tool please?

---

<div class="post-metadata">

**Author:** ![pup\_seba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pup_seba/32/42988_2.png) [@pup\_seba](https://discuss.elastic.co/u/pup_seba)\
**Post date:** [February 17, 2019, 6:40pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742/2 "2019-02-17T18:40:18Z")

</div>

Hi,

I'm now able to do this via ansible and uri module.  
---  
- name: importa info para la demo  
hosts: kibana

```
  tasks:
   - name: importa info para la demo
     uri:
       url: http://localhost:5601/api/saved_objects/_bulk_create
       method: POST
       body: "{{ lookup('file','../files/test.json') }}"
       body_format: json
       headers:
         kbn-xsrf: true

```

But only whtn the content of the file is the one of the example given in the elastic kb artcile.  
[  
{  
"type": "index-pattern",  
"id": "my-pattern",  
"attributes": {  
"title": "my-pattern-\*"  
}  
},  
{  
"type": "dashboard",  
"id": "my-dashboard",  
"attributes": {  
"title": "Look at my dashboard"  
}  
}  
]

If i use the export.json created by the "export" button on the Kibana webUI, I get this error (only showing an extract as real output is way too big for copying/pasting it here:  
fatal: [elasticstack01.essi.lab]: FAILED! =\> {"cache\_control": "no-cache", "changed": false, `  
"connection": "close", "content": "{"statusCode":400,"error":"Bad Request","message":"\"value\" at position 0 fails because [child \"type\" fails because [\"type\" is required], child \"attributes\" fails because [\"attributes\" is required], \"\_type\" is not allowed, \"\_id\" is not allowed, \"\_meta\" is not allowed, \"\_source\" is not allowed]. \"value\" at position 1 fails because [child \"type\" fails because [\"type\" is required], child \"attributes\" fails because [\"attributes\" is required], \"\_type\" is not allowed, \"\_id\" is not allowed, \"\_meta\" is not allowed, \"\_source\" is not allowed]. \"value\" at position 2 fails because [child \"type\" fails because [\"type\" is required], child \"attributes\" fails because [\"attributes\" is required], \"\_type\" is not allowed, \"\_id\" is not allowed, \"\_meta\" is not allowed,

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [February 18, 2019, 6:37pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742/3 "2019-02-18T18:37:31Z")

</div>

@mikecote can we please get some help here?

Thanks,  
Bhavya

---

<div class="post-metadata">

**Author:** ![mikecote](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikecote/32/58549_2.png) [@mikecote](https://discuss.elastic.co/u/mikecote)\
**Post date:** [February 19, 2019, 2:46pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742/4 "2019-02-19T14:46:47Z")

</div>

Hi @pup_seba ,

The reason you are getting these errors is because there currently isn’t a server side API to import the file directly. At this time the import logic is in the web UI, it reads the file, transforms the objects and calls the create API one object at a time.

As a workaround to get your file working with the bulk\_create API, you will have to replicate the front end logic and transform the attributes to what is expected by the bulk\_create API.

Example:

```auto
[{
  "_type": "index-pattern",
  "_id": "my-pattern",
  "_source": {
    "title": "my-pattern-*"
  }
}]

```

to

```auto
[{
  "type": "index-pattern",
  "id": "my-pattern",
  "attributes": {
    "title": "my-pattern-*"
  }
}]

```

If you are having id conflicts, you can also use the `overwrite` query parameter ([https://www.elastic.co/guide/en/kibana/6.6/saved-objects-api-bulk-create.html#\_query\_parameters\_3](https://www.elastic.co/guide/en/kibana/6.6/saved-objects-api-bulk-create.html#_query_parameters_3)).

We are currently working on a server side import and export API that would accept files directly but in the meantime this is the best workaround I can think of if the web UI import isn't suitable.

Hope this helps,  
Mike.

---

<div class="post-metadata">

**Author:** ![pup\_seba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pup_seba/32/42988_2.png) [@pup\_seba](https://discuss.elastic.co/u/pup_seba)\
**Post date:** [February 19, 2019, 3:52pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742/5 "2019-02-19T15:52:49Z")

</div>

Ohh ok. This is great 🙂 I did edit the file to supress the preciding "\_", but did not changed the "\_source" for "attributes" (I did not know I could do that and still work 🙂 ) so I was giving up already 😃

I'll give it another try.

Thank you very much!

---

<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:** [March 19, 2019, 3:52pm UTC](https://discuss.elastic.co/t/saved-objects-api-use-example/168742/6 "2019-03-19T15:52:57Z")

</div>

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