-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathinternational_street.js
58 lines (47 loc) · 1.95 KB
/
international_street.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
const SmartySDK = require("smartystreets-javascript-sdk");
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.internationalStreet.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(["international-global-plus-cloud"]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let client = clientBuilder.buildInternationalStreetClient();
// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/international-street-api#http-input-fields
let lookup1 = new Lookup("CA", "262 Browndale Cr, Richmond Hill, ON");
// uncomment the following line to add a custom parameter
// lookup1.addCustomParameter("input_id", 1234);
let lookup2 = new Lookup();
lookup2.inputId = "ID-8675309";
lookup2.geocode = false;
lookup2.organization = "John Doe";
lookup2.address1 = "Rua Padre Antonio D'Angelo 121";
lookup2.address2 = "Casa Verde";
lookup2.locality = "Sao Paulo";
lookup2.administrativeArea = "SP";
lookup2.country = "Brazil";
lookup2.postalCode = "02516-050";
await handleRequest(lookup1)
await handleRequest(lookup2)
function displayResult(result) {
console.log(result.result[0].components);
}
function handleError(error) {
console.log("ERROR:", error);
}
async function handleRequest(lookup) {
try {
const result = await client.send(lookup);
displayResult(result);
} catch(err) {
handleError(err);
}
}