SmartyStreets JavaScript SDK Utilities

1.2.9 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `smartystreets-javascript-sdk-utils` with the main SmartyStreets SDK to perform address validation and then analyze the response using the utility functions like `isValid`, `isInvalid`, `isAmbiguous`, and `isMissingSecondary`. It covers authentication setup, creating a lookup, sending it via the client, and then interpreting the results programmatically using the utility library.

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);
}

view raw JSON →