Giter Site home page Giter Site logo

getting-started-smartystreets's Introduction

Using SmartyStreets API

git clone https://github.com/smarty/smartystreets-javascript-sdk.git

Obtaining an API Key

Recreating the example

  • Create a folder and open a terminal in that folder
  • Create package.json
npm init -y
  • Install libraries

npm i smartystreets-javascript-sdk dotenv

  • Store the API key and Auth token in a .env file

SMARTY_AUTH_ID=<your_auth_id>
SMARTY_AUTH_TOKEN=<your_auth_token>

const SmartySDK = require("smartystreets-javascript-sdk");
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usAutocompletePro.Lookup;

// 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.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// 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).withLicenses([
    "us-autocomplete-pro-cloud",
]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API

let client = clientBuilder.buildUsAutocompleteProClient();

// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/us-autocomplete-api#pro-http-request-input-fields

// *** Simple Lookup ***
let lookup = new Lookup("4770 Lincoln");

await handleRequest(lookup, "Simple Lookup");

// *** Using Filter and Prefer ***
lookup = new Lookup("4770 Lincoln");

lookup.maxResults = 10;
lookup.includeOnlyCities = ["Chicago,La Grange,IL", "Blaine,WA"];
lookup.preferStates = ["IL"];
lookup.preferRatio = 33;
lookup.source = "all";

await handleRequest(lookup, "Using Filter and Prefer");

// *** Using 'selected' to Expand Secondaries ***
lookup = new Lookup("4770 Lincoln");

lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625";

await handleRequest(lookup, "Using 'selected' to Expand Secondaries");

// ************************************************

function logSuggestions(response, message) {
    console.log(message);
    console.log(response.result);
    console.log("*********************");
}

async function handleRequest(lookup, lookupType) {
    try {
        const results = await client.send(lookup);
        logSuggestions(results, lookupType);
    } catch (err) {
        console.log(err);
    }
}
npm i smartystreets-javascript-sdk dotenv
  • Add a start script in the "scripts" section in package.json
"start": "node us-autocomplete-pro.js",
  • Run it from the terminal
npm start

getting-started-smartystreets's People

Contributors

puranik3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.