-
Notifications
You must be signed in to change notification settings - Fork 14
Simple API Endpoint
Objective: Register a example_api endpoint that, when receiving a payload, will output that to the #GENERAL channel. If there is no #GENERAL channel, it should return HTTP STATUS CODE 404.
Author: Duda Nogueira
We first need to register this API from the main App Class (DemoAppApp) method extendConfiguration:
public async extendConfiguration(configuration: IConfigurationExtend) {
// Providing API Endpoints
await configuration.api.provideApi({
visibility: ApiVisibility.PUBLIC,
security: ApiSecurity.UNSECURE,
endpoints: [new ExampleEndpoint(this)],
});Now that your API is registered, let's check it out. If you go to your App Page, under the Details tab, you can already see it there:

Great! if you execute the curl command, this will happen:
$ curl -X POST http://localhost:3000/api/apps/public/67a4e34b-5c04-4c9c-9358-3d0fd217cefd/example_api
$ {"success":true,"messageId":"44sYZx3pSjpDpbZpY"}Nice! If we check our #GENERAL channel:

Don't panic. This is the desired behavior 😄
We designed our API to output any payload to #GENERAL, so let's give it a try:
curl --request POST --url http://localhost:3000/api/apps/public/67a4e34b-5c04-4c9c-9358-3d0fd217cefd/example_api --data '{"some": "payload","other": {"more": "payload","list": [1,2,3,4]}}'
Oh, nice! Now we got the payload.

You can also store those payloads inside Rocket.Chat using it's robust persistence capabilities or register other methods for that endpoint, like GET, DELETE, PUT, etc. But that's another demo 🙈