|
| 1 | +import SmartySDK from "smartystreets-javascript-sdk"; |
| 2 | + |
| 3 | +const SmartyCore = SmartySDK.core; |
| 4 | +const Lookup = SmartySDK.internationalPostalCode.Lookup; |
| 5 | + |
| 6 | +// for Server-to-server requests, use this code: |
| 7 | +// let authId = process.env.SMARTY_AUTH_ID; |
| 8 | +// let authToken = process.env.SMARTY_AUTH_TOKEN; |
| 9 | +// const credentials = new SmartyCore.StaticCredentials(authId, authToken); |
| 10 | + |
| 11 | +// for client-side requests (browser/mobile), use this code: |
| 12 | +let key = process.env.SMARTY_EMBEDDED_KEY; |
| 13 | +const credentials = new SmartyCore.SharedCredentials(key); |
| 14 | + |
| 15 | +// The appropriate license values to be used for your subscriptions |
| 16 | +// can be found on the Subscription page of the account dashboard. |
| 17 | +// https://www.smarty.com/docs/cloud/licensing |
| 18 | +let clientBuilder = new SmartyCore.ClientBuilder(credentials); |
| 19 | +// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API |
| 20 | +let client = clientBuilder.buildInternationalPostalCodeClient(); |
| 21 | + |
| 22 | +// Documentation for input fields can be found at: |
| 23 | +// https://www.smarty.com/docs/cloud/international-postal-code-api#input-fields |
| 24 | + |
| 25 | +// Lookup by postal code and country |
| 26 | +let lookup1 = new Lookup("Australia", "2776"); |
| 27 | +// uncomment the following line to add a custom parameter |
| 28 | +// lookup1.addCustomParameter("input_id", 1234); |
| 29 | + |
| 30 | +// Lookup by locality, administrative area, and country |
| 31 | +let lookup2 = new Lookup("Brazil", null, "SP", "Sao Paulo", "ID-8675309"); |
| 32 | + |
| 33 | +await handleResponse(lookup1, "Postal code lookup"); |
| 34 | +await handleResponse(lookup2, "Locality and administrative area lookup"); |
| 35 | + |
| 36 | +function displayResult(lookup, message) { |
| 37 | + console.log("*** " + message + " ***"); |
| 38 | + if (lookup.result && lookup.result.length > 0) { |
| 39 | + lookup.result.forEach((result) => { |
| 40 | + console.log("Input ID:", result.inputId); |
| 41 | + console.log("Administrative Area:", result.administrativeArea); |
| 42 | + console.log("Super Administrative Area:", result.superAdministrativeArea); |
| 43 | + console.log("Sub Administrative Area:", result.subAdministrativeArea); |
| 44 | + console.log("Locality:", result.locality); |
| 45 | + console.log("Dependent Locality:", result.dependentLocality); |
| 46 | + console.log("Dependent Locality Name:", result.dependentLocalityName); |
| 47 | + console.log("Double Dependent Locality:", result.doubleDependentLocality); |
| 48 | + console.log("Postal Code:", result.postalCode); |
| 49 | + console.log("Postal Code Extra:", result.postalCodeExtra); |
| 50 | + console.log("Country ISO3:", result.countryIso3); |
| 51 | + console.log("---"); |
| 52 | + }); |
| 53 | + } else { |
| 54 | + console.log("No results found"); |
| 55 | + } |
| 56 | + console.log("\n"); |
| 57 | +} |
| 58 | + |
| 59 | +function handleError(error) { |
| 60 | + console.log("ERROR:", error); |
| 61 | +} |
| 62 | + |
| 63 | +async function handleResponse(lookup, lookupType) { |
| 64 | + try { |
| 65 | + const result = await client.send(lookup); |
| 66 | + displayResult(result, lookupType); |
| 67 | + } catch (err) { |
| 68 | + handleError(err); |
| 69 | + } |
| 70 | +} |
0 commit comments