whatsapp-cloud 0.1.5 → 1.0.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.
- package/CHANGELOG.md +6 -0
- package/README.md +156 -42
- package/agent_docs/feedback/developer_feedback.md +493 -0
- package/agent_docs/rules/resource-design.md +206 -0
- package/agent_docs/whatsapp-cloud-api/docs/templates-api.md +296 -0
- package/agent_docs/whatsapp-cloud-api/guides/app-business-waba-setup.md +132 -0
- package/agent_docs/whatsapp-cloud-api/guides/phone-number-lifecycle.md +219 -0
- package/agent_docs/whatsapp-cloud-api/guides/wabas.md +66 -0
- package/agent_docs/whatsappcloudapi/guides/architecture.md +128 -0
- package/agent_docs/whatsappcloudapi/guides/patterns.md +115 -0
- package/dist/index.cjs +3418 -1235
- package/dist/index.d.cts +5867 -1523
- package/dist/index.d.ts +5867 -1523
- package/dist/index.js +3256 -1205
- package/package.json +9 -1
- package/src/client/HttpClient.ts +121 -63
- package/src/client/WhatsAppClient.ts +26 -34
- package/src/client/index.ts +6 -0
- package/src/{schemas/client.ts → client/schema.ts} +37 -9
- package/src/client/types.ts +14 -0
- package/src/common/errors.ts +54 -0
- package/src/common/index.ts +3 -0
- package/src/examples/explore-setup.ts +130 -0
- package/src/index.ts +5 -21
- package/src/resources/business/index.ts +8 -0
- package/src/resources/business/resource.ts +56 -0
- package/src/resources/business/schema.ts +19 -0
- package/src/resources/business/types.ts +9 -0
- package/src/resources/index.ts +20 -0
- package/src/resources/media/index.ts +8 -0
- package/src/resources/media/resource.ts +236 -0
- package/src/resources/media/schema.ts +87 -0
- package/src/resources/media/types.ts +51 -0
- package/src/resources/messages/index.ts +11 -0
- package/src/resources/messages/resource.ts +216 -0
- package/src/resources/messages/schema.ts +197 -0
- package/src/resources/messages/types.ts +95 -0
- package/src/resources/messages/utils.ts +25 -0
- package/src/resources/phoneNumbers/index.ts +144 -0
- package/src/resources/phoneNumbers/resource.ts +541 -0
- package/src/resources/phoneNumbers/schema.ts +288 -0
- package/src/resources/phoneNumbers/subresources/block/index.ts +8 -0
- package/src/resources/phoneNumbers/subresources/block/resource.ts +146 -0
- package/src/resources/phoneNumbers/subresources/block/schema.ts +74 -0
- package/src/resources/phoneNumbers/subresources/block/types.ts +16 -0
- package/src/resources/phoneNumbers/subresources/messageHistory/index.ts +8 -0
- package/src/resources/phoneNumbers/subresources/messageHistory/resource.ts +105 -0
- package/src/resources/phoneNumbers/subresources/messageHistory/schema.ts +125 -0
- package/src/resources/phoneNumbers/subresources/messageHistory/types.ts +16 -0
- package/src/resources/phoneNumbers/subresources/officialAccount/index.ts +8 -0
- package/src/resources/phoneNumbers/subresources/officialAccount/resource.ts +123 -0
- package/src/resources/phoneNumbers/subresources/officialAccount/schema.ts +73 -0
- package/src/resources/phoneNumbers/subresources/officialAccount/types.ts +12 -0
- package/src/resources/phoneNumbers/subresources/qrCodes/index.ts +8 -0
- package/src/resources/phoneNumbers/subresources/qrCodes/resource.ts +245 -0
- package/src/resources/phoneNumbers/subresources/qrCodes/schema.ts +122 -0
- package/src/resources/phoneNumbers/subresources/qrCodes/types.ts +22 -0
- package/src/resources/phoneNumbers/types.ts +123 -0
- package/src/resources/templates/index.ts +11 -0
- package/src/resources/templates/resource.ts +245 -0
- package/src/resources/templates/schema.ts +483 -0
- package/src/resources/templates/types.ts +282 -0
- package/src/resources/templates/utils.ts +29 -0
- package/src/resources/wabas/index.ts +8 -0
- package/src/resources/wabas/resource.ts +482 -0
- package/src/resources/wabas/schema.ts +332 -0
- package/src/resources/wabas/types.ts +98 -0
- package/src/resources/webhooks/index.ts +11 -0
- package/src/resources/webhooks/resource.ts +211 -0
- package/src/resources/webhooks/schema.ts +150 -0
- package/src/resources/webhooks/types.ts +162 -0
- package/src/resources/webhooks/utils.ts +84 -0
- package/agent_docs/DESIGN.md +0 -707
- package/agent_docs/INCOMING_MESSAGES_BRAINSTORM.md +0 -500
- package/agent_docs/MESSAGES_NAMESPACE_ANALYSIS.md +0 -368
- package/agent_docs/NAMING_DECISION.md +0 -78
- package/agent_docs/STRUCTURE.md +0 -711
- package/agent_docs/messages-namespace-design.md +0 -357
- package/cloud-api-docs/webhooks/endpoint.md +0 -112
- package/cloud-api-docs/webhooks/overview.md +0 -154
- package/docs/DEVELOPMENT.md +0 -154
- package/docs/webhooks.md +0 -104
- package/src/errors.ts +0 -58
- package/src/schemas/accounts/index.ts +0 -1
- package/src/schemas/accounts/phone-number.ts +0 -20
- package/src/schemas/business/account.ts +0 -43
- package/src/schemas/business/index.ts +0 -2
- package/src/schemas/debug.ts +0 -25
- package/src/schemas/index.ts +0 -7
- package/src/schemas/messages/incoming.ts +0 -72
- package/src/schemas/messages/index.ts +0 -3
- package/src/schemas/messages/outgoing.ts +0 -125
- package/src/schemas/messages/response.ts +0 -21
- package/src/schemas/templates/component.ts +0 -190
- package/src/schemas/templates/index.ts +0 -5
- package/src/schemas/templates/language.ts +0 -119
- package/src/schemas/templates/request.ts +0 -81
- package/src/schemas/templates/response.ts +0 -54
- package/src/schemas/webhooks/index.ts +0 -1
- package/src/schemas/webhooks/payload.ts +0 -132
- package/src/services/accounts/AccountsClient.ts +0 -34
- package/src/services/accounts/AccountsService.ts +0 -45
- package/src/services/accounts/index.ts +0 -2
- package/src/services/accounts/methods/list-phone-numbers.ts +0 -15
- package/src/services/business/BusinessClient.ts +0 -34
- package/src/services/business/BusinessService.ts +0 -45
- package/src/services/business/index.ts +0 -3
- package/src/services/business/methods/list-accounts.ts +0 -17
- package/src/services/index.ts +0 -3
- package/src/services/media/MediaService.ts +0 -71
- package/src/services/media/index.ts +0 -1
- package/src/services/messages/MessagesClient.ts +0 -34
- package/src/services/messages/MessagesService.ts +0 -120
- package/src/services/messages/index.ts +0 -13
- package/src/services/messages/methods/send-image.ts +0 -33
- package/src/services/messages/methods/send-location.ts +0 -32
- package/src/services/messages/methods/send-reaction.ts +0 -32
- package/src/services/messages/methods/send-text.ts +0 -32
- package/src/services/messages/utils/build-message-payload.ts +0 -32
- package/src/services/templates/TemplatesClient.ts +0 -35
- package/src/services/templates/TemplatesService.ts +0 -117
- package/src/services/templates/index.ts +0 -3
- package/src/services/templates/methods/create.ts +0 -29
- package/src/services/templates/methods/delete.ts +0 -38
- package/src/services/templates/methods/get.ts +0 -22
- package/src/services/templates/methods/list.ts +0 -37
- package/src/services/templates/methods/update.ts +0 -34
- package/src/services/webhooks/WebhooksService.ts +0 -320
- package/src/services/webhooks/index.ts +0 -8
- package/src/services/webhooks/utils/extract-messages.ts +0 -25
- package/src/services/webhooks/utils/extract-statuses.ts +0 -24
- package/src/services/webhooks/utils/verify.ts +0 -29
- package/src/types/accounts/index.ts +0 -1
- package/src/types/accounts/phone-number.ts +0 -9
- package/src/types/business/account.ts +0 -10
- package/src/types/business/index.ts +0 -2
- package/src/types/client.ts +0 -8
- package/src/types/debug.ts +0 -8
- package/src/types/index.ts +0 -8
- package/src/types/media/index.ts +0 -12
- package/src/types/messages/incoming.ts +0 -27
- package/src/types/messages/index.ts +0 -3
- package/src/types/messages/outgoing.ts +0 -71
- package/src/types/messages/response.ts +0 -7
- package/src/types/templates/component.ts +0 -45
- package/src/types/templates/index.ts +0 -5
- package/src/types/templates/language.ts +0 -10
- package/src/types/templates/request.ts +0 -27
- package/src/types/templates/response.ts +0 -39
- package/src/types/webhooks/index.ts +0 -1
- package/src/types/webhooks/payload.ts +0 -13
- package/src/utils/zod-error.ts +0 -28
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
# WhatsApp Cloud
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
A WhatsApp client tailored for LLMs - built to actually work.
|
|
3
|
+
A TypeScript SDK for the WhatsApp Cloud API that doesn't make you gnash your teeth.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
**Why this one?**
|
|
6
|
+
|
|
7
|
+
- ✅ **Complete coverage** — Messages, media, templates, WABAs, phone numbers, webhooks. All of it.
|
|
8
|
+
- ✅ **Zod-first** — Schemas are the source of truth. Types are inferred. No more guessing.
|
|
9
|
+
- ✅ **Flexible config** — Set `accessToken` once, set IDs on client or override per-request.
|
|
10
|
+
- ✅ **Logical structure** — `client.phoneNumbers.qrCodes.create()` reads like it should.
|
|
11
|
+
- ✅ **Powerful webhooks** — Filter by phone number ID, run `beforeHandler` for shared logic, then route to type-safe handlers.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
7
14
|
|
|
8
15
|
```typescript
|
|
9
16
|
import { WhatsAppClient } from "whatsapp-cloud";
|
|
@@ -61,51 +68,158 @@ export async function GET(request: NextRequest) {
|
|
|
61
68
|
export async function POST(request: NextRequest) {
|
|
62
69
|
const payload = await request.json();
|
|
63
70
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}`
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
client.webhooks.handle(
|
|
72
|
+
payload,
|
|
73
|
+
{
|
|
74
|
+
// Runs before every handler - fetch user, log, etc.
|
|
75
|
+
beforeHandler: async (message, ctx) => {
|
|
76
|
+
const user = await db.users.findByPhone(message.from);
|
|
77
|
+
console.log(`[${ctx.metadata.displayPhoneNumber}] Message from ${message.from}`);
|
|
78
|
+
return { user };
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
text: async (message, ctx, before) => {
|
|
82
|
+
await client.messages.sendText({
|
|
83
|
+
to: `+${message.from}`,
|
|
84
|
+
text: { body: `Hey ${before?.user?.name || "there"}! You said: ${message.text.body}` },
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
image: async (message, ctx, before) => {
|
|
89
|
+
const imageData = await client.media.download(message.image.id);
|
|
90
|
+
// Process image...
|
|
91
|
+
await client.messages.sendText({
|
|
92
|
+
to: `+${message.from}`,
|
|
93
|
+
text: { body: "Image received!" },
|
|
94
|
+
});
|
|
95
|
+
},
|
|
78
96
|
},
|
|
97
|
+
{
|
|
98
|
+
// Only handle messages for this phone number (useful for multi-tenant setups)
|
|
99
|
+
filter: { phoneNumberIds: [process.env.PHONE_NUMBER_ID!] },
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return NextResponse.json({ success: true });
|
|
104
|
+
}
|
|
105
|
+
```
|
|
79
106
|
|
|
80
|
-
|
|
81
|
-
console.log(`Image from ${context.contact?.name || message.from}`);
|
|
107
|
+
## API Support
|
|
82
108
|
|
|
83
|
-
|
|
84
|
-
|
|
109
|
+
| Resource | Status | Description |
|
|
110
|
+
| ------------------ | ------ | ------------------------------------------------------------ |
|
|
111
|
+
| **Messages** | ✅ | Send text, media, location, contacts, interactive, reactions |
|
|
112
|
+
| **Media** | ✅ | Upload, download, get info, delete |
|
|
113
|
+
| **Templates** | ✅ | CRUD operations for message templates |
|
|
114
|
+
| **Webhooks** | ✅ | Verify, extract, handle incoming events |
|
|
115
|
+
| **WABAs** | ✅ | Manage WhatsApp Business Accounts |
|
|
116
|
+
| **Phone Numbers** | ✅ | Registration, verification, profiles |
|
|
117
|
+
| ↳ Block | ✅ | Block/unblock users |
|
|
118
|
+
| ↳ QR Codes | ✅ | Create/manage QR codes |
|
|
119
|
+
| ↳ Message History | ✅ | Query delivery status history |
|
|
120
|
+
| ↳ Official Account | ✅ | OBA verification status |
|
|
121
|
+
| Flows | ❌ | Coming soon |
|
|
122
|
+
| Commerce | ❌ | Coming soon |
|
|
85
123
|
|
|
86
|
-
|
|
87
|
-
// const buffer = Buffer.from(imageData);
|
|
88
|
-
// await saveToStorage(buffer, message.image.id);
|
|
124
|
+
## Method Reference
|
|
89
125
|
|
|
90
|
-
|
|
91
|
-
await client.messages.sendText({
|
|
92
|
-
to: `+${message.from}`,
|
|
93
|
-
text: { body: "Image received! 📸" },
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
});
|
|
126
|
+
### Messages
|
|
97
127
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
128
|
+
```typescript
|
|
129
|
+
client.messages.send(message); // Generic send
|
|
130
|
+
client.messages.sendText(message); // Text message
|
|
131
|
+
client.messages.sendImage(message); // Image message
|
|
132
|
+
client.messages.sendLocation(message); // Location message
|
|
133
|
+
client.messages.sendReaction(message); // Reaction message
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Media
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
client.media.upload(file, type); // Upload media
|
|
140
|
+
client.media.get(mediaId); // Get media info
|
|
141
|
+
client.media.download(mediaId); // Download binary
|
|
142
|
+
client.media.delete(mediaId); // Delete media
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Templates
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
client.templates.create(data) // Create template
|
|
149
|
+
client.templates.list(options?) // List templates
|
|
150
|
+
client.templates.get(templateId) // Get template
|
|
151
|
+
client.templates.update(templateId, data) // Update template
|
|
152
|
+
client.templates.delete(templateName) // Delete template
|
|
101
153
|
```
|
|
102
154
|
|
|
103
|
-
|
|
155
|
+
### Phone Numbers
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
client.phoneNumbers.list(options?) // List numbers
|
|
159
|
+
client.phoneNumbers.get(phoneNumberId?) // Get number details
|
|
160
|
+
client.phoneNumbers.addPreverified(phoneNumber) // Partner flow
|
|
161
|
+
client.phoneNumbers.create(data) // Standard flow
|
|
162
|
+
client.phoneNumbers.requestVerificationCode(data) // Request code
|
|
163
|
+
client.phoneNumbers.verifyCode(data) // Verify code
|
|
164
|
+
client.phoneNumbers.register(data) // Register
|
|
165
|
+
client.phoneNumbers.deregister() // Deregister
|
|
166
|
+
client.phoneNumbers.getProfile() // Get business profile
|
|
167
|
+
client.phoneNumbers.updateProfile(data) // Update profile
|
|
168
|
+
```
|
|
104
169
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
170
|
+
### Phone Numbers → Block
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
client.phoneNumbers.block.list(options?) // List blocked users
|
|
174
|
+
client.phoneNumbers.block.add(users) // Block users
|
|
175
|
+
client.phoneNumbers.block.remove(users) // Unblock users
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Phone Numbers → QR Codes
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
client.phoneNumbers.qrCodes.list(options?) // List QR codes
|
|
182
|
+
client.phoneNumbers.qrCodes.get(codeId) // Get QR code
|
|
183
|
+
client.phoneNumbers.qrCodes.create(data) // Create QR code
|
|
184
|
+
client.phoneNumbers.qrCodes.update(data) // Update QR code
|
|
185
|
+
client.phoneNumbers.qrCodes.delete(codeId) // Delete QR code
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Phone Numbers → Message History
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
client.phoneNumbers.messageHistory.list(options?) // Query history
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Phone Numbers → Official Account
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
client.phoneNumbers.officialAccount.get(); // Get OBA status
|
|
198
|
+
client.phoneNumbers.officialAccount.apply(data); // Apply for OBA
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### WABAs (WhatsApp Business Accounts)
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
client.wabas.list(options?) // List WABAs
|
|
205
|
+
client.wabas.listClient(options?) // List client WABAs
|
|
206
|
+
client.wabas.get(wabaId?) // Get WABA
|
|
207
|
+
client.wabas.create(data) // Create WABA
|
|
208
|
+
client.wabas.update(data) // Update WABA
|
|
209
|
+
client.wabas.listSubscribedApps() // List subscribed apps
|
|
210
|
+
client.wabas.subscribeApp() // Subscribe app
|
|
211
|
+
client.wabas.unsubscribeApp() // Unsubscribe app
|
|
212
|
+
client.wabas.listAssignedUsers(options?) // List assigned users
|
|
213
|
+
client.wabas.addAssignedUser(userId, tasks) // Add user
|
|
214
|
+
client.wabas.removeAssignedUser(userId) // Remove user
|
|
215
|
+
client.wabas.listActivities(options?) // List activities
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Webhooks
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
client.webhooks.verify(params, token); // Verify webhook
|
|
222
|
+
client.webhooks.extractMessages(payload); // Extract messages
|
|
223
|
+
client.webhooks.extractStatuses(payload); // Extract statuses
|
|
224
|
+
client.webhooks.handle(payload, handlers); // Handle events
|
|
225
|
+
```
|