kibana/dev_docs/tutorials/endpoints.mdx
Tyler Smalley c4815d319e
Updates Github link references from master to main (#116789)
Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
2021-10-29 09:53:08 -07:00

88 lines
2.9 KiB
Plaintext

---
id: kibDevTutorialServerEndpoint
slug: /kibana-dev-docs/tutorials/registering-endpoints
title: Registering and accessing an endpoint
summary: Learn how to register a new endpoint and access it
date: 2021-10-05
tags: ['kibana', 'dev', 'architecture', 'tutorials']
---
## Registering an endpoint
The server-side `HttpService` allows server-side plugins to register endpoints with built-in support for request validation. These endpoints may be used by client-side code or be exposed as a public API for users. Most plugins integrate directly with this service.
The service allows plugins to:
- to extend the Kibana server with custom HTTP API.
- to execute custom logic on an incoming request or server response.
- to implement custom authentication and authorization strategy.
<DocCallOut>
See [the server-side HTTP service API docs](https://github.com/elastic/kibana/blob/main/docs/development/core/server/kibana-plugin-core-server.httpservicesetup.md)
</DocCallOut>
**Registering a basic GET endpoint**
```ts
import { schema } from '@kbn/config-schema';
import type { CoreSetup, Plugin } from 'kibana/server';
export class MyPlugin implements Plugin {
public setup(core: CoreSetup) {
const router = core.http.createRouter();
const validate = {
params: schema.object({
id: schema.string(),
}),
};
router.get({
path: '/api/my_plugin/{id}',
validate
},
async (context, request, response) => {
const data = await findObject(request.params.id);
if (!data) return response.notFound();
return response.ok({
body: data,
headers: {
'content-type': 'application/json'
}
});
});
}
}
```
<DocCallOut>
See [the routing example plugin](https://github.com/elastic/kibana/blob/main/examples/routing_example) for more route registration examples.
</DocCallOut>
## Consuming the endpoint from the client-side
The client-side HTTP service provides an API to communicate with the Kibana server via HTTP interface.
The client-side `HttpService` is a preconfigured wrapper around `window.fetch` that includes some default behavior and automatically handles common errors (such as session expiration).
**The service should only be used for access to backend endpoints registered by the same plugin.** Feel free to use another HTTP client library to request 3rd party services.
```ts
import { HttpStart } from 'kibana/public';
interface ResponseType {…};
async function fetchData(http: HttpStart, id: string) {
return await http.get<ResponseType>(
`/api/my_plugin/${id}`,
{ query: … },
);
}
```
<DocCallOut>
See [the client-side HTTP service API docs](https://github.com/elastic/kibana/blob/main/docs/development/core/public/kibana-plugin-core-public.httpsetup.md)
</DocCallOut>
<DocCallOut>
See [the routing example plugin](https://github.com/elastic/kibana/blob/main/examples/routing_example) for more endpoint consumption examples.
</DocCallOut>