87 lines
2.9 KiB
Text
87 lines
2.9 KiB
Text
---
|
|
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/master/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/master/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/master/docs/development/core/public/kibana-plugin-core-public.httpsetup.md)
|
|
</DocCallOut>
|
|
|
|
<DocCallOut>
|
|
See [the routing example plugin](https://github.com/elastic/kibana/blob/master/examples/routing_example) for more endpoint consumption examples.
|
|
</DocCallOut>
|