Lob's new Address Verification API is designed to help customers streamline their direct mail operations by maximizing the deliverability of every address before it's stored in their CRM or other data repository. This tutorial shows our new API in action. We'll build a web app in Rails that verifies an address as soon as it's entered on sign-up.
Hate to read instructions? We’ve got all the source code for this example available in a public repository.
This tutorial will show you how to build a website that will determine if a mailing address is deliverable when you sign up!
Before diving into the code, we will need to register for a Lob API key.
Visit the registration page and sign up for an account. You will be given a Test API Key which will give you full access to the API in a sandbox environment. When in Test mode, the API will only return a sample address.
First, let’s have Rails setup up our project:
Add the following lines to the Gemfile:
Then, install the above dependencies:
We’re going to be using Devise, a popular, flexible authentication solution for Rails.
After this, we’ll create a User table and model by utilizing another one of Devise’s generators.
Now that we have Devise set up, we’ll need to add a few fields to the User table to collecting a mailing address.
Now we can run the migrations.
Now, this is where the magic happens. We’ll add in a validation to check if an address is deliverable in the User model.
We’ll need to modify the registrations controller and views created by Devise to accept these relevant fields upon sign up.
class RegistrationsController < Devise::RegistrationsController
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %>
<%= f.label :email %>
<%= f.email_field :email, autofocus: true %>
<%= f.label :password %> <% if @minimum_password_length %> (<%= @minimum_password_length %> characters minimum) <% end %>
<%= f.password_field :password, autocomplete: "off" %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :address_line1 %>
<%= f.text_field :address_line1 %>
<%= f.label :address_line2 %>
<%= f.text_field :address_line2 %>
<%= f.label :address_city %>
<%= f.text_field :address_city %>
<%= f.label :address_state %>
<%= f.text_field :address_state, maxlength: 2, size: 4 %>
<%= f.label :address_zip %>
<%= f.text_field :address_zip, maxlength: 11, size: 15 %>
<%= f.label :address_country %>
<%= f.text_field :address_country, maxlength: 2, size: 4 %>
<%= f.submit "Sign up" %>
<% end %> <%= render "devise/shared/links" %>
Let’s add a home page and some quick routing logic so we can see a preview of what we have so far. Running the command below, we’ll have a empty controller and view generated for us.
$ rails generate controller home index
And we’ll make a very simple view.
You've signed up!
Now if we start up Rails, you should be able to sign up and you will see an error if you sign up with an undeliverable address.
$ rails s
You can see the full source code for this example here.