serve module provides utilities for running integrations and bots locally during development. It creates an HTTP server that handles Botpress runtime requests, allowing you to test your code before deployment.
Import
serve()
Starts a local HTTP server for handling Botpress runtime requests.Handler
required
Function that processes incoming requests.
number
default:8072
Port number to listen on. Defaults to 8072.
function
Callback invoked when server starts listening.
Promise<http.Server> - The Node.js HTTP server instance.
Request Type
Response Type
parseBody()
Helper function to parse JSON request body.Request
required
The request object to parse.
T.
Throws: Error if body is missing or invalid JSON.
Basic Usage
Health Check Endpoint
The serve function automatically provides a/health endpoint:
Integration Example
Using serve with an integration:index.ts
Bot Example
Using serve with a bot:index.ts
Webhook Handler Example
Handling external webhooks locally:webhook.ts
Request Routing Example
Error Handling
The serve function automatically catches errors in the handler:Testing with curl
Environment-Specific Configuration
Graceful Shutdown
Node.js Only
Best Practices
Use environment variables - Store configuration like API keys, webhook secrets, and port numbers in
.env files.Log requests - Add logging to track incoming requests during development.
Validate signatures - When handling webhooks, always verify signatures to ensure authenticity.
Handle errors gracefully - Catch and log errors, returning appropriate HTTP status codes.
See Also
- Integration Implementation - Implementing integration handlers
- Bot Implementation - Implementing bot handlers
- Local Development - Complete local development guide