Adding a Health Probe to a Teams SDK App (Without a Server Framework)
When exposing a Microsoft Teams bot via an Application Gateway from a private backend, setting up a health probe is essential. It lets your gateway know exactly when the service is healthy, down, or struggling.
By default, a Teams SDK app exposes an /api/messages endpoint. However, using this for health checks is a bad idea. It is reserved for bot communication, and every request sent there requires a valid JSON Web Token (JWT). For a standard health probe, we just need a lightweight, unauthenticated GET endpoint like /health.
Instead of pulling in an entire server framework like Express just to route a health check, you can directly extend the Teams SDK’s internal adapter.
The Solution
We can intercept the underlying adapter right before initializing the app to inject our custom endpoint:
import { App, ExpressAdapter } from "@microsoft/teams.apps";
const app = new App();
// Your standard bot logic
app.on('message', async ({ activity, reply }) => {
await reply(`You said: ${activity.text}`);
});
// Extend the internal adapter to add a lightweight health check
(app.server.adapter as ExpressAdapter).get('/health', (_req: unknown, res: { json(body: object): void }) => {
res.json({
status: 'ok',
uptime: process.uptime()
});
});
app.start().catch(console.error);
Why this works
By casting app.server.adapter to ExpressAdapter, you gain direct access to the underlying routing mechanisms. This allows you to expose a clean, public-facing /health endpoint that returns a 200 OK and uptime data, completely bypassing the JWT authentication required by the bot’s messaging endpoints.
