Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(api): declare the 413 the media byte cap answers
Yudhi Armyndharisf1159d1c3eed
diff --git a/.env.example b/.env.exampleindex 2789916c6..dd1977ea3 100644--- a/.env.example+++ b/.env.example@@ -356,7 +356,9 @@ DATABASE_SYNCHRONIZE=false # WARNING: Set false in production! (data DB# default. Streaming reads are not affected — only the listing call is truncated.# STORAGE_LIST_MAX_FILES=100000-# S3/MinIO settings (if STORAGE_TYPE=s3, auto-configured if MINIO_BUILTIN=true)+# S3/MinIO settings (if STORAGE_TYPE=s3). Saving storage from the dashboard writes the credentials+# below for you when you pick the built-in MinIO. Setting MINIO_BUILTIN=true by hand does not: it+# starts the bundled container and nothing else, so a hand-edited file still needs the keys set.# S3_ENDPOINT is OPTIONAL: leave it unset for standard AWS S3 (derived from region); set it only for# an S3-compatible store (MinIO, R2, …), which also enables path-style addressing.# S3_ENDPOINT=http://localhost:9000 # uncomment / set ONLY for an S3-compatible store (MinIO, R2)@@ -452,7 +454,7 @@ WEBHOOK_SSRF_PROTECT=true# STORE_EPHEMERAL_MESSAGES=true # Set to false to skip persisting and dispatching incoming# # WhatsApp disappearing messages (ephemeralDuration > 0).# # Default: true (backward compatible — store everything).-# MEDIA_DOWNLOAD_MAX_BYTES=52428800 # cap remote-URL sends, inbound media, AND outbound base64 sends (default 50 MiB; oversized inbound media is dropped, message kept; oversized base64 is rejected with 400)+# MEDIA_DOWNLOAD_MAX_BYTES=52428800 # cap remote-URL sends, inbound media, AND outbound base64 sends (default 50 MiB; oversized inbound media is dropped, message kept; oversized base64 is rejected with 413)# MEDIA_DOWNLOAD_TIMEOUT_MS=30000 # abort a slow media download (default 30s)# Inbound media download parallelism (how many inbound media items download at once). Each concurrent# download holds memory up to MEDIA_DOWNLOAD_MAX_BYTES; this bounds N. A non-positive/garbage valuediff --git a/CHANGELOG.md b/CHANGELOG.mdindex 30c55d108..a61bb5da2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -93,6 +93,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0check. Ten of these routes now declare `503` in the API contract; the message send routes keepanswering `500` deliberately, because `503` is replay-safe in the SDK clients and a replayed sendwould duplicate the message.+- The seven routes that answer the media byte cap's `413` now declare it: the five media sends,+ `send-bulk` and the group picture. Only the media conversions, the status sends and the profile+ picture declared it before, and `docs/06` described the same rejection as a `400` on two of them, so+ a client written against the contract handled a status the gateway never sends. Behaviour is+ unchanged. Thanks @onepay-ye for the report.+- `.env.example` no longer describes an oversized base64 send as a `400` (it is `413`), and its S3+ block no longer implies `MINIO_BUILTIN=true` fills in the credentials. Only saving storage from the+ dashboard writes them, so a hand-edited file gets the bundled MinIO container and no keys, and media+ is written to local disk while the bucket stays empty. Thanks @onepay-ye for the report.### Dependenciesdiff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 63bc32599..227500e20 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -1737,7 +1737,7 @@ Send an image (by URL or base64) with an optional caption.{ "messageId": "[email protected]_3EB0ABCD", "timestamp": 1719312000 }```-**Errors:** `400` neither `url` nor `base64`, base64 without `mimetype`, base64 over media cap, SSRF-blocked URL, session not active, or unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine+**Errors:** `400` neither `url` nor `base64`, base64 without `mimetype`, SSRF-blocked URL, session not active, or unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `413` base64 media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine#### POST /api/sessions/:sessionId/messages/send-video@@ -1763,7 +1763,7 @@ Send a video (by URL or base64) with an optional caption. Uses the same `SendMed{ "messageId": "[email protected]_3EB0ABCD", "timestamp": 1719312000 }```-**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine+**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `413` base64 media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine#### POST /api/sessions/:sessionId/messages/send-audio@@ -1789,7 +1789,7 @@ Send an audio message (by URL or base64). Uses `SendAudioMessageDto`. A `caption{ "messageId": "[email protected]_3EB0ABCD", "timestamp": 1719312000 }```-**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine+**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `413` base64 media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine#### POST /api/sessions/:sessionId/messages/send-document@@ -1822,7 +1822,7 @@ Send a document/file (by URL or base64). Uses `SendMediaMessageDto`; `filename`**Engine differences:** Baileys always sends a document as a document, while whatsapp-web.js deliberately keeps normal mimetype classification for `status@broadcast` and broadcast lists — the library returns `null` for document-mode sends to those recipients, so forcing the flag there would turn a working send into a failure. For URL-based sends without an explicit `filename`, whatsapp-web.js derives the URL basename; Baileys falls back to the literal `file`.-**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine+**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `413` base64 media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine#### POST /api/sessions/:sessionId/messages/send-location@@ -1922,7 +1922,7 @@ Send a sticker (by URL or base64; typically webp). Reuses `SendMediaMessageDto`.{ "messageId": "[email protected]_3EB0ABCD", "timestamp": 1719312000 }```-**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine+**Errors:** `400` media validation failure / session not active / unknown body field · `401` missing/invalid API key · `403` key role below OPERATOR · `413` base64 media over the media cap (see §6.3) · `500` engine error · `409` conflict or engine not ready (retryable) · `501` not supported on the active engine#### POST /api/sessions/:sessionId/messages/send-poll@@ -2196,7 +2196,7 @@ The rendered text is bounded the same way, by `TEMPLATE_RENDER_MAX_CHARS` (defau}```-**Errors:** `400` session not active, duplicate `batchId`, base64 over media cap, or DTO/nested validation failure (unknown nested field rejected) · `401` missing/invalid API key · `403` key role below OPERATOR · `500` engine error+**Errors:** `400` session not active, duplicate `batchId`, or DTO/nested validation failure (unknown nested field rejected) · `401` missing/invalid API key · `403` key role below OPERATOR · `413` base64 media over the media cap (see §6.3) · `500` engine error#### POST /api/sessions/:sessionId/messages/batch/:batchId/cancel@@ -2647,7 +2647,7 @@ Set the group's picture. The account must be a group admin.**Response** `200` — `{ "success": true, "message": "Group picture updated" }`-**Errors:** `400` the id does not name a group, the session is not active, or neither `url` nor `base64` was supplied · `401` missing/invalid API key · `403` key lacks OPERATOR role, or the engine refused (admin rights required) · `404` no such group · `409` the session is not connected (engine exists but is not `ready`) · `503` WhatsApp did not answer within the request budget — the change may or may not have been applied+**Errors:** `400` the id does not name a group, the session is not active, or neither `url` nor `base64` was supplied · `401` missing/invalid API key · `403` key lacks OPERATOR role, or the engine refused (admin rights required) · `404` no such group · `409` the session is not connected (engine exists but is not `ready`) · `413` base64 media over the media cap (see §6.3) · `503` WhatsApp did not answer within the request budget — the change may or may not have been applied#### DELETE /api/sessions/:sessionId/groups/:groupId/picturediff --git a/openapi.json b/openapi.jsonindex 800f1fe05..dd4728327 100644--- a/openapi.json+++ b/openapi.json@@ -2507,6 +2507,9 @@"409": {"description": "The session is not connected — an engine exists for it but is not `ready`: disconnected, reconnecting, or still initializing, so the request never reached WhatsApp. Wait for `ready` and retry. A session that was never started answers `400` instead, and the session lifecycle routes answer `409` for a conflicting state rather than this. One window answers this while the session still reads `ready`: WhatsApp Web periodically reloads its own page and the engine re-injects into it — for those few seconds the answer is a `409` naming the reload; retry shortly."},+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."+ },"501": {"description": "Sending media to a channel (`<id>@newsletter`) is not supported by the whatsapp-web.js engine — the page method it needs was removed by a WhatsApp Web update. Text to a channel still works."}@@ -2567,6 +2570,9 @@"409": {"description": "The session is not connected — an engine exists for it but is not `ready`: disconnected, reconnecting, or still initializing, so the request never reached WhatsApp. Wait for `ready` and retry. A session that was never started answers `400` instead, and the session lifecycle routes answer `409` for a conflicting state rather than this. One window answers this while the session still reads `ready`: WhatsApp Web periodically reloads its own page and the engine re-injects into it — for those few seconds the answer is a `409` naming the reload; retry shortly."},+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."+ },"501": {"description": "Sending media to a channel (`<id>@newsletter`) is not supported by the whatsapp-web.js engine — the page method it needs was removed by a WhatsApp Web update. Text to a channel still works."}@@ -2628,6 +2634,9 @@"409": {"description": "The session is not connected — an engine exists for it but is not `ready`: disconnected, reconnecting, or still initializing, so the request never reached WhatsApp. Wait for `ready` and retry. A session that was never started answers `400` instead, and the session lifecycle routes answer `409` for a conflicting state rather than this. One window answers this while the session still reads `ready`: WhatsApp Web periodically reloads its own page and the engine re-injects into it — for those few seconds the answer is a `409` naming the reload; retry shortly."},+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."+ },"501": {"description": "Sending media to a channel (`<id>@newsletter`) is not supported by the whatsapp-web.js engine — the page method it needs was removed by a WhatsApp Web update. Text to a channel still works."}@@ -2689,6 +2698,9 @@"409": {"description": "The session is not connected — an engine exists for it but is not `ready`: disconnected, reconnecting, or still initializing, so the request never reached WhatsApp. Wait for `ready` and retry. A session that was never started answers `400` instead, and the session lifecycle routes answer `409` for a conflicting state rather than this. One window answers this while the session still reads `ready`: WhatsApp Web periodically reloads its own page and the engine re-injects into it — for those few seconds the answer is a `409` naming the reload; retry shortly."},+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."+ },"501": {"description": "Sending media to a channel (`<id>@newsletter`) is not supported by the whatsapp-web.js engine — the page method it needs was removed by a WhatsApp Web update. Text to a channel still works."}@@ -2866,6 +2878,9 @@"409": {"description": "The session is not connected — an engine exists for it but is not `ready`: disconnected, reconnecting, or still initializing, so the request never reached WhatsApp. Wait for `ready` and retry. A session that was never started answers `400` instead, and the session lifecycle routes answer `409` for a conflicting state rather than this. One window answers this while the session still reads `ready`: WhatsApp Web periodically reloads its own page and the engine re-injects into it — for those few seconds the answer is a `409` naming the reload; retry shortly."},+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."+ },"501": {"description": "Sending media to a channel (`<id>@newsletter`) is not supported by the whatsapp-web.js engine — the page method it needs was removed by a WhatsApp Web update. Text to a channel still works."}@@ -3666,6 +3681,9 @@},"400": {"description": "Session not active or invalid request"+ },+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."}},"summary": "Send messages to multiple recipients (async batch processing)",@@ -5888,6 +5906,9 @@"409": {"description": "The session is not connected — an engine exists for it but is not `ready`: disconnected, reconnecting, or still initializing, so the request never reached WhatsApp. Wait for `ready` and retry. A session that was never started answers `400` instead, and the session lifecycle routes answer `409` for a conflicting state rather than this. One window answers this while the session still reads `ready`: WhatsApp Web periodically reloads its own page and the engine re-injects into it — for those few seconds the answer is a `409` naming the reload; retry shortly."},+ "413": {+ "description": "The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and base64 inflates by about a third, so a large send usually meets that coarser limit first: the body parser rejects the request before this check runs."+ },"503": {"description": "WhatsApp did not answer within the request budget. The change may or may not have been applied — the gateway stopped waiting for a confirmation that never came."}diff --git a/src/common/openapi/engine-status-responses.ts b/src/common/openapi/engine-status-responses.tsindex b2c48d487..793ac2b51 100644--- a/src/common/openapi/engine-status-responses.ts+++ b/src/common/openapi/engine-status-responses.ts@@ -119,3 +119,19 @@ export const ENGINE_NOT_SUPPORTED_501 =export const CHANNEL_MEDIA_501 ='Sending media to a channel (`<id>@newsletter`) is not supported by the whatsapp-web.js engine — ' +'the page method it needs was removed by a WhatsApp Web update. Text to a channel still works.';++/**+ * `PayloadTooLargeException` (413), thrown by `assertBase64WithinMediaCap` when an outbound base64+ * payload's DECODED size exceeds the shared media byte cap, before the payload is persisted or handed+ * to an engine.+ *+ * Declared as a constant for the same reason as the rest of this file: the cap belongs to the media+ * path, not to any one route, and it already reaches nine call sites. It was answered by every media+ * send long before it was declared on any of them, which is how a caller reading only the contract+ * came to expect a `400` the code never sends.+ */+export const MEDIA_TOO_LARGE_413 =+ 'The decoded base64 media exceeds the media byte cap (`MEDIA_DOWNLOAD_MAX_BYTES`, 50 MiB by ' ++ 'default). A whole request is separately bounded by `BODY_SIZE_LIMIT` (25 mb by default), and ' ++ 'base64 inflates by about a third, so a large send usually meets that coarser limit first: the ' ++ 'body parser rejects the request before this check runs.';diff --git a/src/modules/group/group.controller.ts b/src/modules/group/group.controller.tsindex 83aa31841..fd0cc4840 100644--- a/src/modules/group/group.controller.ts+++ b/src/modules/group/group.controller.ts@@ -30,6 +30,7 @@ import {ENGINE_NOT_READY_409,ENGINE_REFUSED_403,GROUP_NOT_FOUND_404,+ MEDIA_TOO_LARGE_413,} from '../../common/openapi/engine-status-responses';// Reading an invite code is admin-only, but the groups list returns every group the account@@ -477,6 +478,7 @@ export class GroupController {})@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })@ApiResponse({ status: 404, description: GROUP_NOT_FOUND_404 })+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async setPicture(@Param('sessionId') sessionId: string,@Param('groupId') groupId: string,diff --git a/src/modules/message/message.controller.ts b/src/modules/message/message.controller.tsindex 085134464..7a50c11ea 100644--- a/src/modules/message/message.controller.ts+++ b/src/modules/message/message.controller.ts@@ -52,6 +52,7 @@ import {CUSTOM_LINK_PREVIEW_501,ENGINE_NOT_READY_409,ENGINE_NOT_SUPPORTED_501,+ MEDIA_TOO_LARGE_413,MESSAGE_NOT_FOUND_404,RECIPIENT_UNREACHABLE_400,} from '../../common/openapi/engine-status-responses';@@ -180,6 +181,7 @@ export class MessageController {})@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })@ApiResponse({ status: 501, description: CHANNEL_MEDIA_501 })+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async sendImage(@Param('sessionId') sessionId: string,@Body() dto: SendMediaMessageDto,@@ -203,6 +205,7 @@ export class MessageController {})@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })@ApiResponse({ status: 501, description: CHANNEL_MEDIA_501 })+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async sendVideo(@Param('sessionId') sessionId: string,@Body() dto: SendMediaMessageDto,@@ -226,6 +229,7 @@ export class MessageController {})@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })@ApiResponse({ status: 501, description: CHANNEL_MEDIA_501 })+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async sendAudio(@Param('sessionId') sessionId: string,@Body() dto: SendAudioMessageDto,@@ -249,6 +253,7 @@ export class MessageController {})@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })@ApiResponse({ status: 501, description: CHANNEL_MEDIA_501 })+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async sendDocument(@Param('sessionId') sessionId: string,@Body() dto: SendMediaMessageDto,@@ -303,6 +308,7 @@ export class MessageController {@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })@ApiResponse({ status: 400, description: RECIPIENT_UNREACHABLE_400 })@ApiResponse({ status: 501, description: CHANNEL_MEDIA_501 })+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async sendSticker(@Param('sessionId') sessionId: string,@Body() dto: SendMediaMessageDto,@@ -702,6 +708,7 @@ export class MessageController {status: 400,description: 'Session not active or invalid request',})+ @ApiResponse({ status: 413, description: MEDIA_TOO_LARGE_413 })async sendBulk(@Param('sessionId') sessionId: string,@Body() dto: SendBulkMessageDto,