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