Bulk Address Verification With the Lob API
Tags: tutorial, node, postgres, beginners
Published: 2021-09-29 14:56:08

This article is a hands-on tutorial that will show you how to create an app using Lob’s Address Verification API to verify records from a database in bulk. This tutorial will show you the capabilities of Lob’s API to help make your direct mail process more efficient.

This tutorial assumes that you’re a Node.js developer working with an extensive pre-existing database of address records. The database may contain invalid records, so we use the Lob API to quickly verify the addresses in bulk and flag any bad records found in the database.

Before getting started, you need to ensure that you have [Node.js](https://nodejs.org/), npm, [PostgreSQL](https://www.postgresql.org/), and [psql](https://www.postgresql.org/docs/13/app-psql.html) installed on your development machine.

You can install Node.js and npm by following these steps:

* Download the correct installer binary for your system from the [official website](https://nodejs.org/).
* Install Node.js and npm using the package manager of your system.
* Use a Node.js version manager, such as [NVM](https://github.com/nvm-sh/nvm). This also enables you to easily manage multiple versions of Node.js on your development machine.

You can follow one of these tutorials for installing and setting up PostgreSQL.

[Linux](https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart) | [Mac](https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb) | [Windows](https://www.postgresqltutorial.com/install-postgresql/)

## Bulk Address Verification
In this section, we’ll review how to create a complete Node.js web app step by step using PostgreSQL and the Lob API to accomplish address verification in bulk. Let’s start by setting up a PostgreSQL database with fields for address information (name, street address, state, city, ZIP code, and so on). 

Open a new terminal and run the following command: 

{% gist https://gist.github.com/SidneyAllen/e18b8c2e7aa40f439637394197580945.js %}‍

Provide the password and press Enter by default there isn’t a password.

If you see postgres=# in your terminal, you have successfully logged in.

If psql can’t connect to server-  [verify the postgres database is running](https://stackoverflow.com/questions/31645550/postgresql-why-psql-cant-connect-to-server)

Next, create the addresses table. Use the serial type to create an [auto-incremented primary key](https://www.techiediaries.com/auto-increment-primary-key-postgresql/). The code is as follows:

{% gist https://gist.github.com/SidneyAllen/778e2a227cb0169a61226cecb1606d46.js %}‍

You can run the following command to see the created table:

{% gist https://gist.github.com/SidneyAllen/6b641a759b8a53992ce72817ed38d6c0.js %}‍

Next, let’s fill the table with some dummy records with both valid and invalid addresses. Use a [public location](https://github.com/EthanRBrown/rrad) for valid addresses without sensitive info. You can use the following SQL queries:

{% gist https://gist.github.com/SidneyAllen/11ca1f94d74400f6a4151f17fcdb3f50.js %}‍
‍
If you want to see the new rows inserted in your database table - [checkout this tutorial](https://www.postgresqltutorial.com/postgresql-show-tables/).

Now, let’s create a web application with Node.js. We use Express.js, a popular framework for powering web apps built on top of Node.js.

Head over to your terminal and run the following command:

{% gist https://gist.github.com/SidneyAllen/b37932390e6107ab14fce9351452c316.js %}‍‍

This creates a package.json file with the following contents:

{% gist https://gist.github.com/SidneyAllen/33166eeb55ab5d970d6de02383cc0500.js %}‍

Next, install the dependencies using the following command:

{% gist https://gist.github.com/SidneyAllen/67e028a178358b4bd8848fe25567f5a7.js %}

We installed [Express.js](https://expressjs.com/) for the server and [node-postgres (pg)](https://node-postgres.com/) to connect to PostgreSQL. We also [installed nodemon for automatically restarting](https://30daysofjavascript.com/nodemon/) the Node application when there are file changes in the directory.

Now, we must create an index.js file that we use as the main entry point for our app:

{% gist https://gist.github.com/SidneyAllen/75f9dfa6c1b416c6c15f78b4394c58fc.js %}‍

Open the package.json file and add the following script to start our app in watch mode using nodemon:

{% gist https://gist.github.com/SidneyAllen/fa058ffb2ea0cb21a768aa297957565f.js %}‍

In the index.js file, we must import the express and path modules and set our app and port variables as follows:

{% gist https://gist.github.com/SidneyAllen/755967f2f7be4643c51ea135e908874f.js %}‍

Next, set up the Express app as follows:

{% gist https://gist.github.com/SidneyAllen/ce2b93e62001975de81e23012af85910.js %}‍

Now, let’s add a route for the GET request to the root (/) URL, and return the HTML page:

{% gist  https://gist.github.com/SidneyAllen/7c0456e0191019be3e76c5de98d464ef.js %}‍

Start the server as follows:

{% gist  https://gist.github.com/SidneyAllen/6de28109a2b66306e49e01d40f390a44.js %}‍

Next, create a public/main.html file with the following HTML contents:

{% gist 
https://gist.github.com/SidneyAllen/a161e6431543c16ded9532865a137416.js %}‍

Here, we use the [EJS template syntax](https://www.webtutpro.com/ejs-template-engine-f148420c9ca5) to display the title variable passed from the index.js file.

Now, start the app using the following command:

{% gist  https://gist.github.com/SidneyAllen/d84d1956674f1a26032731cf59de5db3.js  %}‍
‍
This starts a server that’s listening on port 3000. Go to http://localhost:3000/ with your web browser. You should see a blank page with the “This is a title!!!” text.

## Connecting to the PostgreSQL database
Let’s connect to the database and retrieve some records. Go to your terminal and run the following command to [install node-postgres (pg)](https://node-postgres.com/) to connect to PostgreSQL:

{% gist  https://gist.github.com/SidneyAllen/717e229a6cba3e79a59fb3a6be4457ce.js %}‍

We use the node-postgres module to create a [pool of connections](https://www.techiediaries.com/postgresql-connection-pool-client-example). This is useful if we don’t want to open a client and close it for each query to the database.

Some resources you won’t need for this tutorial, but can explore later.

* [Listing users in psql](https://kb.objectrocket.com/postgresql/how-to-list-users-in-postgresql-782)
* [Updating/resetting the password](https://chartio.com/resources/tutorials/how-to-set-the-default-user-password-in-postgresql/) of a known user
* [Creating a user](https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e), a db, and giving the new user permissions

Go back to the index.js file and add the following code:

{% gist  https://gist.github.com/SidneyAllen/e704768c41fd6f6907e9b7fa35cf9e0f.js %}‍

## Getting data from the database
Update the existing app.get method in your code as follows:

{% gist  https://gist.github.com/SidneyAllen/1bcfbc65a5d68c9c73f483454321bfd6.js %}‍

Next, open the main.html file and update it as follows:

{% gist  https://gist.github.com/SidneyAllen/4682ec3a30891bf258a36fac281d1b5d.js  %}‍

Create an HTML table, and loop over the addresses array passed from the index.js file to create the table rows using the [embedded JavaScript template](https://ejs.co/#docs). Go to your browser and refresh your app. You should see something like the following screenshot:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o16vuxo7wv7ov9n6kqti.png)

At this stage, we can retrieve records from our database. Next, let’s see how to use [Lob’s Node.js Address Verification API SDK](https://docs.lob.com/node#us_verifications) to check whether the address records are valid.

Our app is in Node.js, so we rely on the Node SDK from Lob. Lob also offers various SDK options for other languages, including Python, Ruby, PHP, Java, and Elixir.

## Verifying addresses with the Lob API
First, create an account at [Lob.com](https://dashboard.lob.com/#/register) to obtain a live API Key. The key is available in the [Lob Settings Panel](https://dashboard.lob.com/#/settings) and uses the format, live_*.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffzm4r9mguv6u9y50mot.png)

Next, head over to your terminal and run the following command to install the lob-node package:

{% gist  https://gist.github.com/SidneyAllen/2eb27172811fb11bfaee9c93b2872ebc.js %}‍
‍
Now open the index.js file and start by adding the following line after the const port = 3000:

{% gist  https://gist.github.com/SidneyAllen/135d20ce9443b0b33e878f67aed8675b.js %}‍
‍
Update the GET route by adding the following code. First, in the rows array of the results object remove the name and id attributes from the rows as follows:

{% gist  https://gist.github.com/SidneyAllen/29dcef86bc708416280d26671c41e000.js  %}‍
‍

Next, we call the bulkUSVerifications.verify method to verify the addresses in bulk:

{% gist  https://gist.github.com/SidneyAllen/41316eb63e9e4906f617bbf0df5ff639.js %}‍
‍

If there are no errors, add the other two attributes — the [deliverability](https://docs.lob.com/#us_verifications_object) status and the address returned from the Lob API — in each row of the rows array. Then render the main.html file with verified addresses, the number of scanned records, and the valid and invalid addresses.

We use the deliverability string returned from the Lob API to determine the required action according to the different statuses returned by Lob:

If it returns a deliverable status, we take no action.
If it returns an unnecessary, incorrect, or missing unit status, we flag the address for updating, correcting, or manually reviewing.
If it returns an undeliverable status, we flag it for removal from the database.
Add the following switch statement where we have the /* switch statement */ comment:

{% gist  https://gist.github.com/SidneyAllen/e3b3efaf1aa0de0c1d2cb2d763ddca19.js  %}‍

Finally, in the main.html file, let’s display the status and action for each address, the number of records scanned, and how many valid and invalid results we detected:

{% gist  https://gist.github.com/SidneyAllen/8cbf7be126cd73ce49834b428574dad5.js  %}‍

This is a screenshot of the results:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb3sp2ho2ozviod9o6q6.png)

## Conclusion
In this article, we used Node.js to build a web application that integrates with the Lob API to quickly verify addresses in bulk batches and flag bad records found in the database. We used PostgreSQL as a database with fields for address information (name, street address, state, city, and ZIP codes, including valid and invalid addresses). We tested connecting to our database and retrieving records. Then we used Lob’s Node.js Address Verification API SDK to check if the address records are valid.

If you’re interested in bulk address verification and related topics, you can experiment with Lob’s API for free by [signing up](https://dashboard.lob.com/#/register).