# Calling Elasticsearch API from Kibana plugin

**URL:** <https://discuss.elastic.co/t/calling-elasticsearch-api-from-kibana-plugin/343620>\
**Category:** Kibana\
**Tags:** painless, kibana-plugin-development\
**Created:** [September 22, 2023, 12:58pm UTC](https://discuss.elastic.co/t/calling-elasticsearch-api-from-kibana-plugin/343620 "2023-09-22T12:58:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trosagnant](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@trosagnant](https://discuss.elastic.co/u/trosagnant)\
**Post date:** [September 22, 2023, 12:58pm UTC](https://discuss.elastic.co/t/calling-elasticsearch-api-from-kibana-plugin/343620/1 "2023-09-22T12:58:54Z")

</div>

Follow-up for [#337003](https://discuss.elastic.co/t/making-api-post-request-in-kibana-plugin/337003)  
Similar to [#268179](https://discuss.elastic.co/t/call-elasticsearch-queries-from-custom-kibana-plugin/268179)

Hello,  
The main idea for the plugin I develop is to call elastic to alter some already existing index (just like using SQL UPDATE method).  
I'm trying to call elastic API through the route I have defined on the server side, followed up by `context.core.elasticsearch.client.asCurrentUser.updateByQuery` command.  
Unfortunately, when making a request from within the plugin, my browser returns Error 500: Internal Server Error and I cannot fully understand why. Neither Kibana nor Elasticsearch logs don't show any errors and I'm out of ideas how to debug this problem futher.  
I would be very grateful for any tips and insights about what I did wrong here.  
I'm developing for ver. 8.5.3 with intention of updating to the latest one later.

My code for the client side:

```auto
const onSubmitHandler = () => {
//extracting data from eui form
    const form = document.querySelector('form')!;
    const data = new FormData(form);
    const { startDate, endDate } = dateRange;
    const choice = data.get('index') as string;
    const field = data.get('field') as string;
    const value = data.get('new_value') as string;
    const body_array = [];
    body_array.push(choice,field,value,startDate,endDate);
//request to Kibana-Elastic API
    http.fetch('/api/my_plugin/_update_by_query', {
        method: 'POST',
        body: body_array,
      }
    ).then(r => {
      alert(r);
    }).catch(error => alert(error));
    return false;
  };

```

Server side:

```auto
router.post(
    {
      path: '/api/my_plugin/_update_by_query',
      validate: false,
    },

    async (context, request, response)=>{
      try {
        const { body } = request;
        const client = context.core.elasticsearch.client.asCurrentUser;

//calling a request with data passed from the client side
        const { body: result }= await client.updateByQuery({
          "index": request.body[0],
          "script": {
            "source": "ctx._source.kpi_fixed = params.fieldValue",
            "params": {
              "fieldName": request.body[1] ,
              "fieldValue": request.body[2]
            },
            "lang": "painless"
          },
          "query": {
            "range": {
              "@timestamp": {
                "format": "strict_date_optional_time",
                "gte": request.body[3] ,
                "lte": request.body[4]
              }
            }
          }
        });
       
        return response.ok({
          body: result,
        });
      }catch (e) {
        return response.customError(wrapError(e));
      }

    }
  )

```

---

<div class="post-metadata">

**Author:** ![jughosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jughosta/32/107160_2.png) [@jughosta](https://discuss.elastic.co/u/jughosta)\
**Post date:** [October 18, 2023, 1:38pm UTC](https://discuss.elastic.co/t/calling-elasticsearch-api-from-kibana-plugin/343620/2 "2023-10-18T13:38:46Z")

</div>

Hi @trosagnant,

As in the mentioned example, let's try to use this syntax:

```auto
const esClient = (await context.core).elasticsearch.client;
const { body: result }= await esClient.asCurrentUser.updateByQuery({
....

```

---

<div class="post-metadata">

**Author:** ![trosagnant](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@trosagnant](https://discuss.elastic.co/u/trosagnant)\
**Post date:** [November 8, 2023, 5:41pm UTC](https://discuss.elastic.co/t/calling-elasticsearch-api-from-kibana-plugin/343620/3 "2023-11-08T17:41:04Z")

</div>

Hello @jughosta thanks for answering.  
Unfortunately it didn't change anything, I still get the

> {"statusCode":500,"error":"Internal Server Error","message":"Cannot read properties of undefined (reading 'client')"}

Are there any other possible solutions?

---

<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:** [December 6, 2023, 5:41pm UTC](https://discuss.elastic.co/t/calling-elasticsearch-api-from-kibana-plugin/343620/4 "2023-12-06T17:41:52Z")

</div>

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