-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathinternational_postal_code.mjs
More file actions
70 lines (59 loc) · 2.67 KB
/
international_postal_code.mjs
File metadata and controls
70 lines (59 loc) · 2.67 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
import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.internationalPostalCode.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.buildInternationalPostalCodeClient();
// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/international-postal-code-api#input-fields
// Lookup by postal code and country
let lookup1 = new Lookup("Australia", "2776");
// uncomment the following line to add a custom parameter
// lookup1.addCustomParameter("input_id", 1234);
// Lookup by locality, administrative area, and country
let lookup2 = new Lookup("Brazil", null, "SP", "Sao Paulo", "ID-8675309");
await handleResponse(lookup1, "Postal code lookup");
await handleResponse(lookup2, "Locality and administrative area lookup");
function displayResult(lookup, message) {
console.log("*** " + message + " ***");
if (lookup.result && lookup.result.length > 0) {
lookup.result.forEach((result) => {
console.log("Input ID:", result.inputId);
console.log("Administrative Area:", result.administrativeArea);
console.log("Super Administrative Area:", result.superAdministrativeArea);
console.log("Sub Administrative Area:", result.subAdministrativeArea);
console.log("Locality:", result.locality);
console.log("Dependent Locality:", result.dependentLocality);
console.log("Dependent Locality Name:", result.dependentLocalityName);
console.log("Double Dependent Locality:", result.doubleDependentLocality);
console.log("Postal Code:", result.postalCode);
console.log("Postal Code Extra:", result.postalCodeExtra);
console.log("Country ISO3:", result.countryIso3);
console.log("---");
});
} else {
console.log("No results found");
}
console.log("\n");
}
function handleError(error) {
console.log("ERROR:", error);
}
async function handleResponse(lookup, lookupType) {
try {
const result = await client.send(lookup);
displayResult(result, lookupType);
} catch (err) {
handleError(err);
}
}