Skip to main content
The 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.
Returns: 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.
Returns: Parsed JSON body as type T. Throws: Error if body is missing or invalid JSON.

Basic Usage

Health Check Endpoint

The serve function automatically provides a /health endpoint:
This endpoint is used by Botpress to verify the server is running.

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:
For better error handling:

Testing with curl

Environment-Specific Configuration

Graceful Shutdown

Node.js Only

The serve function only works in Node.js environments. It will throw an error if called in browser contexts.

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