SmartyStreets JavaScript SDK Utilities
This library provides utility functions to perform additional analysis on responses from the SmartyStreets US Street API, specifically for lookups validated through the `smartystreets-javascript-sdk`. It offers functions like `isValid()`, `isInvalid()`, `isAmbiguous()`, and `isMissingSecondary()` to interpret the deliverability and clarity of address lookups based on USPS standards. The current stable version is 1.2.9. As per Smarty's disclaimer, this software is provided 'as is' and 'as a gift,' implying an infrequent or demand-driven release cadence and limited official support, differentiating it from commercially maintained libraries. It serves as an auxiliary package to enhance the core SmartyStreets JavaScript SDK functionality rather than a standalone solution.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'result')
cause The utility functions (e.g., `isValid`, `isAmbiguous`) expect a `lookup` object that has successfully been processed by the SmartyStreets SDK and contains a `result` property, which might be `undefined` if the API call failed or the lookup itself was malformed.fixEnsure that the `lookup` object passed to the utility functions is valid and populated from a successful API response. Add error handling for the `client.send()` promise to catch failures before attempting to use the utilities. -
ReferenceError: require is not defined
cause This error occurs when attempting to use CommonJS `require()` syntax in an environment configured for ES Modules (ESM), such as a modern Node.js project with `"type": "module"` in `package.json` or in a browser context without a transpiler.fixSwitch to ES Module `import` syntax. For example, change `const utils = require("smartystreets-javascript-sdk-utils");` to `import * as utils from "smartystreets-javascript-sdk-utils";` or `import { isValid } from "smartystreets-javascript-sdk-utils";`.
Warnings
- gotcha The SmartyStreets JavaScript SDK Utils package is provided 'AS IS' and 'as a gift.' This means that while enhancement requests may be considered, there is no guarantee of official support, bug fixes, or new features. Users should be aware of this limited maintenance model.
- gotcha This utility library is specifically designed to work with lookup objects returned by the `smartystreets-javascript-sdk`. Passing objects from other sources or incorrectly structured lookup objects will lead to undefined behavior or incorrect results.
Install
-
npm install smartystreets-javascript-sdk-utils -
yarn add smartystreets-javascript-sdk-utils -
pnpm add smartystreets-javascript-sdk-utils
Imports
- utils
const utils = require('smartystreets-javascript-sdk-utils');import * as utils from 'smartystreets-javascript-sdk-utils';
- isValid
import isValid from 'smartystreets-javascript-sdk-utils';
import { isValid } from 'smartystreets-javascript-sdk-utils'; - isAmbiguous
const { isAmbiguous } = require('smartystreets-javascript-sdk-utils');import { isAmbiguous } from 'smartystreets-javascript-sdk-utils';
Quickstart
import * as SmartySDK from 'smartystreets-javascript-sdk';
import * as SmartyUtils from 'smartystreets-javascript-sdk-utils';
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usStreet.Lookup;
// Use environment variables for credentials
const authId = process.env.SMARTY_AUTH_ID ?? 'YOUR_SMARTY_AUTH_ID';
const authToken = process.env.SMARTY_AUTH_TOKEN ?? 'YOUR_SMARTY_AUTH_TOKEN';
if (authId === 'YOUR_SMARTY_AUTH_ID' || authToken === 'YOUR_SMARTY_AUTH_TOKEN') {
console.warn('Please set SMARTY_AUTH_ID and SMARTY_AUTH_TOKEN environment variables or replace placeholders.');
}
let clientBuilder = new SmartyCore.ClientBuilder(new SmartyCore.StaticCredentials(authId, authToken));
let client = clientBuilder.buildUsStreetApiClient();
let lookup1 = new Lookup();
lookup1.street = "1600 Pennsylvania Ave NW";
lookup1.city = "Washington";
lookup1.state = "DC";
client.send(lookup1)
.then(handleSuccess)
.catch(handleError);
function handleSuccess(response) {
response.lookups.map(lookup => console.log('Result for ' + lookup.inputString + ':', lookup.result));
// Demonstrate utility functions for the first lookup in the response
if (response.lookups.length > 0) {
const firstLookupResult = response.lookups[0];
console.log(`Is '${firstLookupResult.inputString}' valid (mail deliverable)?`, SmartyUtils.isValid(firstLookupResult));
console.log(`Is '${firstLookupResult.inputString}' invalid (not mail deliverable)?`, SmartyUtils.isInvalid(firstLookupResult));
console.log(`Is '${firstLookupResult.inputString}' ambiguous (multiple candidates)?`, SmartyUtils.isAmbiguous(firstLookupResult));
console.log(`Is '${firstLookupResult.inputString}' missing a secondary address?`, SmartyUtils.isMissingSecondary(firstLookupResult));
}
}
function handleError(error) {
console.error("Error sending lookup:", error);
}