res Instance
Response Object (res)¶
The res instance is used to send custom HTTP responses from webhook and Webapp commands.
It allows your bot to behave like an HTTP API or web endpoint.
Using res, you can return:
- JSON
- HTML
- XML
- Plain text
- Redirects
- Rendered templates (EJS)
Once a response is sent using res, execution ends.
Response Methods¶
set(key, value)¶
Sets HTTP headers on the response.
Headers can be chained.
status(code)¶
Sets the HTTP response status code.
You can chain this with other methods.
send(body)¶
Sends a response with any content type.
res.send("Hello World")
res.send({ message: "Success", data: { id: 1 } })
res.status(201).send("Resource created")
The content type is auto-detected.
json(obj)¶
Sends a JSON response with application/json content type.
This is the most common method for API-style responses.
html(content)¶
Sends an HTML response.
If EJS tags are detected, the content is automatically rendered.
With EJS rendering:
res.html(`
<h1>Welcome <%= user.first_name %></h1>
<p>Your ID: <%= user.id %></p>
<% if (user.premium) { %>
<div class='premium'>Premium User</div>
<% } %>
`)
xml(content)¶
Sends an XML response with application/xml content type.
text(content)¶
Sends a plain text response.
EJS templates are auto-rendered if detected.
res.text("This is plain text")
const textTemplate = "Hello <%= name %>, welcome!"
res.text(textTemplate)
redirect(url)¶
Redirects the request to another URL or command.
Redirect responses are sent immediately.
render(commandPath, options)¶
Renders another command’s output as the response.
Content type is detected automatically based on the target.
With options:
Rendering an HTML command:
Notes¶
- The
resinstance is available only in webhook and Webapp commands - Sending a response ends execution
- Headers, status, and body can be chained
- EJS rendering works automatically for HTML and text
- If no response is sent, a default 2xx response is returned