@delegance/sdk 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/README.md +308 -0
- package/dist/index.d.mts +1616 -0
- package/dist/index.d.ts +1616 -0
- package/dist/index.js +1474 -0
- package/dist/index.mjs +1425 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# @delegance/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for integrating with the Delegance Brokerage platform.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @delegance/sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @delegance/sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @delegance/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { DeleganceClient } from '@delegance/sdk';
|
|
19
|
+
import fs from 'fs';
|
|
20
|
+
|
|
21
|
+
// Initialize the client
|
|
22
|
+
const client = new DeleganceClient({
|
|
23
|
+
apiKey: 'dlg_xxxxxxxx_sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Create a company
|
|
27
|
+
const company = await client.companies.create({
|
|
28
|
+
name: 'ABC Construction',
|
|
29
|
+
industry: 'construction',
|
|
30
|
+
companySize: '11-25',
|
|
31
|
+
contactEmail: 'contact@abcconstruction.com',
|
|
32
|
+
fein: '12-3456789',
|
|
33
|
+
address: {
|
|
34
|
+
street: '123 Main St',
|
|
35
|
+
city: 'Austin',
|
|
36
|
+
state: 'TX',
|
|
37
|
+
zip: '78701'
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
console.log('Created company:', company.id);
|
|
42
|
+
|
|
43
|
+
// Upload a policy document
|
|
44
|
+
const doc = await client.documents.upload({
|
|
45
|
+
companyId: company.id,
|
|
46
|
+
file: fs.readFileSync('./GL-Policy-2025.pdf'),
|
|
47
|
+
fileName: 'GL-Policy-2025.pdf',
|
|
48
|
+
documentType: 'policy'
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log('Document uploaded:', doc.id, 'Status:', doc.status);
|
|
52
|
+
|
|
53
|
+
// Wait for extraction to complete
|
|
54
|
+
const result = await client.documents.waitForExtraction(doc.id, {
|
|
55
|
+
timeoutMs: 60000,
|
|
56
|
+
onStatusChange: (status) => console.log('Processing:', status)
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (result.extraction) {
|
|
60
|
+
console.log('Extracted policies:', result.extraction.policies);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Configuration
|
|
65
|
+
|
|
66
|
+
### Client Options
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const client = new DeleganceClient({
|
|
70
|
+
// Required: Your API key
|
|
71
|
+
apiKey: 'dlg_xxxxxxxx_sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
72
|
+
|
|
73
|
+
// Optional: API base URL (default: https://app.delegance.ai)
|
|
74
|
+
baseUrl: 'https://app.delegance.ai',
|
|
75
|
+
|
|
76
|
+
// Optional: Request timeout in ms (default: 30000)
|
|
77
|
+
timeout: 30000,
|
|
78
|
+
|
|
79
|
+
// Optional: Number of retry attempts (default: 3)
|
|
80
|
+
retries: 3,
|
|
81
|
+
|
|
82
|
+
// Optional: Custom headers
|
|
83
|
+
headers: {
|
|
84
|
+
'X-Custom-Header': 'value'
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Environment Variables
|
|
90
|
+
|
|
91
|
+
You can also create a client from environment variables:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { createClientFromEnv } from '@delegance/sdk';
|
|
95
|
+
|
|
96
|
+
// Reads DELEGANCE_API_KEY and optionally DELEGANCE_BASE_URL
|
|
97
|
+
const client = createClientFromEnv();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## API Reference
|
|
101
|
+
|
|
102
|
+
### Companies
|
|
103
|
+
|
|
104
|
+
#### Create a Company
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const company = await client.companies.create({
|
|
108
|
+
name: 'ABC Construction', // Required
|
|
109
|
+
industry: 'construction', // Required
|
|
110
|
+
companySize: '11-25', // Required: '1', '2-10', '11-25', '26-50', '51-100', '100-499', '500+'
|
|
111
|
+
contactEmail: 'contact@abc.com', // Required
|
|
112
|
+
|
|
113
|
+
// Optional fields
|
|
114
|
+
legalName: 'ABC Construction LLC',
|
|
115
|
+
fein: '12-3456789',
|
|
116
|
+
website: 'https://abc.com',
|
|
117
|
+
yearEstablished: '2015',
|
|
118
|
+
companyStructure: 'LLC',
|
|
119
|
+
description: 'Commercial construction company',
|
|
120
|
+
contactPhone: '+1-555-123-4567',
|
|
121
|
+
externalId: 'ams-12345', // For deduplication
|
|
122
|
+
address: {
|
|
123
|
+
street: '123 Main St',
|
|
124
|
+
suite: 'Suite 100',
|
|
125
|
+
city: 'Austin',
|
|
126
|
+
state: 'TX',
|
|
127
|
+
zip: '78701'
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### List Companies
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
// List your company
|
|
136
|
+
const companies = await client.companies.list();
|
|
137
|
+
|
|
138
|
+
// Include companies created via this API key
|
|
139
|
+
const allCompanies = await client.companies.list({ includeCreated: true });
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Upsert (Create if not exists)
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const { company, isExisting } = await client.companies.upsert({
|
|
146
|
+
name: 'ABC Construction',
|
|
147
|
+
industry: 'construction',
|
|
148
|
+
companySize: '11-25',
|
|
149
|
+
contactEmail: 'contact@abc.com',
|
|
150
|
+
externalId: 'ams-12345' // Deduplication key
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (isExisting) {
|
|
154
|
+
console.log('Company already existed');
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Documents
|
|
159
|
+
|
|
160
|
+
#### Upload a Document
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import fs from 'fs';
|
|
164
|
+
|
|
165
|
+
const doc = await client.documents.upload({
|
|
166
|
+
companyId: 'company-uuid',
|
|
167
|
+
file: fs.readFileSync('./policy.pdf'), // Buffer, Blob, or base64 string
|
|
168
|
+
fileName: 'policy.pdf',
|
|
169
|
+
|
|
170
|
+
// Optional
|
|
171
|
+
documentType: 'policy', // 'policy', 'loss_run', 'financial_statement', 'sov', 'application', 'certificate', 'endorsement', 'bor', 'other'
|
|
172
|
+
externalId: 'doc-123', // For deduplication
|
|
173
|
+
metadata: { source: 'ams' }
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### Upload Multiple Documents
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
const { documents, errors } = await client.documents.uploadBatch([
|
|
181
|
+
{ companyId, file: buffer1, fileName: 'policy1.pdf' },
|
|
182
|
+
{ companyId, file: buffer2, fileName: 'policy2.pdf' },
|
|
183
|
+
{ companyId, file: buffer3, fileName: 'policy3.pdf' }
|
|
184
|
+
], { concurrency: 3 });
|
|
185
|
+
|
|
186
|
+
console.log(`Uploaded ${documents.length} documents`);
|
|
187
|
+
if (errors.length > 0) {
|
|
188
|
+
console.error('Failed uploads:', errors);
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Get Document Status
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
const doc = await client.documents.get('document-uuid');
|
|
196
|
+
|
|
197
|
+
console.log('Status:', doc.status);
|
|
198
|
+
// 'pending' | 'processing' | 'completed' | 'processed' | 'error' | 'failed'
|
|
199
|
+
|
|
200
|
+
if (doc.extraction) {
|
|
201
|
+
console.log('Extracted policies:', doc.extraction.policies);
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
#### List Documents
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
// List all documents
|
|
209
|
+
const { documents, pagination } = await client.documents.list();
|
|
210
|
+
|
|
211
|
+
// Filter by type and status
|
|
212
|
+
const policies = await client.documents.list({
|
|
213
|
+
documentType: 'policy',
|
|
214
|
+
status: 'completed',
|
|
215
|
+
limit: 50,
|
|
216
|
+
offset: 0
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Paginate
|
|
220
|
+
while (pagination.hasMore) {
|
|
221
|
+
const next = await client.documents.list({ offset: pagination.offset + 50 });
|
|
222
|
+
// ...
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### Wait for Extraction
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
const result = await client.documents.waitForExtraction('document-uuid', {
|
|
230
|
+
timeoutMs: 120000, // 2 minutes max
|
|
231
|
+
pollIntervalMs: 2000, // Check every 2 seconds
|
|
232
|
+
onStatusChange: (status) => console.log('Status:', status)
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
if (result.status === 'completed') {
|
|
236
|
+
console.log('Policies found:', result.extraction?.policies.length);
|
|
237
|
+
} else if (result.status === 'error') {
|
|
238
|
+
console.error('Processing failed:', result.error?.message);
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Error Handling
|
|
243
|
+
|
|
244
|
+
The SDK throws typed errors for different scenarios:
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import {
|
|
248
|
+
AuthenticationError,
|
|
249
|
+
ValidationError,
|
|
250
|
+
NotFoundError,
|
|
251
|
+
RateLimitError,
|
|
252
|
+
DeleganceError
|
|
253
|
+
} from '@delegance/sdk';
|
|
254
|
+
|
|
255
|
+
try {
|
|
256
|
+
await client.documents.get('invalid-uuid');
|
|
257
|
+
} catch (error) {
|
|
258
|
+
if (error instanceof AuthenticationError) {
|
|
259
|
+
console.error('Invalid API key');
|
|
260
|
+
} else if (error instanceof NotFoundError) {
|
|
261
|
+
console.error('Document not found');
|
|
262
|
+
} else if (error instanceof ValidationError) {
|
|
263
|
+
console.error('Invalid input:', error.details);
|
|
264
|
+
} else if (error instanceof RateLimitError) {
|
|
265
|
+
console.error('Rate limited, retry after:', error.retryAfter);
|
|
266
|
+
} else if (error instanceof DeleganceError) {
|
|
267
|
+
console.error('API error:', error.message, 'Status:', error.statusCode);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## TypeScript Support
|
|
273
|
+
|
|
274
|
+
The SDK is written in TypeScript and includes full type definitions:
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
import type {
|
|
278
|
+
Company,
|
|
279
|
+
Document,
|
|
280
|
+
DocumentWithExtraction,
|
|
281
|
+
ExtractedPolicy,
|
|
282
|
+
CreateCompanyInput,
|
|
283
|
+
UploadDocumentInput
|
|
284
|
+
} from '@delegance/sdk';
|
|
285
|
+
|
|
286
|
+
// Use Zod schemas for validation
|
|
287
|
+
import { CreateCompanySchema, DocumentTypeSchema } from '@delegance/sdk';
|
|
288
|
+
|
|
289
|
+
const result = CreateCompanySchema.safeParse(data);
|
|
290
|
+
if (!result.success) {
|
|
291
|
+
console.error(result.error.issues);
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Getting an API Key
|
|
296
|
+
|
|
297
|
+
1. Log in to the Delegance dashboard
|
|
298
|
+
2. Navigate to Settings > API Keys
|
|
299
|
+
3. Click "Create API Key"
|
|
300
|
+
4. Copy the key (it's only shown once!)
|
|
301
|
+
|
|
302
|
+
API keys have the format: `dlg_xxxxxxxx_sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`
|
|
303
|
+
|
|
304
|
+
## Support
|
|
305
|
+
|
|
306
|
+
- Documentation: https://docs.delegance.ai
|
|
307
|
+
- Issues: https://github.com/delegance/delegance-sdk/issues
|
|
308
|
+
- Email: support@delegance.ai
|