-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract-doc-async.js
64 lines (53 loc) · 1.77 KB
/
extract-doc-async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
// This script asynchronously extracts structured data from the specified PDF.
// For more information, see https://docs.sensible.so/docs/api-tutorial-async-1.
import fetch from "node-fetch";
import { Headers } from "node-fetch";
// The name of a document type in Sensible, e.g., auto_insurance_quote
const DOCUMENT_TYPE = "YOUR_DOCUMENT_TYPE";
// The URL of the PDF you'd like to extract from
const DOCUMENT_URL = "YOUR_DOCUMENT_URL";
// Your Sensible API key
const API_KEY = "YOUR_API_KEY";
async function main() {
const headers = new Headers();
headers.append("Authorization", `Bearer ${API_KEY}`);
headers.append("Content-Type", "application/json");
const body = JSON.stringify({
document_url: DOCUMENT_URL,
});
const response = await fetch(
`https://api.sensible.so/v0/extract_from_url/${DOCUMENT_TYPE}`,
{
method: "POST",
headers,
body,
}
);
if (!response.ok) {
console.log(await response.text());
} else {
let documentExtraction = await response.json();
let pollCount = 0;
// In production you'd use a webhook to avoid polling
while (documentExtraction.status == "WAITING") {
// Wait a few seconds for the extraction to complete on each iteration
await new Promise((r) => setTimeout(r, 3000));
const response = await fetch(
`https://api.sensible.so/v0/documents/${documentExtraction.id}`,
{ headers }
);
if (!response.ok) {
console.log(await response.text());
break;
} else {
documentExtraction = await response.json();
console.log(
`Poll attempt: ${++pollCount}, status: ${documentExtraction.status}`
);
}
console.log(JSON.stringify(documentExtraction, null, 2));
}
}
}
main();