Overview
The HTTP API adapter allows applications to interact with NapCat by sending HTTP requests. It supports both standard HTTP endpoints and WebSocket connections over the same port.Configuration
Configure the HTTP server in your OneBot config:Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
enable | boolean | false | Enable the HTTP server |
port | number | 3000 | Port to listen on |
host | string | 127.0.0.1 | Host address to bind |
enableCors | boolean | true | Enable CORS for cross-origin requests |
enableWebsocket | boolean | false | Enable WebSocket on the same port |
messagePostFormat | string | array | Message format (array or string) |
token | string | "" | Access token for authentication |
debug | boolean | false | Enable debug logging |
Making Requests
Basic Request
Make API calls by sending HTTP requests tohttp://host:port/action_name:
GET Requests
You can also use GET requests with query parameters:Authentication
NapCat supports two authentication methods:1. Authorization Header (Recommended)
2. Query Parameter
Response Format
All API responses follow the OneBot 11 standard format:Success Response
Error Response
Common Status Codes
| HTTP Code | Retcode | Description |
|---|---|---|
| 200 | 0 | Success |
| 200 | 1400 | Invalid JSON format |
| 200 | 1403 | Token verification failed |
| 200 | 1404 | API action not found |
| 403 | - | Authentication failed |
WebSocket on HTTP Port
WhenenableWebsocket is enabled, you can connect to WebSocket on the same port:
Echo Parameter
Include anecho field in your request to track responses:
Request Body Format
NapCat accepts:- Standard JSON (
application/json) - URL-encoded form data (
application/x-www-form-urlencoded) - JSON5 format (relaxed JSON syntax)
- Requests without
Content-Typeheader (auto-detected as JSON)
Multiple HTTP Servers
You can run multiple HTTP servers on different ports:Implementation Details
The HTTP adapter is implemented inpackages/napcat-onebot/network/http-server.ts and uses:
- Express.js for HTTP routing
- CORS support for cross-origin requests
- Support for request bodies up to 5000MB
- Automatic JSON5 parsing for flexible syntax
Best Practices
- Always use authentication - Set a strong token in production
- Use HTTPS in production - Place NapCat behind a reverse proxy with SSL
- Bind to localhost - Use
127.0.0.1unless you need external access - Enable CORS carefully - Only enable if you need browser access
- Monitor the debug logs - Set
debug: trueduring development
Next Steps
- Explore WebSocket connections for real-time events
- Set up HTTP POST webhooks for event delivery
- Check API reference for available actions
