mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 02:09:32 -04:00
Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co> # Conflicts: # .github/ISSUE_TEMPLATE/v8_breaking_change.md # .github/PULL_REQUEST_TEMPLATE.md # dev_docs/contributing/best_practices.mdx # dev_docs/key_concepts/anatomy_of_a_plugin.mdx # docs/developer/contributing/interpreting-ci-failures.asciidoc # examples/routing_example/public/app.tsx # legacy_rfcs/text/0005_route_handler.md # legacy_rfcs/text/0006_management_section_service.md # legacy_rfcs/text/0015_bazel.md # packages/kbn-spec-to-console/README.md # src/dev/code_coverage/docs/team_assignment/README.md # src/plugins/embeddable/README.asciidoc
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/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>
|