Register API Endpoints
In this example, we will register a public API endpoint that receives data from external HTTP requests. We will forward the data received from the endpoint to the
#general
channel.First of all, let's create a new Rocket.Chat app from scratch using
rc-apps create.
(If rc-apps
is an unrecognized command, please check out the Getting Started section to make initial preparations first).In the main
App
class, we need to implement the extendConfiguration
method, within which we use configuration.api.provideApi
to register a new API endpoint new Endpoint(this)
.1
// Main App Class
2
import { IAppAccessors, IConfigurationExtend, ILogger } from '@rocket.chat/apps-engine/definition/accessors';
3
import { ApiSecurity, ApiVisibility } from '@rocket.chat/apps-engine/definition/api';
4
import { App } from '@rocket.chat/apps-engine/definition/App';
5
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
6
7
import { Endpoint } from './endpoint';
8
9
export class RocketChatTester extends App {
10
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
11
super(info, logger, accessors);
12
}
13
14
public async extendConfiguration(configuration: IConfigurationExtend) {
15
// Register API endpoints
16
await configuration.api.provideApi({
17
visibility: ApiVisibility.PUBLIC,
18
security: ApiSecurity.UNSECURE,
19
endpoints: [new Endpoint(this)],
20
});
21
}
22
}
Notice that we imported an
Endpoint
class from another file. Now, let's create a new file named endpoint.ts
and implement the class Endpoint
we used above.1
// endpoint.ts
2
import { HttpStatusCode, IHttp, IModify, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors';
3
import { ApiEndpoint, IApiEndpointInfo, IApiRequest, IApiResponse } from '@rocket.chat/apps-engine/definition/api';
4
5
export class Endpoint extends ApiEndpoint {
6
public path = 'api';
7
8
public async post(
9
request: IApiRequest, endpoint: IApiEndpointInfo,
10
read: IRead,
11
modify: IModify,
12
http: IHttp,
13
persis: IPersistence,
14
): Promise<IApiResponse> {
15
const body = Object.entries(request.content)
16
.map(([key, value]) => `${key}: ${value}`)
17
.join('\n');
18
const room = await read.getRoomReader().getByName('general');
19
20
if (!room) {
21
return {
22
status: HttpStatusCode.NOT_FOUND,
23
content: `Room "#general" could not be found`,
24
};
25
}
26
27
const messageBuilder = modify.getCreator().startMessage()
28
.setText(body)
29
.setRoom(room);
30
const messageId = await modify.getCreator().finish(messageBuilder);
31
32
return this.success(JSON.stringify({ messageId }));
33
}
34
}
In the file
endpoint.ts
, we create a class that extends the base class ApiEndpoint
. We define the path of the API endpoint by assigning the value 'api'
to the public property path
.Then, let's implement the method
post
, which will be executed every time the API endpoint receives an HTTP POST
request from an external service. To forward any data received to the #general
channel, we obtain the request content by request.content
, create a message containing the request content, and then send it to the channel #general
.Deploy your app. You can check the complete endpoint URL you registered for the app on the App Info page.
%20(1).png?alt=media)
Open the terminal and use curl to post some data to the endpoint. The result should be as follows:
> curl --data 'Jack=Hello :)&Lucy=Hi!' -X POST http://localhost:3000/api/apps/public/bc4dd4a1-bf9b-408e-83a4-aba7eba0bf02/api
{"messageId":"dREmKaR7qHyN98rtZ"}
.png?alt=media)
Last modified 19d ago