whatsapp-agent-sdk 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/CONTRIBUTING.md +25 -0
  3. package/LICENSE +22 -0
  4. package/PUBLISHING.md +64 -0
  5. package/README.md +292 -2
  6. package/SECURITY.md +30 -0
  7. package/dist/cjs/client.js +720 -0
  8. package/dist/cjs/client.js.map +1 -0
  9. package/dist/cjs/errors.js +148 -0
  10. package/dist/cjs/errors.js.map +1 -0
  11. package/dist/cjs/index.js +41 -0
  12. package/dist/cjs/index.js.map +1 -0
  13. package/dist/cjs/limits.js +79 -0
  14. package/dist/cjs/limits.js.map +1 -0
  15. package/dist/cjs/package.json +3 -0
  16. package/dist/cjs/rate-limit.js +26 -0
  17. package/dist/cjs/rate-limit.js.map +1 -0
  18. package/dist/cjs/types.js +3 -0
  19. package/dist/cjs/types.js.map +1 -0
  20. package/dist/esm/client.d.ts +49 -0
  21. package/dist/esm/client.d.ts.map +1 -0
  22. package/dist/esm/client.js +716 -0
  23. package/dist/esm/client.js.map +1 -0
  24. package/dist/esm/errors.d.ts +54 -0
  25. package/dist/esm/errors.d.ts.map +1 -0
  26. package/dist/esm/errors.js +130 -0
  27. package/dist/esm/errors.js.map +1 -0
  28. package/dist/esm/index.d.ts +5 -0
  29. package/dist/esm/index.d.ts.map +1 -0
  30. package/dist/esm/index.js +5 -0
  31. package/dist/esm/index.js.map +1 -0
  32. package/dist/esm/limits.d.ts +35 -0
  33. package/dist/esm/limits.d.ts.map +1 -0
  34. package/dist/esm/limits.js +73 -0
  35. package/dist/esm/limits.js.map +1 -0
  36. package/dist/esm/rate-limit.d.ts +6 -0
  37. package/dist/esm/rate-limit.d.ts.map +1 -0
  38. package/dist/esm/rate-limit.js +22 -0
  39. package/dist/esm/rate-limit.js.map +1 -0
  40. package/dist/esm/types.d.ts +139 -0
  41. package/dist/esm/types.d.ts.map +1 -0
  42. package/dist/esm/types.js +2 -0
  43. package/dist/esm/types.js.map +1 -0
  44. package/docs/API.md +201 -0
  45. package/examples/echo-agent.ts +21 -0
  46. package/examples/send-media.ts +17 -0
  47. package/examples/send-message.ts +11 -0
  48. package/package.json +70 -6
package/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented here.
4
+
5
+ ## 0.1.0 - 2026-10-05
6
+
7
+ Initial community SDK release for WhatsApp Agent Platform v1:
8
+
9
+ - Typed long-poll updates with exact signed-int64 cursor preservation.
10
+ - Creator discovery from retained Agent Platform updates.
11
+ - Text, image, video, audio, document, and sticker sends.
12
+ - Quoted replies.
13
+ - Read receipts and typing indicator.
14
+ - Media upload, metadata, download, and deletion.
15
+ - Typed API/transport/validation errors.
16
+ - Safe retry behavior for ambiguous sends.
17
+ - Built-in per-method rate limiting and documented platform caps.
18
+ - ESM + CommonJS builds and TypeScript declarations.
19
+
@@ -0,0 +1,25 @@
1
+ # Contributing
2
+
3
+ Contributions are welcome once the repository is public.
4
+
5
+ ## Development
6
+
7
+ ```bash
8
+ npm install
9
+ npm run typecheck
10
+ npm test
11
+ npm run build
12
+ ```
13
+
14
+ Before opening a pull request:
15
+
16
+ 1. Add or update tests for protocol behavior.
17
+ 2. Keep the package free of real API keys, WhatsApp user IDs, private messages, and media.
18
+ 3. Preserve raw response fields where practical so the SDK remains forward-compatible.
19
+ 4. Do not silently change retry semantics for `POST /messages`; ambiguous failures can duplicate messages.
20
+ 5. Update `CHANGELOG.md` for externally visible changes.
21
+
22
+ ## Protocol changes
23
+
24
+ When the WhatsApp Agent Platform manual changes, cite the manual version/date in the pull request and update limits, types, tests, and docs together.
25
+
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 whatsapp-agent-sdk contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/PUBLISHING.md ADDED
@@ -0,0 +1,64 @@
1
+ # Publishing checklist
2
+
3
+ The package is currently named `whatsapp-agent-sdk` and was not found on npm when this project was prepared on 2026-10-05. npm names are first-come, first-served, so re-check immediately before publishing.
4
+
5
+ ## 1. Create the public repository
6
+
7
+ Before publishing, add your repository metadata to `package.json`, for example:
8
+
9
+ ```json
10
+ {
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/YOUR_USERNAME/whatsapp-agent-sdk.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/YOUR_USERNAME/whatsapp-agent-sdk/issues"
17
+ },
18
+ "homepage": "https://github.com/YOUR_USERNAME/whatsapp-agent-sdk#readme"
19
+ }
20
+ ```
21
+
22
+ Do not add a real Agent Platform API key to the repository.
23
+
24
+ ## 2. Final validation
25
+
26
+ ```bash
27
+ npm install
28
+ npm audit
29
+ npm run check
30
+ npm pack --dry-run
31
+ npm view whatsapp-agent-sdk name
32
+ ```
33
+
34
+ For the last command, an npm `E404` means the package name is still unclaimed.
35
+
36
+ ## 3. Publish
37
+
38
+ ```bash
39
+ npm login
40
+ npm publish
41
+ ```
42
+
43
+ `prepack` automatically runs type checking, tests, and the build before npm creates the publish tarball.
44
+
45
+ ## 4. Tag the source release
46
+
47
+ ```bash
48
+ git tag v0.1.0
49
+ git push origin main --tags
50
+ ```
51
+
52
+ Create a GitHub release from `CHANGELOG.md` if desired.
53
+
54
+ ## Future releases
55
+
56
+ Use semantic versioning:
57
+
58
+ ```bash
59
+ npm version patch # bug fix
60
+ npm version minor # backward-compatible feature
61
+ npm version major # breaking API change
62
+ ```
63
+
64
+ Update `CHANGELOG.md` before publishing each version.
package/README.md CHANGED
@@ -1,3 +1,293 @@
1
- # Temporary Holding Version
1
+ # whatsapp-agent-sdk
2
+
3
+ Community TypeScript/Node.js SDK for WhatsApp's **Agent Platform API** (`/agent/v1`) — the API for personal third-party agents created inside WhatsApp.
4
+
5
+ > **Community project.** This is not an official WhatsApp or Meta SDK and is not affiliated with, endorsed by, sponsored by, or maintained by WhatsApp or Meta.
6
+
7
+ ## Why this exists
8
+
9
+ WhatsApp's Agent Platform is different from both the WhatsApp Business Cloud API and unofficial WhatsApp Web libraries such as Baileys.
10
+
11
+ | | Agent Platform | Business Cloud API | WhatsApp Web / Baileys |
12
+ |---|---|---|---|
13
+ | Intended use | Personal third-party agents | Businesses messaging customers | Automating a linked WhatsApp client |
14
+ | Setup | Create an agent in WhatsApp and copy its API key | Meta developer/business setup | QR/link-device flow |
15
+ | Inbound transport | Long polling | Webhooks | WhatsApp Web protocol |
16
+ | Current recipient model | Agent creator | Business customers | Depends on linked account |
17
+ | Official API | Yes | Yes | No |
18
+
19
+ This package wraps the official Agent Platform HTTP API while adding typed models, exact cursor handling, retries, rate limiting, validation, creator discovery, and media helpers.
20
+
21
+ ## Requirements
22
+
23
+ - Node.js **18+**.
24
+ - A WhatsApp account that currently has the **Agents** feature. Availability is still limited and may differ by account/country.
25
+ - An Agent Platform API key created inside WhatsApp.
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ npm install whatsapp-agent-sdk
31
+ ```
32
+
33
+ ## Get an API key
34
+
35
+ In WhatsApp:
36
+
37
+ 1. Open **Settings**.
38
+ 2. Open **Agents**.
39
+ 3. Create an agent.
40
+ 4. Open the agent chat and its **Chat info**.
41
+ 5. Copy the **API key**.
42
+
43
+ Store the key as a server-side secret. Do not expose it in browser code, commit it to Git, put it in URLs, or log it.
44
+
45
+ ```bash
46
+ WHATSAPP_AGENT_TOKEN=your_api_key
47
+ ```
48
+
49
+ ## Quick start
50
+
51
+ ```ts
52
+ import { WhatsAppAgentClient } from "whatsapp-agent-sdk";
53
+
54
+ const client = new WhatsAppAgentClient({
55
+ token: process.env.WHATSAPP_AGENT_TOKEN!,
56
+ });
57
+
58
+ // If the retained Agent Platform buffer contains a creator update, the SDK
59
+ // discovers the creator automatically before the first send.
60
+ const sent = await client.sendText("Hello from my agent 👋");
61
+ console.log(sent.messageId);
62
+ ```
63
+
64
+ If the retained buffer is empty, send the agent one message from WhatsApp first, or pass the creator's opaque `user:<id>` identifier explicitly.
65
+
66
+ ## Receive messages
67
+
68
+ The Agent Platform uses **long polling**, not a public webhook.
69
+
70
+ ```ts
71
+ const client = new WhatsAppAgentClient({
72
+ token: process.env.WHATSAPP_AGENT_TOKEN!,
73
+ });
74
+
75
+ for await (const update of client.pollUpdates({ offset: 0 })) {
76
+ for (const message of update.messages) {
77
+ if (message.type !== "text" || !message.text) continue;
78
+
79
+ // Optional: marks the message read and shows typing.
80
+ await client.sendTyping(message.id);
81
+
82
+ await client.reply(message, `You said: ${message.text}`);
83
+ }
84
+
85
+ // Persist only AFTER your processing succeeds.
86
+ if (update.nextOffset) {
87
+ await saveCursor(update.nextOffset);
88
+ }
89
+ }
90
+ ```
91
+
92
+ ### Cursor safety
93
+
94
+ `next_offset` is a signed 64-bit integer in the protocol. JavaScript numbers cannot represent every 64-bit integer exactly, so this SDK returns `update.nextOffset` as a **decimal string**. Pass that string back to `offset` unchanged.
95
+
96
+ ```ts
97
+ const update = await client.getUpdates({ offset: "9223372036854775807" });
98
+ ```
99
+
100
+ Never increment or calculate the cursor yourself.
101
+
102
+ ## Send messages
103
+
104
+ ### Text
105
+
106
+ ```ts
107
+ await client.sendText("Hello");
108
+
109
+ await client.sendText("user:123...", "Hello");
110
+
111
+ await client.sendText("user:123...", "See this link", {
112
+ previewUrl: true,
113
+ });
114
+ ```
115
+
116
+ ### Quote/reply
117
+
118
+ ```ts
119
+ await client.reply(message, "Got it.");
120
+
121
+ await client.sendText(message.from, "Got it.", {
122
+ replyTo: message.id,
123
+ });
124
+ ```
125
+
126
+ ### Images, video, audio, documents, stickers
127
+
128
+ Upload and send a local file in one call:
129
+
130
+ ```ts
131
+ await client.sendImage({
132
+ file: "./photo.jpg",
133
+ caption: "Photo from my agent",
134
+ });
135
+
136
+ await client.sendDocument({
137
+ file: "./report.pdf",
138
+ filename: "report.pdf",
139
+ caption: "Report",
140
+ });
141
+ ```
142
+
143
+ Or reuse a media ID:
144
+
145
+ ```ts
146
+ const mediaId = await client.uploadMedia("./photo.jpg");
147
+ await client.sendImage({ mediaId, caption: "Uploaded once" });
148
+ ```
149
+
150
+ ## Read receipts and typing
151
+
152
+ ```ts
153
+ await client.markRead(message.id);
154
+ await client.sendTyping(message.id);
155
+ ```
156
+
157
+ `sendTyping()` also marks the message read. A typing indicator clears after a reply or roughly 25 seconds.
158
+
159
+ **Important:** marking an inbound message read removes that message from the replayable retained update buffer. If replay-after-crash matters, process successfully before marking read.
160
+
161
+ ## Media
162
+
163
+ ```ts
164
+ const media = await client.getMedia(message.media!.id);
165
+ console.log(media.mimeType, media.fileSize);
166
+
167
+ const bytes = await client.downloadMedia(message.media!.id);
168
+ await client.downloadMedia(message.media!.id, "./downloaded-file");
169
+
170
+ await client.deleteMedia(message.media!.id);
171
+ ```
172
+
173
+ The media download URL returned by WhatsApp requires the same bearer token. The SDK refuses HTTP redirect following for authenticated media downloads so the token is not forwarded to a redirect target.
174
+
175
+ ## Current platform limits
176
+
177
+ The SDK validates/paces against the limits documented for Agent Platform v1:
178
+
179
+ | Operation | Limit |
180
+ |---|---:|
181
+ | `POST /messages` | 12/minute/agent |
182
+ | `POST /statuses` | 12/minute/agent |
183
+ | `GET /updates` | 15/minute/agent |
184
+ | `POST /media` | 12/minute/agent |
185
+ | `GET /media/:id` | 12/minute/agent |
186
+ | `DELETE /media/:id` | 12/minute/agent |
187
+
188
+ Other useful caps:
189
+
190
+ - Text: **4096 characters**.
191
+ - Media caption: **1024 characters**.
192
+ - Image: JPEG/PNG, up to **5 MiB**.
193
+ - Sticker: WebP, up to **500 KiB**.
194
+ - Video/audio/document: up to **16 MiB**.
195
+ - Poll `limit`: max **100**.
196
+ - Long-poll timeout: max **25 seconds**.
197
+ - Retained Agent Platform updates and uploaded media: up to **30 days**.
198
+
199
+ These limits belong to the evolving Agent Platform and may change. Check the official developer manual before relying on them for long-lived production behavior.
200
+
201
+ ## Important platform behavior
202
+
203
+ - **Creator-only today:** the API currently sends to the agent creator using an opaque `user:<id>`, not an E.164 phone number.
204
+ - **No access to other chats:** an agent cannot read your normal WhatsApp chats or be added to them through this API.
205
+ - **One poller per API key:** a newer `GET /updates` poll replaces an older one. The older poll receives HTTP 409 / code `1752041`.
206
+ - **Polling does not consume updates:** `offset=0` replays the retained buffer. Read receipts can remove inbound messages from replay.
207
+ - **204 means no update:** retry with the same cursor.
208
+ - **At-least-once handling:** persist the cursor after successful processing and deduplicate by message ID if necessary.
209
+ - **No exact ordering guarantee for concurrent sends:** serialize sends yourself when strict ordering matters.
210
+ - **Not a full WhatsApp client API:** message edits/deletes, outbound reactions, groups, buttons/lists, command menus, and voice-note bubbles are not exposed by Agent Platform v1.
211
+
212
+ ## Errors and retries
213
+
214
+ The SDK exposes typed errors:
215
+
216
+ ```ts
217
+ import {
218
+ AuthenticationError,
219
+ ForbiddenError,
220
+ PollReplacedError,
221
+ RateLimitError,
222
+ ServerError,
223
+ } from "whatsapp-agent-sdk";
224
+ ```
225
+
226
+ All API errors expose the HTTP status, WhatsApp error code, details, trace ID, and raw response when available.
227
+
228
+ Safe retry behavior matters for sending:
229
+
230
+ - `429` rate limits are retried with backoff.
231
+ - Explicit `503 / 131016` "not accepted for delivery" errors are retryable.
232
+ - A `500`, connection reset, or timeout after `POST /messages` can have an **unknown send outcome**. The SDK does **not** automatically retry those sends by default because doing so can duplicate a message.
233
+ - Set `retrySendOnServerError: true` only if duplicate delivery is acceptable.
234
+
235
+ ## Disable built-in rate limiting
236
+
237
+ The SDK rate-limits itself by default.
238
+
239
+ ```ts
240
+ const client = new WhatsAppAgentClient({
241
+ token,
242
+ rateLimit: false,
243
+ });
244
+ ```
245
+
246
+ This is mainly useful for tests or when a higher-level application already coordinates the API budgets.
247
+
248
+ ## Privacy and security
249
+
250
+ Agent Platform chats are **not considered end-to-end encrypted** by WhatsApp because Meta operates infrastructure on behalf of the third-party agent. Your normal WhatsApp chats remain separate from the agent.
251
+
252
+ Treat the agent API key like a password:
253
+
254
+ - keep it server-side;
255
+ - use environment variables or a secrets manager;
256
+ - never ship it in frontend/browser bundles;
257
+ - never include it in logs;
258
+ - rotate it if it is exposed.
259
+
260
+ See [SECURITY.md](./SECURITY.md) for project-specific guidance and WhatsApp's official Third-Party Agent Terms for platform privacy details.
261
+
262
+ ## API reference
263
+
264
+ See **[docs/API.md](./docs/API.md)** for constructor options, every method, accepted media formats, error classes, and endpoint mapping.
265
+
266
+ ## Examples
267
+
268
+ - [`examples/send-message.ts`](./examples/send-message.ts) — proactive text send.
269
+ - [`examples/echo-agent.ts`](./examples/echo-agent.ts) — long-poll and reply loop.
270
+ - [`examples/send-media.ts`](./examples/send-media.ts) — image/document uploads.
271
+
272
+ ## Development
273
+
274
+ ```bash
275
+ npm install
276
+ npm run typecheck
277
+ npm test
278
+ npm run build
279
+ npm pack --dry-run
280
+ ```
281
+
282
+ ## Official resources
283
+
284
+ - WhatsApp Agent Platform Developer Manual: https://www.whatsapp.com/developer/WhatsApp-Agent-Platform-Developer-Manual.pdf
285
+ - WhatsApp Help Center — third-party agents: https://faq.whatsapp.com/1050934623978152
286
+ - Third-Party Agent Terms: https://www.whatsapp.com/legal/third-party-agents-terms
287
+
288
+ ## License
289
+
290
+ MIT.
291
+
292
+ WhatsApp is a trademark of WhatsApp LLC. Meta is a trademark of Meta Platforms, Inc. This project is independent and is not affiliated with, endorsed by, sponsored by, or maintained by WhatsApp or Meta.
2
293
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
package/SECURITY.md ADDED
@@ -0,0 +1,30 @@
1
+ # Security
2
+
3
+ ## API keys
4
+
5
+ The WhatsApp Agent Platform API key is a bearer secret. Anyone who obtains it may be able to operate that agent within the platform's permissions.
6
+
7
+ - Keep it on a trusted server/device.
8
+ - Use environment variables or a secrets manager.
9
+ - Never commit it to source control.
10
+ - Never put it in a URL/query string.
11
+ - Never expose it in browser/client-side JavaScript.
12
+ - Never log the `Authorization` header.
13
+ - Regenerate/rotate the key if it is exposed.
14
+
15
+ This repository intentionally contains no real API key.
16
+
17
+ ## Agent message privacy
18
+
19
+ WhatsApp states that conversations with third-party agents are not considered end-to-end encrypted because Meta manages the service on behalf of the agent. Applications using this SDK should clearly disclose their own data handling and any model/provider that receives message or media content.
20
+
21
+ Official terms: https://www.whatsapp.com/legal/third-party-agents-terms
22
+
23
+ ## Media downloads
24
+
25
+ Media URLs returned by the authenticated API require bearer authorization. The SDK sets `redirect: "error"` for these downloads so an authorization header is not intentionally forwarded to a redirect destination.
26
+
27
+ ## Reporting a vulnerability
28
+
29
+ Before the project has a public repository security channel, report vulnerabilities privately to the repository maintainer rather than opening an issue containing secrets or exploit details.
30
+