@veryfi/veryfi-sdk 1.2.3 → 1.2.5
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 +4 -1
- package/lib/main.js +91 -15
- package/lib/types/main.d.ts +71 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ You may need to run the above command with `sudo`.
|
|
|
38
38
|
|
|
39
39
|
### Obtaining Client ID and user keys
|
|
40
40
|
|
|
41
|
-
If you don't have an account with Veryfi, please go ahead and register here: [https://
|
|
41
|
+
If you don't have an account with Veryfi, please go ahead and register here: [https://app.veryfi.com/signup/api/](https://app.veryfi.com/signup/api/)
|
|
42
42
|
|
|
43
43
|
### Node.js API Client Library
|
|
44
44
|
|
|
@@ -248,11 +248,14 @@ veryfi_client.update_document(id=12345,
|
|
|
248
248
|
```
|
|
249
249
|
|
|
250
250
|
## Need help?
|
|
251
|
+
Visit https://docs.veryfi.com/ to access integration guides and usage notes in the Veryfi API Documentation Portal
|
|
251
252
|
|
|
252
253
|
If you run into any issue or need help installing or using the library, please contact support@veryfi.com.
|
|
253
254
|
|
|
254
255
|
If you found a bug in this library or would like new features added, then open an issue or pull requests against this repo!
|
|
255
256
|
|
|
257
|
+
### [API Docs](https://docs.veryfi.com/)
|
|
258
|
+
|
|
256
259
|
### [Learn more at our blog](https://www.veryfi.com/nodejs/)
|
|
257
260
|
|
|
258
261
|
## Tutorial Video
|
package/lib/main.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const {createHmac} = require('crypto');
|
|
2
2
|
const axios = require('axios').default;
|
|
3
3
|
const path = require('path');
|
|
4
|
+
const FormData = require('form-data');
|
|
4
5
|
const fs = require('fs');
|
|
6
|
+
const {promisify} = require("util");
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Create instance of a Client
|
|
@@ -21,12 +23,13 @@ function Client(
|
|
|
21
23
|
api_key,
|
|
22
24
|
base_url = "https://api.veryfi.com/",
|
|
23
25
|
api_version = "v8",
|
|
24
|
-
timeout = 120
|
|
26
|
+
timeout = 120) {
|
|
25
27
|
this.client_id = client_id;
|
|
26
28
|
this.client_secret = client_secret;
|
|
27
29
|
this.username = username;
|
|
28
30
|
this.api_key = api_key;
|
|
29
31
|
this.base_url = base_url;
|
|
32
|
+
this.timeout = timeout;
|
|
30
33
|
this.api_version = api_version;
|
|
31
34
|
this.CATEGORIES = [
|
|
32
35
|
"Advertising & Marketing",
|
|
@@ -64,7 +67,7 @@ Client.prototype._check_w2_version = function () {
|
|
|
64
67
|
*/
|
|
65
68
|
Client.prototype._get_headers = function (has_files = false) {
|
|
66
69
|
let final_headers = {
|
|
67
|
-
"User-Agent": "Node.js Veryfi-Nodejs/1.2.
|
|
70
|
+
"User-Agent": "Node.js Veryfi-Nodejs/1.2.5",
|
|
68
71
|
"Accept": "application/json",
|
|
69
72
|
"Content-Type": "application/json",
|
|
70
73
|
"Client-Id": this.client_id,
|
|
@@ -117,11 +120,10 @@ Client.prototype._generate_signature = function (payload_params, timestamp) {
|
|
|
117
120
|
* @param {string} endpoint_name Endpoint name such as 'documents', 'users', etc.
|
|
118
121
|
* @param {{}} request_arguments JSON payload to send to Veryfi
|
|
119
122
|
* @param params {{}} query params.
|
|
120
|
-
* @param
|
|
123
|
+
* @param {Boolean} has_files Are there any files to be submitted as binary
|
|
121
124
|
* @returns {JSON} A JSON of the response data.
|
|
122
125
|
*/
|
|
123
|
-
Client.prototype._request = async function (http_verb, endpoint_name, request_arguments, params = {},
|
|
124
|
-
let has_files = (Boolean(file_stream));
|
|
126
|
+
Client.prototype._request = async function (http_verb, endpoint_name, request_arguments, params = {}, has_files = false) {
|
|
125
127
|
let headers = this._get_headers(has_files);
|
|
126
128
|
let api_url = `${this._get_url()}/partner${endpoint_name}`;
|
|
127
129
|
|
|
@@ -134,12 +136,21 @@ Client.prototype._request = async function (http_verb, endpoint_name, request_ar
|
|
|
134
136
|
});
|
|
135
137
|
}
|
|
136
138
|
|
|
137
|
-
if (has_files
|
|
138
|
-
|
|
139
|
+
if (has_files) {
|
|
140
|
+
const formData = new FormData();
|
|
141
|
+
for (const key in request_arguments) {
|
|
142
|
+
if (request_arguments.hasOwnProperty(key)) {
|
|
143
|
+
const value = request_arguments[key];
|
|
144
|
+
formData.append(key, value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
headers = Object.assign(headers, formData.getHeaders());
|
|
148
|
+
const getLength = promisify(formData.getLength).bind(formData);
|
|
149
|
+
headers['Content-Length'] = await getLength()
|
|
150
|
+
request_arguments = formData;
|
|
139
151
|
} else {
|
|
140
|
-
|
|
152
|
+
request_arguments = JSON.stringify(request_arguments);
|
|
141
153
|
}
|
|
142
|
-
|
|
143
154
|
try {
|
|
144
155
|
return await axios.request({
|
|
145
156
|
method: http_verb,
|
|
@@ -150,9 +161,10 @@ Client.prototype._request = async function (http_verb, endpoint_name, request_ar
|
|
|
150
161
|
params: params
|
|
151
162
|
});
|
|
152
163
|
} catch (response) {
|
|
153
|
-
let errorStatus = response
|
|
154
|
-
let errorInfo = response
|
|
155
|
-
|
|
164
|
+
let errorStatus = response?.response?.status;
|
|
165
|
+
let errorInfo = response?.response?.data?.error;
|
|
166
|
+
let message = response?.message;
|
|
167
|
+
throw new Error(`Error: ${errorStatus} ${errorInfo} ${message}`);
|
|
156
168
|
}
|
|
157
169
|
}
|
|
158
170
|
|
|
@@ -228,21 +240,57 @@ Client.prototype.process_document = async function (
|
|
|
228
240
|
/**
|
|
229
241
|
* Process a document and extract all the fields from it
|
|
230
242
|
* @example
|
|
231
|
-
* veryfi_client.
|
|
243
|
+
* veryfi_client.process_document_stream('ReadStream',
|
|
244
|
+
* 'receipt.png',
|
|
245
|
+
* ['Entertainment','Food'],
|
|
246
|
+
* true,
|
|
247
|
+
* {'extra': 'parameters'})
|
|
248
|
+
*
|
|
249
|
+
* @memberof Client
|
|
250
|
+
* @param {ReadStream} file ReadStream of a file to submit for data extraction
|
|
251
|
+
* @param {String} file_name The file name including the extension
|
|
252
|
+
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
253
|
+
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
254
|
+
* @param {Object} kwargs Additional request parameters
|
|
255
|
+
* @returns {JSON} Data extracted from the document
|
|
256
|
+
*/
|
|
257
|
+
Client.prototype.process_document_stream = async function (
|
|
258
|
+
file,
|
|
259
|
+
file_name,
|
|
260
|
+
delete_after_processing = false,
|
|
261
|
+
{...kwargs} = {}
|
|
262
|
+
) {
|
|
263
|
+
|
|
264
|
+
let endpoint_name = "/documents/";
|
|
265
|
+
|
|
266
|
+
let request_arguments = {
|
|
267
|
+
"file_name": file_name,
|
|
268
|
+
"file": file,
|
|
269
|
+
"auto_delete": delete_after_processing.toString(),
|
|
270
|
+
};
|
|
271
|
+
request_arguments = Object.assign(request_arguments, kwargs);
|
|
272
|
+
let document = await this._request("POST", endpoint_name, request_arguments, {}, true);
|
|
273
|
+
return document['data'];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Process a document and extract all the fields from it
|
|
278
|
+
* @example
|
|
279
|
+
* veryfi_client.process_document_base64string('base64_encoded_string',
|
|
232
280
|
* 'receipt.png',
|
|
233
281
|
* ['Entertainment','Food'],
|
|
234
282
|
* true,
|
|
235
283
|
* {'extra': 'parameters'})
|
|
236
284
|
*
|
|
237
285
|
* @memberof Client
|
|
238
|
-
* @param {String} base64_encoded_string Buffer of a file to submit for data extraction
|
|
286
|
+
* @param {String} base64_encoded_string Buffer string of a file to submit for data extraction
|
|
239
287
|
* @param {String} file_name The file name including the extension
|
|
240
288
|
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
241
289
|
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
242
290
|
* @param {Object} kwargs Additional request parameters
|
|
243
291
|
* @returns {JSON} Data extracted from the document
|
|
244
292
|
*/
|
|
245
|
-
Client.prototype.
|
|
293
|
+
Client.prototype.process_document_base64string = async function (
|
|
246
294
|
base64_encoded_string,
|
|
247
295
|
file_name,
|
|
248
296
|
categories = null,
|
|
@@ -487,6 +535,34 @@ Client.prototype.delete_tags = async function (document_id) {
|
|
|
487
535
|
return this._request("DELETE", endpoint_name, request_arguments);
|
|
488
536
|
}
|
|
489
537
|
|
|
538
|
+
/**
|
|
539
|
+
* Replace multiple tags on an existing document
|
|
540
|
+
*
|
|
541
|
+
* @param {number} document_id ID of the document you'd like to add a Tag
|
|
542
|
+
* @param {string[]} tags array of tags to be added
|
|
543
|
+
* @return {JSON} response about tag added.
|
|
544
|
+
*/
|
|
545
|
+
Client.prototype.replace_tags = async function (document_id, tags) {
|
|
546
|
+
let endpoint_name = `/documents/${document_id}/`;
|
|
547
|
+
let request_arguments = {"tags": tags};
|
|
548
|
+
let response = await this._request("PUT", endpoint_name, request_arguments);
|
|
549
|
+
return response['data'];
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Add multiple tags on an existing document
|
|
554
|
+
*
|
|
555
|
+
* @param {number} document_id ID of the document you'd like to add a Tag
|
|
556
|
+
* @param {string[]} tags array of tags to be added
|
|
557
|
+
* @return {JSON} response about tag added.
|
|
558
|
+
*/
|
|
559
|
+
Client.prototype.add_tags = async function (document_id, tags) {
|
|
560
|
+
let endpoint_name = `/documents/${document_id}/tags/`;
|
|
561
|
+
let request_arguments = {"tags": tags};
|
|
562
|
+
let response = await this._request("POST", endpoint_name, request_arguments);
|
|
563
|
+
return response['data'];
|
|
564
|
+
}
|
|
565
|
+
|
|
490
566
|
|
|
491
567
|
// Exports
|
|
492
568
|
|
package/lib/types/main.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Additional request parameters type
|
|
3
5
|
*/
|
|
@@ -44,6 +46,7 @@ export declare type VeryfiDocument = {
|
|
|
44
46
|
payment_display_name?: null | string;
|
|
45
47
|
payment_terms?: null | string;
|
|
46
48
|
payment_type?: null | string;
|
|
49
|
+
pdf_url?: null | string;
|
|
47
50
|
phone_number?: null | string;
|
|
48
51
|
purchase_order_number?: null | string;
|
|
49
52
|
rounding?: null | number;
|
|
@@ -186,6 +189,56 @@ export declare class Client {
|
|
|
186
189
|
*/
|
|
187
190
|
public get_document(document_id: string): Promise<VeryfiDocument>;
|
|
188
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Process a document and extract all the fields from it
|
|
194
|
+
* @example
|
|
195
|
+
* veryfi_client.process_document_buffer('buffer',
|
|
196
|
+
* 'receipt.png',
|
|
197
|
+
* ['Entertainment','Food'],
|
|
198
|
+
* true,
|
|
199
|
+
* {'extra': 'parameters'})
|
|
200
|
+
*
|
|
201
|
+
* @memberof Client
|
|
202
|
+
* @param {ReadStream} file ReadStream of a file to submit for data extraction
|
|
203
|
+
* @param {String} file_name The file name including the extension
|
|
204
|
+
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
205
|
+
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
206
|
+
* @param {Object} kwargs Additional request parameters
|
|
207
|
+
* @returns {JSON} Data extracted from the document
|
|
208
|
+
*/
|
|
209
|
+
public process_document_stream(
|
|
210
|
+
file: fs.ReadStream,
|
|
211
|
+
file_name: string,
|
|
212
|
+
categories?: string[],
|
|
213
|
+
delete_after_processing?: boolean,
|
|
214
|
+
{ ...kwargs }?: VeryfiExtraArgs
|
|
215
|
+
): Promise<VeryfiDocument>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Process a document and extract all the fields from it
|
|
219
|
+
* @example
|
|
220
|
+
* veryfi_client.process_document_buffer_string('base64_encoded_string',
|
|
221
|
+
* 'receipt.png',
|
|
222
|
+
* ['Entertainment','Food'],
|
|
223
|
+
* true,
|
|
224
|
+
* {'extra': 'parameters'})
|
|
225
|
+
*
|
|
226
|
+
* @memberof Client
|
|
227
|
+
* @param {String} base64_encoded_string Buffer string of a file to submit for data extraction
|
|
228
|
+
* @param {String} file_name The file name including the extension
|
|
229
|
+
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
230
|
+
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
231
|
+
* @param {Object} kwargs Additional request parameters
|
|
232
|
+
* @returns {JSON} Data extracted from the document
|
|
233
|
+
*/
|
|
234
|
+
public process_document_base64string(
|
|
235
|
+
base64_encoded_string: string,
|
|
236
|
+
file_name: string,
|
|
237
|
+
categories?: string[],
|
|
238
|
+
delete_after_processing?: boolean,
|
|
239
|
+
{ ...kwargs }?: VeryfiExtraArgs
|
|
240
|
+
): Promise<VeryfiDocument>;
|
|
241
|
+
|
|
189
242
|
/**
|
|
190
243
|
* Process a document and extract all the fields from it
|
|
191
244
|
* @example
|
|
@@ -275,6 +328,24 @@ export declare class Client {
|
|
|
275
328
|
*/
|
|
276
329
|
public delete_tags(document_id: string): Promise<any>;
|
|
277
330
|
|
|
331
|
+
/**
|
|
332
|
+
* Add multiple tags on an existing document
|
|
333
|
+
*
|
|
334
|
+
* @param {number} document_id ID of the document you'd like to add a Tag
|
|
335
|
+
* @param {string[]} tags name to add
|
|
336
|
+
* @return {Promise<Tag>} response about tags added.
|
|
337
|
+
*/
|
|
338
|
+
public add_tags(document_id: string, tags: string[]): Promise<Tag>;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Replace multiple tags on an existing document
|
|
342
|
+
*
|
|
343
|
+
* @param {number} document_id ID of the document you'd like to add a Tag
|
|
344
|
+
* @param {string[]} tags names to be added
|
|
345
|
+
* @return {Promise<Tag>} response about tags added.
|
|
346
|
+
*/
|
|
347
|
+
public replace_tags(document_id: string, tags: string[]): Promise<Tag>;
|
|
348
|
+
|
|
278
349
|
}
|
|
279
350
|
|
|
280
351
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veryfi/veryfi-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "Node.js module for communicating with the Veryfi OCR API",
|
|
5
5
|
"main": "lib/main.js",
|
|
6
6
|
"typings": "lib/types/main.d.ts",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"@jest/globals": "^29.0.3",
|
|
36
36
|
"axios": "^0.21.1",
|
|
37
37
|
"docdash": "^1.2.0",
|
|
38
|
+
"form-data": "^4.0.0",
|
|
38
39
|
"jest-config": "^29.0.3"
|
|
39
40
|
},
|
|
40
41
|
"repository": {
|