Defining Raw Endpoints
Drop down in abstraction to access the raw HTTP request
Sometimes you need to operate a lower abstraction than Encore.ts normally provides. For example, you might want to access the underlying HTTP request, often useful for things like accepting webhooks.
Encore.ts has you covered using "raw endpoints".
To define a raw endpoint, use the api.raw
function. It works similarly to
api
, but does not accept a request and response schema. Instead, it works like
the Node.js http
module and Express.js
, where the function receives two
parameters: a request object and a response writer.
It looks like this:
import { api } from "encore.dev/api";
export const myRawEndpoint = api.raw(
{ expose: true, path: "/raw", method: "GET" },
async (req, resp) => {
resp.writeHead(200, { "Content-Type": "text/plain" });
resp.end("Hello, raw world!");
},
);
It can be called like so:
$ curl http://localhost:4000/rawHello, raw world!
Related example
encoredev/examples/slack-bot
Slack Bot example application that uses Raw endpoints to accept webhooks.
$ encore app create --example=ts/slack-bot