-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathus_autocomplete_pro.js
65 lines (49 loc) · 2.19 KB
/
us_autocomplete_pro.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
65
const SmartySDK = require("smartystreets-javascript-sdk");
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usAutocompletePro.Lookup;
// 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.StaticCredentials(authId, authToken);
// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// 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).withLicenses(["us-autocomplete-pro-cloud"]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let client = clientBuilder.buildUsAutocompleteProClient();
// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/us-autocomplete-api#pro-http-request-input-fields
// *** Simple Lookup ***
let lookup = new Lookup("4770 Lincoln");
// uncomment the following line to add a custom parameter
// lookup.addCustomParameter("max_results", 3);
await handleRequest(lookup, "Simple Lookup");
// *** Using Filter and Prefer ***
lookup = new Lookup("4770 Lincoln");
lookup.maxResults = 10;
lookup.includeOnlyCities = ["Chicago,La Grange,IL", "Blaine,WA"];
lookup.preferStates = ["IL"];
lookup.preferRatio = 33;
lookup.source = "all";
await handleRequest(lookup, "Using Filter and Prefer");
// *** Using 'selected' to Expand Secondaries ***
lookup = new Lookup("4770 Lincoln");
lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625";
await handleRequest(lookup, "Using 'selected' to Expand Secondaries")
// ************************************************
function logSuggestions(response, message) {
console.log(message);
console.log(response.result);
console.log("*********************");
}
async function handleRequest(lookup, lookupType) {
try {
const results = await client.send(lookup);
logSuggestions(results, lookupType);
} catch(err) {
console.log(err)
}
}