@zapyapi/sdk 1.0.0-beta.1

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 (44) hide show
  1. package/README.md +308 -0
  2. package/index.cjs +1176 -0
  3. package/index.js +1149 -0
  4. package/package.json +55 -0
  5. package/src/client.d.ts +98 -0
  6. package/src/client.d.ts.map +1 -0
  7. package/src/errors.d.ts +70 -0
  8. package/src/errors.d.ts.map +1 -0
  9. package/src/generated/Api.d.ts +1542 -0
  10. package/src/generated/Api.d.ts.map +1 -0
  11. package/src/index.d.ts +38 -0
  12. package/src/index.d.ts.map +1 -0
  13. package/src/resources/base.resource.d.ts +17 -0
  14. package/src/resources/base.resource.d.ts.map +1 -0
  15. package/src/resources/index.d.ts +8 -0
  16. package/src/resources/index.d.ts.map +1 -0
  17. package/src/resources/instances.resource.d.ts +75 -0
  18. package/src/resources/instances.resource.d.ts.map +1 -0
  19. package/src/resources/messages.resource.d.ts +159 -0
  20. package/src/resources/messages.resource.d.ts.map +1 -0
  21. package/src/resources/webhooks.resource.d.ts +115 -0
  22. package/src/resources/webhooks.resource.d.ts.map +1 -0
  23. package/src/types/common.types.d.ts +89 -0
  24. package/src/types/common.types.d.ts.map +1 -0
  25. package/src/types/enums.d.ts +142 -0
  26. package/src/types/enums.d.ts.map +1 -0
  27. package/src/types/index.d.ts +11 -0
  28. package/src/types/index.d.ts.map +1 -0
  29. package/src/types/instances.types.d.ts +114 -0
  30. package/src/types/instances.types.d.ts.map +1 -0
  31. package/src/types/messages.types.d.ts +166 -0
  32. package/src/types/messages.types.d.ts.map +1 -0
  33. package/src/types/webhook-config.types.d.ts +60 -0
  34. package/src/types/webhook-config.types.d.ts.map +1 -0
  35. package/src/types/webhook-events.types.d.ts +232 -0
  36. package/src/types/webhook-events.types.d.ts.map +1 -0
  37. package/src/utils/index.d.ts +6 -0
  38. package/src/utils/index.d.ts.map +1 -0
  39. package/src/utils/phone.d.ts +38 -0
  40. package/src/utils/phone.d.ts.map +1 -0
  41. package/src/utils/webhook-signature.d.ts +69 -0
  42. package/src/utils/webhook-signature.d.ts.map +1 -0
  43. package/src/version.d.ts +3 -0
  44. package/src/version.d.ts.map +1 -0
package/README.md ADDED
@@ -0,0 +1,308 @@
1
+ # @zapyapi/sdk
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@zapyapi/sdk.svg)](https://www.npmjs.com/package/@zapyapi/sdk)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@zapyapi/sdk.svg)](https://www.npmjs.com/package/@zapyapi/sdk)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D18-brightgreen.svg)](https://nodejs.org/)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0%2B-blue.svg)](https://www.typescriptlang.org/)
8
+
9
+ Official TypeScript SDK for [ZapyAPI](https://zapyapi.com) - REST API for WhatsApp integration.
10
+
11
+ Send messages, manage instances, and receive webhooks with a simple API. No infrastructure to manage.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @zapyapi/sdk
17
+ # or
18
+ yarn add @zapyapi/sdk
19
+ # or
20
+ pnpm add @zapyapi/sdk
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ ```typescript
26
+ import { ZapyClient } from '@zapyapi/sdk';
27
+
28
+ // Initialize the client
29
+ const client = new ZapyClient({
30
+ apiKey: 'your-api-key', // Get yours at https://app.zapyapi.com/
31
+ });
32
+
33
+ // Send a text message
34
+ const response = await client.messages.sendText('my-instance', {
35
+ to: '5511999999999',
36
+ text: 'Hello from ZapyAPI SDK!',
37
+ });
38
+
39
+ console.log(`Message sent with ID: ${response.messageId}`);
40
+ ```
41
+
42
+ ## Features
43
+
44
+ - **Type-safe** - Full TypeScript support with comprehensive types
45
+ - **Dual Format** - ESM and CommonJS support out of the box
46
+ - **Modern** - Works with Node.js 18+
47
+ - **Simple** - Intuitive API design for great developer experience
48
+ - **Complete** - Covers all ZapyAPI endpoints (instances, messages, webhooks)
49
+
50
+ ## API Reference
51
+
52
+ ### Client Initialization
53
+
54
+ ```typescript
55
+ import { ZapyClient, createClient } from '@zapyapi/sdk';
56
+
57
+ // Using constructor
58
+ const client = new ZapyClient({
59
+ apiKey: 'your-api-key',
60
+ baseUrl: 'https://api.zapyapi.com/api', // optional
61
+ timeout: 30000, // optional, in milliseconds
62
+ });
63
+
64
+ // Using factory function
65
+ const client = createClient({ apiKey: 'your-api-key' });
66
+ ```
67
+
68
+ ### Instances
69
+
70
+ ```typescript
71
+ // List all instances
72
+ const instances = await client.instances.list({ page: 1, limit: 10 });
73
+ console.log(`Total: ${instances.total} instances`);
74
+
75
+ // Create a new instance (Partner only)
76
+ const instance = await client.instances.create({
77
+ name: 'My Instance',
78
+ metadata: { department: 'sales' },
79
+ });
80
+
81
+ // Get QR code for connection
82
+ const qr = await client.instances.getQRCode('my-instance');
83
+ // qr.qrCode is a base64 encoded image
84
+
85
+ // Restart an instance
86
+ await client.instances.restart('my-instance');
87
+
88
+ // Logout from WhatsApp
89
+ await client.instances.logout('my-instance');
90
+ ```
91
+
92
+ ### Messages
93
+
94
+ #### Send Text Message
95
+
96
+ ```typescript
97
+ const response = await client.messages.sendText('my-instance', {
98
+ to: '5511999999999',
99
+ text: 'Hello, World!',
100
+ quotedMessageId: 'ABC123...', // optional, for replies
101
+ });
102
+ ```
103
+
104
+ #### Send Image
105
+
106
+ ```typescript
107
+ // Using URL
108
+ await client.messages.sendImage('my-instance', {
109
+ to: '5511999999999',
110
+ url: 'https://example.com/image.jpg',
111
+ caption: 'Check this out!', // optional
112
+ });
113
+
114
+ // Using Base64
115
+ await client.messages.sendImage('my-instance', {
116
+ to: '5511999999999',
117
+ base64: 'data:image/jpeg;base64,...',
118
+ caption: 'Check this out!',
119
+ });
120
+ ```
121
+
122
+ #### Send Video
123
+
124
+ ```typescript
125
+ await client.messages.sendVideo('my-instance', {
126
+ to: '5511999999999',
127
+ url: 'https://example.com/video.mp4',
128
+ caption: 'Watch this!',
129
+ });
130
+ ```
131
+
132
+ #### Send Audio Note (Voice Message)
133
+
134
+ ```typescript
135
+ await client.messages.sendAudioNote('my-instance', {
136
+ to: '5511999999999',
137
+ url: 'https://example.com/audio.ogg',
138
+ });
139
+ ```
140
+
141
+ #### Send Document
142
+
143
+ ```typescript
144
+ await client.messages.sendDocument('my-instance', {
145
+ to: '5511999999999',
146
+ url: 'https://example.com/document.pdf',
147
+ filename: 'report.pdf',
148
+ caption: 'Here is the report',
149
+ });
150
+ ```
151
+
152
+ #### Other Message Operations
153
+
154
+ ```typescript
155
+ // Forward a message
156
+ await client.messages.forward('my-instance', {
157
+ to: '5511888888888',
158
+ messageId: 'ABC123...',
159
+ });
160
+
161
+ // Edit a message
162
+ await client.messages.edit('my-instance', {
163
+ messageId: 'ABC123...',
164
+ text: 'Updated message text',
165
+ });
166
+
167
+ // Mark as read
168
+ await client.messages.markAsRead('my-instance', 'ABC123...');
169
+
170
+ // Delete a message
171
+ await client.messages.delete('my-instance', 'ABC123...');
172
+
173
+ // Get media download link
174
+ const media = await client.messages.getMediaDownloadLink('my-instance', 'ABC123...');
175
+ console.log(media.url);
176
+ ```
177
+
178
+ ### Webhooks
179
+
180
+ ```typescript
181
+ // Configure webhooks
182
+ await client.webhooks.configure('my-instance', {
183
+ url: 'https://your-server.com/webhook',
184
+ events: ['message', 'message.ack', 'connection.update'],
185
+ headers: {
186
+ 'X-Custom-Header': 'your-value',
187
+ },
188
+ });
189
+
190
+ // Get current configuration
191
+ const config = await client.webhooks.get('my-instance');
192
+
193
+ // Delete webhook configuration
194
+ await client.webhooks.delete('my-instance');
195
+ ```
196
+
197
+ ## Error Handling
198
+
199
+ ```typescript
200
+ import {
201
+ ZapyClient,
202
+ ZapyApiError,
203
+ AuthenticationError,
204
+ RateLimitError,
205
+ ValidationError,
206
+ } from '@zapyapi/sdk';
207
+
208
+ try {
209
+ await client.messages.sendText('my-instance', {
210
+ to: '5511999999999',
211
+ text: 'Hello!',
212
+ });
213
+ } catch (error) {
214
+ if (error instanceof AuthenticationError) {
215
+ console.error('Invalid API key');
216
+ } else if (error instanceof RateLimitError) {
217
+ console.error(`Rate limited. Retry after: ${error.retryAfter}ms`);
218
+ } else if (error instanceof ZapyApiError) {
219
+ console.error(`API Error [${error.code}]: ${error.message}`);
220
+ console.error(`Request ID: ${error.requestId}`);
221
+ }
222
+ }
223
+ ```
224
+
225
+ ## Utilities
226
+
227
+ ```typescript
228
+ import { normalizePhone, isValidPhone, isGroup, extractPhone } from '@zapyapi/sdk';
229
+
230
+ // Normalize phone numbers
231
+ normalizePhone('11999999999'); // '5511999999999'
232
+ normalizePhone('+5511999999999'); // '5511999999999'
233
+
234
+ // Validate phone numbers
235
+ isValidPhone('5511999999999'); // true
236
+ isValidPhone('5511999999999@c.us'); // true
237
+
238
+ // Check if JID is a group
239
+ isGroup('123456789@g.us'); // true
240
+ isGroup('5511999999999@c.us'); // false
241
+
242
+ // Extract phone from JID
243
+ extractPhone('5511999999999@c.us'); // '5511999999999'
244
+ ```
245
+
246
+ ## Webhook Event Types
247
+
248
+ The SDK provides types for webhook events:
249
+
250
+ ```typescript
251
+ import type {
252
+ WebhookEvent,
253
+ MessageWebhookEvent,
254
+ MessageAckWebhookEvent,
255
+ ConnectionWebhookEvent,
256
+ QRCodeWebhookEvent,
257
+ } from '@zapyapi/sdk';
258
+
259
+ function handleWebhook(event: WebhookEvent) {
260
+ switch (event.event) {
261
+ case 'message':
262
+ // event is MessageWebhookEvent
263
+ console.log(`New message from ${event.data.from}`);
264
+ break;
265
+ case 'message.ack':
266
+ // event is MessageAckWebhookEvent
267
+ console.log(`Message ${event.data.messageId} status: ${event.data.ack}`);
268
+ break;
269
+ case 'connection.update':
270
+ // event is ConnectionWebhookEvent
271
+ console.log(`Connection status: ${event.data.status}`);
272
+ break;
273
+ }
274
+ }
275
+ ```
276
+
277
+ ## Requirements
278
+
279
+ - Node.js 18 or higher
280
+ - TypeScript 5.0+ (for TypeScript users)
281
+
282
+ ## CommonJS Usage
283
+
284
+ The SDK also works with CommonJS:
285
+
286
+ ```javascript
287
+ const { ZapyClient } = require('@zapyapi/sdk');
288
+
289
+ const client = new ZapyClient({ apiKey: 'your-api-key' });
290
+ ```
291
+
292
+ ## License
293
+
294
+ MIT
295
+
296
+ ## Links
297
+
298
+ - [Documentation](https://docs.zapyapi.com)
299
+ - [Dashboard](https://app.zapyapi.com)
300
+ - [API Reference](https://docs.zapyapi.com/api)
301
+
302
+ ## Support
303
+
304
+ - Email: contato@zapyapi.com
305
+
306
+ ---
307
+
308
+ Made with :heart: by [ZapyAPI](https://zapyapi.com)