@veryfi/veryfi-sdk 1.1.1 → 1.1.4

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.
@@ -0,0 +1,31 @@
1
+ # This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ main ]
9
+ pull_request:
10
+ branches: [ main ]
11
+
12
+ jobs:
13
+ build:
14
+
15
+ runs-on: ubuntu-latest
16
+
17
+ strategy:
18
+ matrix:
19
+ node-version: [12.x, 14.x, 16.x]
20
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
+
22
+ steps:
23
+ - uses: actions/checkout@v2
24
+ - name: Use Node.js ${{ matrix.node-version }}
25
+ uses: actions/setup-node@v2
26
+ with:
27
+ node-version: ${{ matrix.node-version }}
28
+ cache: 'npm'
29
+ - run: npm ci
30
+ - run: npm run build --if-present
31
+ - run: npm test
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Veryfi SDK for Node.js
2
2
 
3
- [![Node.js - version](https://img.shields.io/badge/node-%3E%3D%206.0.0-brightgreen)](https://pypi.python.org/pypi/veryfi/)
4
- [![npm](https://img.shields.io/badge/npm-v7.0.0-blue)](https://pypi.python.org/pypi/veryfi)
3
+ [![Node.js - version](https://img.shields.io/badge/node-%3E%3D%206.0.0-brightgreen)](https://www.npmjs.com/package/@veryfi/veryfi-sdk)
4
+ [![npm](https://img.shields.io/badge/npm-v7.0.0-blue)](https://www.npmjs.com/package/@veryfi/veryfi-sdk)
5
5
  [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
6
6
 
7
7
  ![Veryfi Logo](https://cdn.veryfi.com/logos/veryfi-logo-wide-github.png)
@@ -18,21 +18,20 @@
18
18
  - [Response](#response)
19
19
  - [Updating a document](#updating-a-document)
20
20
  - [Need help?](#need-help)
21
+ - [Learn more at our blog](#learn-more-at-our-blog)
22
+ - [Tutorial Video](#tutorial-video)
21
23
 
22
24
  **veryfi-nodejs** is a Node.js module for communicating with the [Veryfi OCR API](https://veryfi.com/api/)
23
25
 
24
26
  ## Installation
25
27
 
26
- Install from [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), a
27
- package manager for Javascript.
28
-
29
- Install the package from npm:
28
+ Install from [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), a package manager for Javascript.
30
29
 
31
30
  ```bash
32
31
  npm i @veryfi/veryfi-sdk
33
32
  ```
34
33
 
35
- You may need to run the above commands with `sudo`.
34
+ You may need to run the above command with `sudo`.
36
35
 
37
36
  ## Getting Started
38
37
 
@@ -42,21 +41,21 @@ If you don't have an account with Veryfi, please go ahead and register here: [ht
42
41
 
43
42
  ### Node.js API Client Library
44
43
 
45
- The **veryfi** library can be used to communicate with Veryfi API. All available functionality is described here <https://veryfi.github.io/veryfi-nodejs/Client.html>
44
+ The **veryfi** library can be used to communicate with Veryfi API. All available functionality is described here: <https://veryfi.github.io/veryfi-nodejs/Client.html>
46
45
 
47
- Below is the sample script using **veryfi** to OCR and extract data from a document:
46
+ Below is a sample script using **Veryfi** for OCR and extracting data from a document:
48
47
 
49
48
  ### Extracting Data
50
49
 
51
50
  ```js
52
- const Client = require('veryfi-sdk');
51
+ const Client = require('@veryfi/veryfi-sdk');
53
52
  const client_id = 'your_client_id';
54
53
  const client_secret = 'your_client_secret';
55
54
  const username = 'your_username';
56
55
  const api_key = 'your_password';
57
56
 
58
- const categories = ['Grocery', 'Utilities', 'Travel']
59
- const file_path = './test/receipt.png'
57
+ const categories = ['Grocery', 'Utilities', 'Travel'];
58
+ const file_path = './test/receipt.png';
60
59
  ```
61
60
 
62
61
  This submits a document for processing (3-5 seconds for a response)
@@ -231,9 +230,9 @@ console.log(response);
231
230
  ### Updating a document
232
231
 
233
232
  ```js
234
- let = new_vendor = {"name": "Starbucks", "address": "123 Easy Str, San Francisco, CA 94158"}
235
- let category = "Meals & Entertainment"
236
- let new_total = 11.23
233
+ let new_vendor = {"name": "Starbucks", "address": "123 Easy Str, San Francisco, CA 94158"};
234
+ let category = "Meals & Entertainment";
235
+ let new_total = 11.23;
237
236
  veryfi_client.update_document(id=12345,
238
237
  {
239
238
  'vendor': new_vendor,
@@ -248,4 +247,8 @@ If you run into any issue or need help installing or using the library, please c
248
247
 
249
248
  If you found a bug in this library or would like new features added, then open an issue or pull requests against this repo!
250
249
 
251
- To learn more about Veryfi visit <https://www.veryfi.com/>
250
+ ### [Learn more at our blog](https://www.veryfi.com/nodejs/)
251
+
252
+ ## Tutorial Video
253
+
254
+ [![Veryfi Tutorial](https://img.youtube.com/vi/PcJdgnvyfBc/maxresdefault.jpg)](https://www.youtube.com/watch?v=PcJdgnvyfBc)
package/lib/main.js CHANGED
@@ -28,10 +28,8 @@ function Client(
28
28
  this.username = username;
29
29
  this.api_key = api_key;
30
30
  this.base_url = base_url;
31
- this.timeout = timeout;
32
31
  this.api_version = api_version;
33
- this.headers = {};
34
- CATEGORIES = [
32
+ this.CATEGORIES = [
35
33
  "Advertising & Marketing",
36
34
  "Automotive",
37
35
  "Bank Charges & Fees",
@@ -47,7 +45,7 @@ function Client(
47
45
  "Utilities",
48
46
  "Job Supplies",
49
47
  "Grocery",
50
- ];
48
+ ];
51
49
  }
52
50
 
53
51
 
@@ -59,7 +57,7 @@ function Client(
59
57
  */
60
58
  Client.prototype._get_headers = function (has_files=false){
61
59
  let final_headers = {
62
- "User-Agent": "Nodejs Veryfi-Nodejs/1.0.0",
60
+ "User-Agent": "Node.js Veryfi-Nodejs/1.1.4",
63
61
  "Accept": "application/json",
64
62
  "Content-Type": "application/json",
65
63
  "Client-Id": this.client_id,
@@ -83,12 +81,12 @@ Client.prototype._get_url = function (){
83
81
  /**
84
82
  * Generate unique signature for payload params.
85
83
  * @private
86
- * @param {JSON} payload_params JSON params to be sent to API request
84
+ * @param {{}} payload_params JSON params to be sent to API request
87
85
  * @param {Number} timestamp Unix Long timestamp
88
86
  * @returns {string} Unique signature generated using the client_secret and the payload
89
87
  */
90
88
  Client.prototype._generate_signature = function (payload_params, timestamp){
91
- var payload = `timestamp:${timestamp}`;
89
+ let payload = `timestamp:${timestamp}`;
92
90
  for (let i=0; i<Object.keys(payload_params).length; i++){
93
91
  let key = Object.keys(payload_params)[i];
94
92
  let value = payload_params[key];
@@ -98,8 +96,8 @@ Client.prototype._generate_signature = function (payload_params, timestamp){
98
96
  const payload_bytes = encodeURI(payload);
99
97
  let hmac = createHmac('sha256', secret_bytes);
100
98
  hmac.update(payload_bytes);
101
- let tmp_signature = hmac.digest('sha256');
102
- var base64_signature = Buffer.from(tmp_signature).toString('base64');
99
+ let tmp_signature = hmac.digest();
100
+ let base64_signature = Buffer.from(tmp_signature).toString('base64');
103
101
  base64_signature = Buffer.from(base64_signature).toString('utf-8').trim();
104
102
 
105
103
  return base64_signature;
@@ -110,41 +108,40 @@ Client.prototype._generate_signature = function (payload_params, timestamp){
110
108
  * @private
111
109
  * @param {string} http_verb HTTP Method
112
110
  * @param {string} endpoint_name Endpoint name such as 'documents', 'users', etc.
113
- * @param {JSON} request_arguments JSON payload to send to Veryfi
111
+ * @param {{}} request_arguments JSON payload to send to Veryfi
112
+ * @param file_stream file stream of the document
114
113
  * @returns {JSON} A JSON of the response data.
115
114
  */
116
115
  Client.prototype._request = async function (http_verb, endpoint_name, request_arguments, file_stream=null){
117
116
  let has_files = (Boolean(file_stream));
118
- let headers = this._get_headers(has_files=has_files);
117
+ let headers = this._get_headers(has_files);
119
118
  let api_url = `${this._get_url()}/partner${endpoint_name}`;
120
119
 
121
120
  if (this.client_secret){
122
121
  let timestamp = Date.now();
123
- let signature = this._generate_signature(request_arguments, timestamp=timestamp);
122
+ let signature = this._generate_signature(request_arguments, timestamp);
124
123
  headers = Object.assign(headers,{"X-Veryfi-Request-Timestamp": timestamp,
125
124
  "X-Veryfi-Request-Signature": signature});
126
125
  }
127
126
 
128
- if (has_files==false) {
127
+ if (has_files === false) {
129
128
  request_arguments = JSON.stringify(request_arguments);
130
129
  } else {
131
- Object.assign(headers,{...request_arguments.getHeaders()});
130
+ Object.assign(headers,{...request_arguments});
132
131
  }
133
132
 
134
133
  try {
135
- const response = await axios.request({
136
- method : http_verb,
137
- url : api_url,
138
- headers : headers,
139
- data : request_arguments,
140
- timeout : this.timeout * 1000
134
+ return await axios.request({
135
+ method: http_verb,
136
+ url: api_url,
137
+ headers: headers,
138
+ data: request_arguments,
139
+ timeout: this.timeout * 1000
141
140
  });
142
- return response;
143
141
  } catch (response) {
144
- console.log(response);
145
142
  let Err = new VeryfiClientError(response,response['response']['data']);
146
143
  let err_msg = Err.from_response(response['response']);
147
- throw new Error(err_msg['status'],err_msg['error']);
144
+ throw new Error(err_msg['status'] + err_msg['error']);
148
145
  }
149
146
  }
150
147
 
@@ -167,7 +164,7 @@ Client.prototype.get_documents = async function (){
167
164
  /**
168
165
  * Retrieve document by ID
169
166
  * @memberof Client
170
- * @param {Number} document_id ID of the document you'd like to retrieve
167
+ * @param {string} document_id ID of the document you'd like to retrieve
171
168
  * @returns {JSON} Data extracted from the Document
172
169
  */
173
170
  Client.prototype.get_document = async function (document_id) {
@@ -201,17 +198,57 @@ Client.prototype.process_document = async function (
201
198
 
202
199
  let endpoint_name = "/documents/";
203
200
  if (!categories){
204
- categories = CATEGORIES;
201
+ categories = this.CATEGORIES;
205
202
  }
206
203
  let file_name = path.basename(file_path)
207
204
  const image_file = fs.readFileSync(file_path, {encoding: 'base64'});
208
- var base64_encoded_string = Buffer.from(image_file).toString('utf-8');
209
- var request_arguments = {
205
+ const base64_encoded_string = Buffer.from(image_file).toString('utf-8');
206
+ let request_arguments = {
210
207
  "file_name": file_name,
211
208
  "file_data": base64_encoded_string,
212
209
  "categories": categories,
213
210
  "auto_delete": delete_after_processing,
211
+ };
212
+ request_arguments = Object.assign(request_arguments,kwargs)
213
+ let document = await this._request("POST", endpoint_name, request_arguments)
214
+ return document['data'];
215
+ }
216
+
217
+ /**
218
+ * Process a document and extract all the fields from it
219
+ * @example
220
+ * veryfi_client.process_document_buffer('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 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
+ Client.prototype.process_document_buffer = async function (
235
+ base64_encoded_string,
236
+ file_name,
237
+ categories = null,
238
+ delete_after_processing = false,
239
+ {...kwargs} = {}
240
+ ) {
241
+
242
+ let endpoint_name = "/documents/";
243
+ if (!categories){
244
+ categories = this.CATEGORIES;
214
245
  }
246
+ let request_arguments = {
247
+ "file_name": file_name,
248
+ "file_data": base64_encoded_string,
249
+ "categories": categories,
250
+ "auto_delete": delete_after_processing,
251
+ };
215
252
  request_arguments = Object.assign(request_arguments,kwargs)
216
253
  let document = await this._request("POST", endpoint_name, request_arguments)
217
254
  return document['data'];
@@ -219,6 +256,9 @@ Client.prototype.process_document = async function (
219
256
 
220
257
  /**
221
258
  * Process document from url and extract all the fields from it.
259
+ * @example
260
+ * veryfi_client.process_document_url('https://cdn.example.com/receipt.jpg')
261
+ *
222
262
  * @memberof Client
223
263
  * @param {string} file_url Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
224
264
  * @param {Array} file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"]
@@ -231,17 +271,20 @@ Client.prototype.process_document = async function (
231
271
  * @return {JSON} Data extracted from the document.
232
272
  */
233
273
  Client.prototype.process_document_url = async function (
234
- file_url = null,
274
+ file_url = "",
275
+ file_urls = null,
235
276
  categories = null,
236
277
  delete_after_processing = false,
237
278
  boost_mode = 0,
238
- external_id = null,
239
- max_pages_to_process = null,
240
- file_urls = null,
279
+ external_id = "",
280
+ max_pages_to_process = 1,
241
281
  {...kwargs} = {},
242
282
  ) {
243
283
  let endpoint_name = "/documents/";
244
- var request_arguments = {
284
+ if (!categories){
285
+ categories = this.CATEGORIES;
286
+ }
287
+ let request_arguments = {
245
288
  "auto_delete": delete_after_processing,
246
289
  "boost_mode": boost_mode,
247
290
  "categories": categories,
@@ -259,13 +302,13 @@ Client.prototype.process_document_url = async function (
259
302
  * Delete document from Veryfi
260
303
  *
261
304
  * @memberof Client
262
- * @param {Number} document_id ID of the document you'd like to delete
305
+ * @param {string} document_id ID of the document you'd like to delete
263
306
  */
264
307
  Client.prototype.delete_document = async function (document_id){
265
-
266
308
  let endpoint_name = `/documents/${document_id}/`;
267
309
  let request_arguments = {"id": document_id};
268
- await this._request("DELETE", endpoint_name, request_arguments);
310
+ let response = await this._request("DELETE", endpoint_name, request_arguments);
311
+ return response;
269
312
  }
270
313
 
271
314
  /**
@@ -274,7 +317,7 @@ Client.prototype.delete_document = async function (document_id){
274
317
  * veryfi_client.update_document(id, {date:"2021-01-01", notes:"look what I did"})
275
318
  *
276
319
  * @memberof Client
277
- * @param {Number} document_id ID of the document you'd like to update
320
+ * @param {string} document_id ID of the document you'd like to update
278
321
  * @param {Object} kwargs fields to update
279
322
  * @return {JSON} A document json with updated fields, if fields are writable. Otherwise a document with unchanged fields.
280
323
  */
@@ -286,4 +329,4 @@ Client.prototype.update_document = async function (document_id, {...kwargs} = {}
286
329
 
287
330
  // Exports
288
331
 
289
- module.exports = Client;
332
+ module.exports = Client;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veryfi/veryfi-sdk",
3
- "version": "1.1.1",
3
+ "version": "1.1.4",
4
4
  "description": "Node.js module for communicating with the Veryfi OCR API",
5
5
  "main": "lib/main.js",
6
6
  "scripts": {
@@ -0,0 +1,4 @@
1
+ {
2
+ "status":"ok",
3
+ "message":"Document has been deleted"
4
+ }
@@ -0,0 +1,111 @@
1
+ {
2
+ "abn_number":"",
3
+ "account_number":"",
4
+ "barcodes":[
5
+
6
+ ],
7
+ "bill_to_address":"",
8
+ "bill_to_name":"",
9
+ "card_number":"",
10
+ "category":"Meals",
11
+ "created":"2021-02-04 00:19:07",
12
+ "currency_code":"COP",
13
+ "date":"2021-01-24 19:40:00",
14
+ "delivery_date":"",
15
+ "discount":0.0,
16
+ "document_title":"",
17
+ "document_type":"receipt",
18
+ "due_date":"",
19
+ "external_id":"",
20
+ "id":31727276,
21
+ "img_file_name":"c55c3a43-06ca-4da7-b780-60768d7f6155.jpg",
22
+ "img_thumbnail_url":"https://scdn.veryfi.com/receipts/c55c3a43-06ca-4da7-b780-60768d7f6155_1_t.jpg?Expires=1626052133&Signature=bQfb8nYHxKeQzaibhQv6lznNTM980DXxicImi9PFbb-iP9yejag4mgzacCcaOIoXEQms-L-EzmpHzpI6Y6q8pzfy5vhS8x2KjqZ2N2iGRadolKTsHTfuQCZdC2tassH~0faTnJ9L2NDrPlU97mnc06Nqa6graAj1zJ7uKOITeF4ONhVWVrj1cRM~lOFqwq9Gk5Xf9-uRVQPan0d7gpAbdx1sZE25ImphkzQen5IaduEvd6k4vhlHeqeOfj911Z8ReAXz2MWEgPuRcgj7H9DwHATiQMR6M4ft40d7zBd2TjyKRuSgH~fDfq8CFLLAtxoo6mhaawi-HIZi~4kwFDRQKg__&Key-Pair-Id=APKAJCILBXEJFZF4DCHQ",
23
+ "img_url":"https://scdn.veryfi.com/receipts/c55c3a43-06ca-4da7-b780-60768d7f6155.jpg?Expires=1626052133&Signature=bi4gQP2IOjPy1IsFk4apCdBOQLOOwxWZIM3xPTs-Ojzz7gJEQ5Jt6oc5gcR898rA8FrNJ-1RjzzxMiW5p3lPNcnXyeiWj8IH9HG45cb0M0-TK~YUsFjFRVXpflSN9PB5Xb3R56Mq1HPN2xTCE~o2lhnJyQEK52u--n4nyX4dg7aMQc7DLBuidqFZoB4eNrvPZ7q1uDpT~hcrD5x4i26g7owfiGTKsh60OwpljIcQV61vNqJxcyyVD6Dlk9pl-5MJZGQxUKWeuwY76fg6zUtuUqgRL1he2DKjOSLwNGbLTmFGcNq6R0AvHs45gtyMNKn5SdttvWnQJagHZl4tG8DF7g__&Key-Pair-Id=APKAJCILBXEJFZF4DCHQ",
24
+ "incoterms":"",
25
+ "insurance":"",
26
+ "invoice_number":"1381884",
27
+ "is_document":1,
28
+ "is_money_in":0,
29
+ "line_items":[
30
+ {
31
+ "date":"",
32
+ "description":"MimVaiCucManiChoco",
33
+ "discount":0.0,
34
+ "id":32895461,
35
+ "order":0,
36
+ "price":0.0,
37
+ "quantity":10.0,
38
+ "reference":"",
39
+ "section":"",
40
+ "sku":"",
41
+ "tax":0.0,
42
+ "tax_rate":0.0,
43
+ "total":5900.0,
44
+ "type":"food",
45
+ "unit_of_measure":"",
46
+ "weight":""
47
+ },
48
+ {
49
+ "date":"",
50
+ "description":"",
51
+ "discount":0.0,
52
+ "id":32895462,
53
+ "order":1,
54
+ "price":0.0,
55
+ "quantity":10.0,
56
+ "reference":"",
57
+ "section":"",
58
+ "sku":"",
59
+ "tax":0.0,
60
+ "tax_rate":0.0,
61
+ "total":5900.0,
62
+ "type":"",
63
+ "unit_of_measure":"",
64
+ "weight":""
65
+ }
66
+ ],
67
+ "notes":"",
68
+ "ocr_text":"\nSomos Regimen Comun\nNo Somos Retenedores del IVA\nP.C.A PRODUCTORA Y COMERCIALIZADORA DE\nALIMENTOS S.A.S\nDG 51 35 12 CC PUERTA DEL NORTE\nNIT:890,912,221-4\nFactura de Venta Nro. 859 1381884\nNeg Pto Ter Fecha\tCajero\n001 859 001 24/01/2021 19:40 859\nDescripcion\tCant Valor\nMimVaiCucManiChoco 1.0 5,900.\n* Subtotal *\t\t5,900.\nCantidad Bolsas(*)\t\t0\nImpuesto Bolsas(*)\t\t0\n** T O T A L **\t5,900.\nEFECTIVO\t\t\t20,000.\nCAMBIO\t\t\t14,100.\n%\tBase Imp.\tTotal\n0\t5,900.\t0. 5,900.\nMIMO'S EXITO BELLO TEL 481 05 06\nResolucion Facturacion DIAN Nro\n18763003974691 Feb 4 2020\nAutorizacion Rango 859-1142814 al 859-138\n2856 Vigencia 12 Meses\n",
69
+ "order_date":"",
70
+ "payment_display_name":"Cash",
71
+ "payment_terms":"",
72
+ "payment_type":"cash",
73
+ "phone_number":"",
74
+ "purchase_order_number":"",
75
+ "reference_number":"VBDHC-7276",
76
+ "service_end_date":"",
77
+ "service_start_date":"",
78
+ "ship_date":"",
79
+ "shipping":0.0,
80
+ "subtotal":5900.0,
81
+ "tags":[
82
+
83
+ ],
84
+ "tax":0,
85
+ "tax_lines":[
86
+
87
+ ],
88
+ "tip":0.0,
89
+ "total":0.0,
90
+ "total_weight":"",
91
+ "tracking_number":"",
92
+ "updated":"2021-02-04 00:19:10",
93
+ "vat_number":"890,912,221-4",
94
+ "vendor":{
95
+ "address":"DG 51 35 12 CC PUERTA DEL NORTE",
96
+ "fax_number":"",
97
+ "name":"P.c.a Productora Y Comercializadora De",
98
+ "phone_number":"",
99
+ "raw_name":"P.C.A PRODUCTORA Y COMERCIALIZADORA DE",
100
+ "vendor_logo":"https://cdn.veryfi.com/logos/tmp/397947509.png",
101
+ "vendor_type":""
102
+ },
103
+ "vendor_account_number":"",
104
+ "vendor_bank_name":"",
105
+ "vendor_bank_number":"",
106
+ "vendor_bank_swift":"",
107
+ "vendor_iban":"",
108
+ "warnings":[
109
+
110
+ ]
111
+ }