An API endpoint is a location from which APIs can access the resources they need to carry out their function or can post data to it.
In this recipe, we will register a public API endpoint that receives data from external HTTP requests. We will try to forward the data received from the endpoint to the #general channel.
Register an API endpoint
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).
In the file endpoint.ts, we created a class that extends the base class ApiEndpoint. We defined 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 once the API endpoint receives an HTTP POST request from an external service. Following our original idea - forwarding any data received to the #general channel, we obtain the request content by request.content and create a message containing the request content, then send it to the channel #general.
Test the result
Using the CLI command rc-apps deploy to deploy the app to your Rocket.Chat server first. Navigate to Rocket.Chat Admin -> Apps -> Apps Detailed Page to check the full endpoint URL you registered for the App.
Open the terminal and use curl post some data to the endpoint. The result should be like below: