# How to use Kibana API in plugin development

**URL:** <https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131>\
**Category:** Kibana\
**Created:** [December 7, 2021, 2:37pm UTC](https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131 "2021-12-07T14:37:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [December 7, 2021, 2:37pm UTC](https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131/1 "2021-12-07T14:37:37Z")

</div>

Hi there,

we're trying to develop a custom plugin and we're stuck when it comes to call a Kibana APIs.

We can import the **RequestHandlerContext** interface to provide a context we can use to call Elasticsearch APIs, but we didn't find anything useful inside context.core to make the code equivalent of the [following call](https://www.elastic.co/guide/en/kibana/current/index-patterns-api-get.html)

`curl -XGET "localhost:5601/api/index_patterns/index_pattern/<index_pattern_id>`

Our final goal would be to retrieve all the fields from each index pattern, which means retrieve all index patterns (we can use the `context.core.savedObjects.client.find({ type: 'index-pattern' })` call to achieve that) and for each one take its `id` and make the above call, extracting its fields.

Is it possible the function we're looking for is the `getFieldsForIndexPattern` defined in the `src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts` file? If so, how can I import it in my file to properly use it? It requires a param `indexPattern` of type `IndexPattern | IndexPatternSpec`, which leads me to "how do I instantiate a index pattern object? I may hopefully retrieve it from a `getIndexPattern()` method, passing the right `index_pattern_id` string, but all the functions with such a name/behavior I found are private and only usable inside the `src/plugins` subfolder.

Do I have to initialize my plugins taking into account the `src/plugins/data/...` plugins in order to use such calls?  
Thanks!

EDIT: I forgot to mention I'd like to call those APIs from the `server` side.

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [December 7, 2021, 4:21pm UTC](https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131/2 "2021-12-07T16:21:18Z")

</div>

Hi @Fabio-sama, inside Kibana plugin you shouldn't use REST API for this, but should use other plugins instead.

In this case you need to depend on `data` plugin which provides services for working with index patterns (they are called `data views` now in main branch).

Not sure if you've seen, but we have a bunch of examples plugins. We don't have many with server-side code though, but this one might be useful:

> **[kibana/examples/search\_examples/server at main · elastic/kibana](https://github.com/elastic/kibana/tree/main/examples/search_examples/server)**
>
> main/examples/search\_examples/server

To run kibana with these plugins start development with: `yarn start --run-examples` and you will have `Developer Examples` app in a side menu

Similar how that examples plugins uses data.search server side, you'll need to use data.indexPatterns

> <https://github.com/elastic/kibana/blob/44c19bd2ca94810ef8fdc88aa07e3e73aff9e666/src/plugins/data/server/plugin.ts#L115>

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [December 7, 2021, 4:24pm UTC](https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131/3 "2021-12-07T16:24:14Z")

</div>

This is how index pattern rest API is implemented. You can use it as an example on how to use index pattern service server-side:

> <https://github.com/elastic/kibana/blob/main/src/plugins/data_views/server/routes/get_index_pattern.ts>

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [December 7, 2021, 4:55pm UTC](https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131/4 "2021-12-07T16:55:32Z")

</div>

Hi there,

first of all thank you for your reply.

Yes I didn't mean to directly use the REST API. What I meant was to find the right plugin (object, interface, whatever) which could return me what the API call returns (to be more specific, the index pattern fields).

I think I might have sorted it out. I was looking in the wrong path since I was looking in the `src/plugins/data/public` for something I need to use in server side.

What I need was in `src/plugins/data/server/` and was the `IndexPatternFetcher` class, which takes the Elasticsearch client in its constructor (which I have in server side from `context.core.elasticsearch.client.asCurrentUser`) and has the `getFieldsForWildcard` function which does exactly what I need (it takes the index\_pattern.attributes.title and not the index\_pattern.attributes.id but it's ok).

So thank you so much for you interest.  
I'll test my solution a little bit more before mark it as Solution.

---

<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:** [January 4, 2022, 4:55pm UTC](https://discuss.elastic.co/t/how-to-use-kibana-api-in-plugin-development/291131/5 "2022-01-04T16:55:52Z")

</div>

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