-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathus_street.mjs
More file actions
75 lines (60 loc) · 2.58 KB
/
us_street.mjs
File metadata and controls
75 lines (60 loc) · 2.58 KB
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
65
66
67
68
69
70
71
72
73
74
75
import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usStreet.Lookup;
// for client-side requests (browser/mobile), use this code:
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);
// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);
// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let client = clientBuilder.buildUsStreetApiClient();
// Documentation for input fields can be found at:
// https://www.smarty.com/docs/us-street-api#input-fields
let lookup1 = new Lookup();
lookup1.inputId = "24601"; // Optional ID from your system
lookup1.addressee = "John Doe";
lookup1.street = "330 N 100 W";
lookup1.street2 = "closet under the stairs";
lookup1.secondary = "APT 2";
lookup1.urbanization = ""; // Only applies to Puerto Rico addresses
lookup1.city = "Provo";
lookup1.state = "Utah";
lookup1.zipCode = "84601";
lookup1.maxCandidates = 3;
lookup1.match = "enhanced"; // The API will return detailed output based on a more aggressive matching mechanism. It also includes a more comprehensive address dataset beyond just the postal address data. Requires a US Core license or a US Rooftop Geocoding license.
// Refer to the documentation for additional MatchStrategy options.
let lookup2 = new Lookup();
lookup2.street = "1600 Amphitheater Pkwy";
lookup2.lastLine = "Mountainview, CA";
lookup2.maxCandidates = 5;
let lookup3 = new Lookup();
lookup3.inputId = "8675309";
lookup3.street = "1600 Amphitheatre Parkway Mountain View, CA 94043";
// uncomment the following line to add a custom parameter
// lookup3.addCustomParameter("max_candidates", 1);
// NOTE: batches are not supported when using SharedCredentials.
let batch = new SmartyCore.Batch();
batch.add(lookup1);
batch.add(lookup2);
batch.add(lookup3);
await handleResponse(batch);
function handleSuccess(response) {
response.lookups.map((lookup) => console.log(lookup.result));
}
function handleError(response) {
console.log(response);
}
async function handleResponse(lookup) {
try {
const result = await client.send(lookup);
handleSuccess(result);
} catch (err) {
handleError(err);
}
}