@workbenchcrm/sdk 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/LICENSE +21 -0
- package/README.md +341 -0
- package/dist/index.d.mts +1533 -0
- package/dist/index.d.ts +1533 -0
- package/dist/index.js +1082 -0
- package/dist/index.mjs +1043 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 KoalityCRM
|
|
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,341 @@
|
|
|
1
|
+
# @workbench/sdk
|
|
2
|
+
|
|
3
|
+
Official Node.js SDK for the [Workbench CRM](https://tryworkbench.app) API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @workbench/sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @workbench/sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @workbench/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { WorkbenchClient } from '@workbench/sdk';
|
|
19
|
+
|
|
20
|
+
// Initialize with your API key
|
|
21
|
+
const workbench = new WorkbenchClient({
|
|
22
|
+
apiKey: 'wbk_live_xxxxxxxxxxxxxxxxxxxxx'
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// List clients
|
|
26
|
+
const { data: clients, pagination } = await workbench.clients.list({
|
|
27
|
+
status: 'active',
|
|
28
|
+
per_page: 10
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Create an invoice
|
|
32
|
+
const { data: invoice } = await workbench.invoices.create({
|
|
33
|
+
client_id: clients[0].id,
|
|
34
|
+
items: [
|
|
35
|
+
{ description: 'Consulting Services', quantity: 2, unit_price: 150 }
|
|
36
|
+
],
|
|
37
|
+
tax_rate: 8.5
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Send the invoice
|
|
41
|
+
await workbench.invoices.send(invoice.id);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Authentication
|
|
45
|
+
|
|
46
|
+
### API Key Authentication
|
|
47
|
+
|
|
48
|
+
Get your API key from [Workbench Settings > API Keys](https://app.tryworkbench.app/settings/api-keys).
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const workbench = new WorkbenchClient({
|
|
52
|
+
apiKey: 'wbk_live_xxxxxxxxxxxxxxxxxxxxx'
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### OAuth Authentication
|
|
57
|
+
|
|
58
|
+
For third-party applications using OAuth 2.0:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const workbench = new WorkbenchClient({
|
|
62
|
+
accessToken: 'wbk_at_xxxxxxxxxxxxxxxxxxxxx'
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Configuration
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const workbench = new WorkbenchClient({
|
|
70
|
+
apiKey: 'wbk_live_xxx',
|
|
71
|
+
|
|
72
|
+
// Optional: Custom base URL (default: https://api.tryworkbench.app)
|
|
73
|
+
baseUrl: 'https://api.tryworkbench.app',
|
|
74
|
+
|
|
75
|
+
// Optional: Request timeout in milliseconds (default: 30000)
|
|
76
|
+
timeout: 30000,
|
|
77
|
+
|
|
78
|
+
// Optional: Maximum retries for failed requests (default: 3)
|
|
79
|
+
maxRetries: 3
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Resources
|
|
84
|
+
|
|
85
|
+
### Clients
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
// List clients
|
|
89
|
+
const { data, pagination } = await workbench.clients.list({
|
|
90
|
+
status: 'active',
|
|
91
|
+
search: 'john',
|
|
92
|
+
page: 1,
|
|
93
|
+
per_page: 20
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Get a client
|
|
97
|
+
const { data: client } = await workbench.clients.get('client-uuid');
|
|
98
|
+
|
|
99
|
+
// Create a client
|
|
100
|
+
const { data: newClient } = await workbench.clients.create({
|
|
101
|
+
first_name: 'John',
|
|
102
|
+
last_name: 'Doe',
|
|
103
|
+
email: 'john@example.com',
|
|
104
|
+
phone: '+1-555-123-4567',
|
|
105
|
+
company: 'Acme Corp',
|
|
106
|
+
status: 'active',
|
|
107
|
+
tags: ['vip', 'commercial']
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Update a client
|
|
111
|
+
const { data: updated } = await workbench.clients.update('client-uuid', {
|
|
112
|
+
status: 'inactive'
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Delete a client
|
|
116
|
+
await workbench.clients.delete('client-uuid');
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Invoices
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// List invoices
|
|
123
|
+
const { data, pagination } = await workbench.invoices.list({
|
|
124
|
+
status: 'sent',
|
|
125
|
+
client_id: 'client-uuid'
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Get an invoice
|
|
129
|
+
const { data: invoice } = await workbench.invoices.get('invoice-uuid');
|
|
130
|
+
|
|
131
|
+
// Create an invoice
|
|
132
|
+
const { data: newInvoice } = await workbench.invoices.create({
|
|
133
|
+
client_id: 'client-uuid',
|
|
134
|
+
due_date: '2024-02-15',
|
|
135
|
+
items: [
|
|
136
|
+
{ description: 'Web Development', quantity: 10, unit_price: 100 },
|
|
137
|
+
{ description: 'Hosting Setup', quantity: 1, unit_price: 50 }
|
|
138
|
+
],
|
|
139
|
+
tax_rate: 8.5,
|
|
140
|
+
notes: 'Payment due within 30 days'
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Update an invoice
|
|
144
|
+
await workbench.invoices.update('invoice-uuid', {
|
|
145
|
+
status: 'paid'
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Send an invoice
|
|
149
|
+
await workbench.invoices.send('invoice-uuid');
|
|
150
|
+
|
|
151
|
+
// Delete an invoice
|
|
152
|
+
await workbench.invoices.delete('invoice-uuid');
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Quotes
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// List quotes
|
|
159
|
+
const { data } = await workbench.quotes.list({ status: 'sent' });
|
|
160
|
+
|
|
161
|
+
// Create a quote
|
|
162
|
+
const { data: quote } = await workbench.quotes.create({
|
|
163
|
+
client_id: 'client-uuid',
|
|
164
|
+
valid_until: '2024-03-01',
|
|
165
|
+
items: [
|
|
166
|
+
{ description: 'Kitchen Renovation', quantity: 1, unit_price: 5000 }
|
|
167
|
+
]
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Send a quote
|
|
171
|
+
await workbench.quotes.send(quote.id);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Jobs
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// List jobs
|
|
178
|
+
const { data } = await workbench.jobs.list({
|
|
179
|
+
status: 'scheduled',
|
|
180
|
+
priority: 'high'
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Create a job
|
|
184
|
+
const { data: job } = await workbench.jobs.create({
|
|
185
|
+
client_id: 'client-uuid',
|
|
186
|
+
title: 'Kitchen Faucet Installation',
|
|
187
|
+
priority: 'high',
|
|
188
|
+
scheduled_start: '2024-01-20T09:00:00Z',
|
|
189
|
+
scheduled_end: '2024-01-20T12:00:00Z'
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Update job status
|
|
193
|
+
await workbench.jobs.update(job.id, {
|
|
194
|
+
status: 'completed',
|
|
195
|
+
actual_end: new Date().toISOString()
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Service Requests
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// List service requests
|
|
203
|
+
const { data } = await workbench.serviceRequests.list({ status: 'new' });
|
|
204
|
+
|
|
205
|
+
// Create a service request
|
|
206
|
+
const { data: request } = await workbench.serviceRequests.create({
|
|
207
|
+
title: 'AC Repair',
|
|
208
|
+
contact_name: 'Jane Smith',
|
|
209
|
+
contact_email: 'jane@example.com',
|
|
210
|
+
contact_phone: '+1-555-987-6543',
|
|
211
|
+
address: '123 Main St',
|
|
212
|
+
priority: 'urgent'
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Webhooks
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
// List webhooks
|
|
220
|
+
const { data: webhooks } = await workbench.webhooks.list();
|
|
221
|
+
|
|
222
|
+
// Create a webhook
|
|
223
|
+
const { data: webhook } = await workbench.webhooks.create({
|
|
224
|
+
name: 'Invoice Notifications',
|
|
225
|
+
url: 'https://example.com/webhooks/workbench',
|
|
226
|
+
events: ['invoice.created', 'invoice.paid']
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// IMPORTANT: Store the webhook secret securely!
|
|
230
|
+
console.log('Webhook secret:', webhook.secret);
|
|
231
|
+
|
|
232
|
+
// Update a webhook
|
|
233
|
+
await workbench.webhooks.update(webhook.id, {
|
|
234
|
+
events: ['invoice.created', 'invoice.paid', 'invoice.overdue']
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Delete a webhook
|
|
238
|
+
await workbench.webhooks.delete(webhook.id);
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Webhook Signature Verification
|
|
242
|
+
|
|
243
|
+
Verify that webhooks are actually from Workbench:
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { verifyWebhookSignature, constructWebhookEvent } from '@workbench/sdk';
|
|
247
|
+
import express from 'express';
|
|
248
|
+
|
|
249
|
+
const app = express();
|
|
250
|
+
|
|
251
|
+
// Use raw body for signature verification
|
|
252
|
+
app.post('/webhooks', express.raw({ type: 'application/json' }), (req, res) => {
|
|
253
|
+
const signature = req.headers['x-workbench-signature'] as string;
|
|
254
|
+
const secret = process.env.WEBHOOK_SECRET!;
|
|
255
|
+
|
|
256
|
+
try {
|
|
257
|
+
// Option 1: Just verify
|
|
258
|
+
verifyWebhookSignature(req.body, signature, secret);
|
|
259
|
+
const event = JSON.parse(req.body.toString());
|
|
260
|
+
|
|
261
|
+
// Option 2: Verify and parse in one step
|
|
262
|
+
const event = constructWebhookEvent(req.body, signature, secret);
|
|
263
|
+
|
|
264
|
+
// Handle the event
|
|
265
|
+
switch (event.event) {
|
|
266
|
+
case 'invoice.paid':
|
|
267
|
+
console.log('Invoice paid:', event.data.id);
|
|
268
|
+
break;
|
|
269
|
+
case 'client.created':
|
|
270
|
+
console.log('New client:', event.data.first_name);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
res.sendStatus(200);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.error('Webhook verification failed:', error);
|
|
277
|
+
res.sendStatus(400);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Error Handling
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import { WorkbenchClient, WorkbenchError } from '@workbench/sdk';
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
const { data } = await workbench.clients.get('invalid-uuid');
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (error instanceof WorkbenchError) {
|
|
291
|
+
console.error('API Error:', error.message);
|
|
292
|
+
console.error('Status:', error.status);
|
|
293
|
+
console.error('Code:', error.code);
|
|
294
|
+
console.error('Request ID:', error.requestId);
|
|
295
|
+
|
|
296
|
+
if (error.details) {
|
|
297
|
+
error.details.forEach(d => {
|
|
298
|
+
console.error(` ${d.field}: ${d.message}`);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## TypeScript Support
|
|
306
|
+
|
|
307
|
+
This SDK is written in TypeScript and includes full type definitions:
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import type {
|
|
311
|
+
Client,
|
|
312
|
+
Invoice,
|
|
313
|
+
Quote,
|
|
314
|
+
Job,
|
|
315
|
+
ServiceRequest,
|
|
316
|
+
Webhook,
|
|
317
|
+
CreateClientOptions,
|
|
318
|
+
InvoiceStatus
|
|
319
|
+
} from '@workbench/sdk';
|
|
320
|
+
|
|
321
|
+
// Types are inferred from methods
|
|
322
|
+
const { data: clients } = await workbench.clients.list();
|
|
323
|
+
// clients is Client[]
|
|
324
|
+
|
|
325
|
+
const { data: invoice } = await workbench.invoices.get('uuid');
|
|
326
|
+
// invoice is Invoice
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## API Documentation
|
|
330
|
+
|
|
331
|
+
For complete API documentation, visit [docs.tryworkbench.app](https://docs.tryworkbench.app).
|
|
332
|
+
|
|
333
|
+
## Support
|
|
334
|
+
|
|
335
|
+
- **Documentation**: [docs.tryworkbench.app](https://docs.tryworkbench.app)
|
|
336
|
+
- **Issues**: [GitHub Issues](https://github.com/KoalityCRM/workbench-sdk-node/issues)
|
|
337
|
+
- **Email**: support@tryworkbench.app
|
|
338
|
+
|
|
339
|
+
## License
|
|
340
|
+
|
|
341
|
+
MIT License - see [LICENSE](LICENSE) for details.
|