# Call custom ES api from inside plugin

**URL:** <https://discuss.elastic.co/t/call-custom-es-api-from-inside-plugin/105477>\
**Category:** Kibana\
**Created:** [October 26, 2017, 8:53pm UTC](https://discuss.elastic.co/t/call-custom-es-api-from-inside-plugin/105477 "2017-10-26T20:53:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![valereds](https://avatars.discourse-cdn.com/v4/letter/v/f19dbf/32.png) [@valereds](https://discuss.elastic.co/u/valereds)\
**Post date:** [October 26, 2017, 8:53pm UTC](https://discuss.elastic.co/t/call-custom-es-api-from-inside-plugin/105477/1 "2017-10-26T20:53:50Z")

</div>

Hi all, my need is to call a custom api like /elasticsearchip:port/path/to/api and pass two parameter in query string like ?parm1=value&param2=value. Which is the best and clean solution? I’m trying to make a server.route and use callWithRequest transport.request but just want an advice to find the most robust way to achieve that..(just because I’ve got always 500 internal server error when call api)..thank you

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [October 27, 2017, 1:51am UTC](https://discuss.elastic.co/t/call-custom-es-api-from-inside-plugin/105477/2 "2017-10-27T01:51:24Z")

</div>

To confirm, you have created an Elasticsearch API which adds a new endpoint you're attempting to query?

If I am following that, you have a few options off the top of my head.

You could use XMLHttpRequest, request, or whatever your flavor is and hit the endpoint directly.

Alternatively, you could create an elasticsearch-js plugin to add support for your new endpoint and create a new cluster with that plugin.

```auto
const config = Object.assign({ plugins: [myPlugin] }, server.config().get('elasticsearch'));
const cluster = server.plugins.elasticsearch.createCluster('whatever', config);

```

You can then obtain the cluster by using `server.plugins.elasticsearch.getCluster('whatever');` which contains callWithRequest.

Here is documentation on extending elasticsearch-js [https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/extending\_core\_components.html](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/extending_core_components.html)

I attempted to allow for adding plugins after client instantiation but ran into problems and out of time. Feel free to revive and see if you can resolve the issues if you're up to it: [https://github.com/elastic/elasticsearch-js/pull/482](https://github.com/elastic/elasticsearch-js/pull/482). This would have removed the need of creating a new cluster.

---

<div class="post-metadata">

**Author:** ![valereds](https://avatars.discourse-cdn.com/v4/letter/v/f19dbf/32.png) [@valereds](https://discuss.elastic.co/u/valereds)\
**Post date:** [November 2, 2017, 10:23am UTC](https://discuss.elastic.co/t/call-custom-es-api-from-inside-plugin/105477/3 "2017-11-02T10:23:17Z")

</div>

Hi @tylersmalley, thank you for your response.  
I ended using callWithRequest and transport.request of elasticsearch inside a server.route.

But now I've a problem, after calling the API the backend answer me after 30s and I've this:

```
> Request error, retrying
> POST http://elasticIP:9200/path/of/api => socket hang up

```

And my http request get an 504 Gatway timeout.  
I tried to set elasticsearch.requestTimeout but is seems to not solve my problem.  
I've also tried to set requestTimeout params inside the transport.request but the request hangs up always at 30s...very very strange

---

<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:** [November 30, 2017, 10:34am UTC](https://discuss.elastic.co/t/call-custom-es-api-from-inside-plugin/105477/4 "2017-11-30T10:34:19Z")

</div>

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