Skip to content

Simple API Endpoint

Duda Nogueira edited this page Dec 23, 2022 · 6 revisions

About

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

relevant code

API Registration

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)],
    });

Consuming a registered API

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:

image

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:

image

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.

image

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 🙈

Clone this wiki locally