@semboja/connect 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.
- package/LICENSE +21 -0
- package/README.md +304 -0
- package/dist/index.d.mts +739 -0
- package/dist/index.d.ts +739 -0
- package/dist/index.js +635 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +599 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Semboja Tech
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# @semboja/connect
|
|
2
|
+
|
|
3
|
+
Official Node.js SDK for [Semboja WhatsApp API Bridge](https://connect.semboja.tech).
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@semboja/connect)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @semboja/connect
|
|
12
|
+
# or
|
|
13
|
+
yarn add @semboja/connect
|
|
14
|
+
# or
|
|
15
|
+
pnpm add @semboja/connect
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { SembojaClient } from '@semboja/connect';
|
|
22
|
+
|
|
23
|
+
const client = new SembojaClient('sk_live_your_api_key');
|
|
24
|
+
|
|
25
|
+
// Send a text message
|
|
26
|
+
const result = await client.messages.sendText({
|
|
27
|
+
phoneNumberId: '123456789',
|
|
28
|
+
to: '+6281234567890',
|
|
29
|
+
text: 'Hello from Semboja!',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log('Message ID:', result.data.messages[0].id);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Type-safe** - Full TypeScript support with comprehensive types
|
|
38
|
+
- **Simple API** - Intuitive methods for all WhatsApp operations
|
|
39
|
+
- **Error handling** - Typed errors for easy error handling
|
|
40
|
+
- **Webhook verification** - Built-in signature verification
|
|
41
|
+
- **Retry logic** - Automatic retries with exponential backoff
|
|
42
|
+
- **Test mode** - Safe testing with `sk_test_*` keys
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### Initialize Client
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { SembojaClient } from '@semboja/connect';
|
|
50
|
+
|
|
51
|
+
// Simple initialization
|
|
52
|
+
const client = new SembojaClient('sk_live_your_api_key');
|
|
53
|
+
|
|
54
|
+
// With options
|
|
55
|
+
const client = new SembojaClient({
|
|
56
|
+
apiKey: process.env.SEMBOJA_API_KEY!,
|
|
57
|
+
timeout: 60000, // 60 seconds
|
|
58
|
+
retries: 5,
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Send Messages
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Text message
|
|
66
|
+
await client.messages.sendText({
|
|
67
|
+
phoneNumberId: '123456789',
|
|
68
|
+
to: '+6281234567890',
|
|
69
|
+
text: 'Hello, World!',
|
|
70
|
+
previewUrl: true, // Enable URL preview
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Reply to a message
|
|
74
|
+
await client.messages.sendText({
|
|
75
|
+
phoneNumberId: '123456789',
|
|
76
|
+
to: '+6281234567890',
|
|
77
|
+
text: 'Thanks for your message!',
|
|
78
|
+
replyTo: 'wamid.xxx', // Message ID to reply to
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Template message
|
|
82
|
+
await client.messages.sendTemplate({
|
|
83
|
+
phoneNumberId: '123456789',
|
|
84
|
+
to: '+6281234567890',
|
|
85
|
+
template: {
|
|
86
|
+
name: 'order_confirmation',
|
|
87
|
+
language: { code: 'id' },
|
|
88
|
+
components: [
|
|
89
|
+
{
|
|
90
|
+
type: 'body',
|
|
91
|
+
parameters: [
|
|
92
|
+
{ type: 'text', text: 'John' },
|
|
93
|
+
{ type: 'text', text: 'ORD-12345' },
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Image message
|
|
101
|
+
await client.messages.sendImage({
|
|
102
|
+
phoneNumberId: '123456789',
|
|
103
|
+
to: '+6281234567890',
|
|
104
|
+
image: {
|
|
105
|
+
link: 'https://example.com/image.jpg',
|
|
106
|
+
caption: 'Check this out!',
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Document message
|
|
111
|
+
await client.messages.sendDocument({
|
|
112
|
+
phoneNumberId: '123456789',
|
|
113
|
+
to: '+6281234567890',
|
|
114
|
+
document: {
|
|
115
|
+
link: 'https://example.com/invoice.pdf',
|
|
116
|
+
filename: 'invoice.pdf',
|
|
117
|
+
caption: 'Your invoice',
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Reaction
|
|
122
|
+
await client.messages.sendReaction({
|
|
123
|
+
phoneNumberId: '123456789',
|
|
124
|
+
to: '+6281234567890',
|
|
125
|
+
reaction: {
|
|
126
|
+
messageId: 'wamid.xxx',
|
|
127
|
+
emoji: '👍',
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Interactive buttons
|
|
132
|
+
await client.messages.sendInteractive({
|
|
133
|
+
phoneNumberId: '123456789',
|
|
134
|
+
to: '+6281234567890',
|
|
135
|
+
interactive: {
|
|
136
|
+
type: 'button',
|
|
137
|
+
body: { text: 'Choose an option:' },
|
|
138
|
+
action: {
|
|
139
|
+
buttons: [
|
|
140
|
+
{ type: 'reply', reply: { id: 'yes', title: 'Yes' } },
|
|
141
|
+
{ type: 'reply', reply: { id: 'no', title: 'No' } },
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Templates
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// List all templates
|
|
152
|
+
const templates = await client.templates.list();
|
|
153
|
+
|
|
154
|
+
// Filter by status
|
|
155
|
+
const approved = await client.templates.list({ status: 'APPROVED' });
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Phone Numbers
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const phoneNumbers = await client.phoneNumbers.list();
|
|
162
|
+
|
|
163
|
+
for (const phone of phoneNumbers.data) {
|
|
164
|
+
console.log(`${phone.verified_name}: ${phone.display_phone_number}`);
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Usage Statistics
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const usage = await client.usage.get();
|
|
172
|
+
|
|
173
|
+
console.log(`Period: ${usage.data.period.start} - ${usage.data.period.end}`);
|
|
174
|
+
console.log(`Messages sent: ${usage.data.messages.sent}`);
|
|
175
|
+
console.log(`Messages received: ${usage.data.messages.received}`);
|
|
176
|
+
console.log(`API calls: ${usage.data.api_calls}`);
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Test Mode
|
|
180
|
+
|
|
181
|
+
Use `sk_test_*` API keys to test without sending real messages:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
const client = new SembojaClient('sk_test_your_test_key');
|
|
185
|
+
|
|
186
|
+
// Check if in test mode
|
|
187
|
+
console.log('Test mode:', client.isTestMode); // true
|
|
188
|
+
|
|
189
|
+
// Trigger a test webhook
|
|
190
|
+
await client.test.triggerWebhook({
|
|
191
|
+
phoneNumberId: '123456789',
|
|
192
|
+
type: 'text',
|
|
193
|
+
from: '+6281234567890',
|
|
194
|
+
text: 'Test incoming message',
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Webhook Verification
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { verifyWebhookSignature } from '@semboja/connect';
|
|
202
|
+
|
|
203
|
+
// Express.js example
|
|
204
|
+
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
|
|
205
|
+
const isValid = verifyWebhookSignature({
|
|
206
|
+
payload: req.body,
|
|
207
|
+
signature: req.headers['x-semboja-signature'] as string,
|
|
208
|
+
timestamp: req.headers['x-semboja-timestamp'] as string,
|
|
209
|
+
secret: process.env.WEBHOOK_SECRET!,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (!isValid) {
|
|
213
|
+
return res.status(401).send('Invalid signature');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const event = JSON.parse(req.body.toString());
|
|
217
|
+
console.log('Received event:', event);
|
|
218
|
+
|
|
219
|
+
res.status(200).send('OK');
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Error Handling
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import {
|
|
227
|
+
SembojaClient,
|
|
228
|
+
AuthenticationError,
|
|
229
|
+
RateLimitError,
|
|
230
|
+
ValidationError,
|
|
231
|
+
} from '@semboja/connect';
|
|
232
|
+
|
|
233
|
+
const client = new SembojaClient('sk_live_your_api_key');
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
await client.messages.sendText({
|
|
237
|
+
phoneNumberId: '123456789',
|
|
238
|
+
to: '+6281234567890',
|
|
239
|
+
text: 'Hello!',
|
|
240
|
+
});
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (error instanceof AuthenticationError) {
|
|
243
|
+
console.error('Invalid API key');
|
|
244
|
+
} else if (error instanceof RateLimitError) {
|
|
245
|
+
console.error('Rate limited, retry after:', error.resetAt);
|
|
246
|
+
} else if (error instanceof ValidationError) {
|
|
247
|
+
console.error('Invalid request:', error.message);
|
|
248
|
+
} else {
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## API Reference
|
|
255
|
+
|
|
256
|
+
### SembojaClient
|
|
257
|
+
|
|
258
|
+
| Property | Type | Description |
|
|
259
|
+
|----------|------|-------------|
|
|
260
|
+
| `messages` | `Messages` | Messages API |
|
|
261
|
+
| `templates` | `Templates` | Templates API |
|
|
262
|
+
| `phoneNumbers` | `PhoneNumbers` | Phone Numbers API |
|
|
263
|
+
| `usage` | `Usage` | Usage API |
|
|
264
|
+
| `test` | `Test` | Test API (test mode only) |
|
|
265
|
+
| `isTestMode` | `boolean` | Whether using test API key |
|
|
266
|
+
|
|
267
|
+
### Messages API
|
|
268
|
+
|
|
269
|
+
| Method | Description |
|
|
270
|
+
|--------|-------------|
|
|
271
|
+
| `sendText(options)` | Send a text message |
|
|
272
|
+
| `sendTemplate(options)` | Send a template message |
|
|
273
|
+
| `sendImage(options)` | Send an image |
|
|
274
|
+
| `sendVideo(options)` | Send a video |
|
|
275
|
+
| `sendAudio(options)` | Send an audio file |
|
|
276
|
+
| `sendDocument(options)` | Send a document |
|
|
277
|
+
| `sendReaction(options)` | Send a reaction |
|
|
278
|
+
| `sendInteractive(options)` | Send interactive message |
|
|
279
|
+
|
|
280
|
+
### Error Classes
|
|
281
|
+
|
|
282
|
+
| Class | Status Code | Description |
|
|
283
|
+
|-------|-------------|-------------|
|
|
284
|
+
| `SembojaError` | varies | Base error class |
|
|
285
|
+
| `AuthenticationError` | 401 | Invalid API key |
|
|
286
|
+
| `RateLimitError` | 429 | Rate limit exceeded |
|
|
287
|
+
| `ValidationError` | 400 | Invalid request |
|
|
288
|
+
| `NotFoundError` | 404 | Resource not found |
|
|
289
|
+
| `ServerError` | 500 | Server error |
|
|
290
|
+
| `NetworkError` | - | Network/connection error |
|
|
291
|
+
|
|
292
|
+
## Requirements
|
|
293
|
+
|
|
294
|
+
- Node.js 20.0.0 or higher
|
|
295
|
+
|
|
296
|
+
## Links
|
|
297
|
+
|
|
298
|
+
- [API Documentation](https://connect.semboja.tech/docs)
|
|
299
|
+
- [Console](https://console.semboja.tech)
|
|
300
|
+
- [Semboja](https://semboja.tech)
|
|
301
|
+
|
|
302
|
+
## License
|
|
303
|
+
|
|
304
|
+
MIT - [Semboja Tech](https://semboja.tech)
|