# How to get/use indexPatternService in server side api in kibana plugin

**URL:** <https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531>\
**Category:** Kibana\
**Created:** [June 19, 2019, 6:48pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531 "2019-06-19T18:48:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Riddhesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/riddhesh/32/47232_2.png) [@Riddhesh](https://discuss.elastic.co/u/Riddhesh)\
**Post date:** [June 19, 2019, 6:48pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531/1 "2019-06-19T18:48:38Z")

</div>

I want to use indexPatternService on server api side of plugin, e.g. to get fields for a pattern(`/api/index_patterns/_fields_for_wildcard`).

I know in server there is `indexPatternsServiceFactory`, but I don't know how to get service from it or how to use it.  
I was able to get savedObjectClient with below code(found it from kibana's source code), I know something similar is needed for indexPattern. Any example is very much appreciated.

```auto
init(server, options) {
  server.route({
    path: '/api/manage/something',
    method: 'GET',
    async handler(req, reply) {
      const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects;
      const { callWithInternalUser } = server.plugins.elasticsearch.getCluster(
        'admin'
      );
      const internalRepository = getSavedObjectsRepository(callWithInternalUser);
      const savedObjectClint = new SavedObjectsClient(internalRepository);

      reply("Hello World");
    }
  });
}

```

kibana version 6.4.2

Thanks!

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [June 19, 2019, 9:20pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531/2 "2019-06-19T21:20:18Z")

</div>

Not sure if it was available in 6.4.2, but have you tried accessing it from the `req` object?

```auto
const indexPatterns = req.getIndexPatternsService();

```

---

<div class="post-metadata">

**Author:** ![Riddhesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/riddhesh/32/47232_2.png) [@Riddhesh](https://discuss.elastic.co/u/Riddhesh)\
**Post date:** [June 19, 2019, 9:54pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531/3 "2019-06-19T21:54:24Z")

</div>

@lukas, `req.getIndexPatternsService()` works! don't know why I didn't print the req object! Thanks a lot. Also it has `getSavedObjectsClient()` as well.  
Although I've got the service I'm getting 401 authentication error if I try to do:

```auto
await req.getIndexPatternsService().getFieldsForWildcard('test')
or
await req.getSavedObjectsClient().get('index-pattern', 'abcd-1234-abcd')

```

How can we specify to use admin cluster, so it will use auth from kibana config to call es?

Thanks!

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [June 19, 2019, 11:12pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531/4 "2019-06-19T23:12:45Z")

</div>

Hmm, in that case you'll probably have to use the factory. Maybe this will work:

```auto
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
const indexPatterns = server.indexPatternsServiceFactory({ callCluster: callWithInternalUser });

```

---

<div class="post-metadata">

**Author:** ![Riddhesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/riddhesh/32/47232_2.png) [@Riddhesh](https://discuss.elastic.co/u/Riddhesh)\
**Post date:** [June 19, 2019, 11:16pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531/5 "2019-06-19T23:16:10Z")

</div>

@lukas, yes, this works! 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:** [July 17, 2019, 11:16pm UTC](https://discuss.elastic.co/t/how-to-get-use-indexpatternservice-in-server-side-api-in-kibana-plugin/186531/6 "2019-07-17T23:16:26Z")

</div>

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