Lob's website experience is not optimized for Internet Explorer.
Please choose another browser.

Arrow Up to go to top of page
Hero Image for Lob Deep Dives Blog PostAutocomplete Address Verification in React: Add, Validate, and Enhance Location FormsDirect Mail Q&A's
Engineering
October 27, 2021

Autocomplete Address Verification in React: Add, Validate, and Enhance Location Forms

By

Muhammad Martinez

Share this post

Many web applications require users to enter their address, but typing errors and incomplete entries can cause delivery and verification issues. Autocomplete address verification helps reduce these problems by suggesting valid addresses as users type and validating them using address verification.

Modern React applications often use location autocomplete to improve the address entry experience. By integrating autocomplete address validation, forms can offer users real-time suggestions and confirm that submitted addresses exist and are properly formatted. This approach improves address data quality and reduces downstream issues caused by inaccurate inputs.

Set up your React app

Getting started with autocomplete address verification in React is straightforward when you have the right tools. You’ll need a React application and an API key from an address verification service to begin building your location forms.

First, install the package needed for React location autocomplete functionality. Lob provides a React component designed specifically for address autocomplete and verification:

npm install @lob/react-address-autocomplete

After installation, import the component into your React application. This React component handles both autocomplete suggestions and address verification in a single package, which simplifies your development process.

Setting up your API credentials:

  • Test keys: Use these during development to test functionality without processing live data
  • Live keys: Switch to these for production to access full address coverage
  • Environment variables: Store your API keys securely using environment variables in your React app

Add address autocomplete

The React location autocomplete component connects to Lob’s address autocomplete capabilities to provide suggestions as users type. Once integrated, it automatically surfaces address suggestions after a few characters are entered.

Here’s how to implement basic autocomplete functionality:

import { AddressAutocomplete } from '@lob/react-address-autocomplete';

function AddressForm() {

  return (

    <AddressAutocomplete

      apiKey={process.env.REACT_APP_LOB_API_KEY}

      onSelection={(address) => console.log(address)}

    />

  );

}

The component returns formatted address suggestions based on user input. When using a test API key, results are intentionally limited to help you validate the integration. Live keys provide access to the full address database for production use.

Key autocomplete features:

  • Real-time suggestions: Addresses appear as users type, typically after three to four characters
  • Formatted results: Returned addresses follow standardized formatting
  • Geographic filtering: Suggestions can be limited to specific regions or states when needed

If your application needs to support broader location inputs, such as place names or points of interest, you can optionally pair address verification with tools like Google Places Autocomplete to surface predictions, then validate the selected address before submission.

Verify addresses

Address verification confirms that a selected or entered address actually exists and can receive mail. This step typically happens after autocomplete selection and helps ensure your form collects valid, deliverable addresses.

The verification process checks the address against postal databases and returns details about address quality and deliverability.

import { verifyAddress } from '@lob/react-address-autocomplete';

const handleAddressVerification = async (address) => {

  try {

    const verifiedAddress = await verifyAddress(address, {

      apiKey: process.env.REACT_APP_LOB_API_KEY

    });

    if (verifiedAddress.deliverability === 'deliverable') {

      setValidAddress(verifiedAddress);

    }

  } catch (error) {

    console.error('Address verification failed:', error);

  }

};

Verification response details:

  • Deliverability status: Indicates whether the address can receive mail
  • Standardized format: Returns the address in postal-compliant formatting
  • Component breakdown: Separates street, city, state, and ZIP code fields
  • Additional details: May include unit numbers or secondary address information

For a deeper look at why verification matters, see Lob’s guide to address verification best practices.

Display an address form

When building an address form, you can choose between a streamlined single-line autocomplete input or a more traditional multi-field layout.

Single-line autocomplete
This approach uses one input field where users type and select from suggestions.

Multi-line address form
This option separates address components into individual fields and allows for more granular control.

function AddressForm() {

  const [verificationStatus, setVerificationStatus] = useState(null);

  const handleAddressSelection = async (address) => {

    const verification = await verifyAddress(address);

    setVerificationStatus(verification.deliverability);

  };

  return (

    <div>

      <AddressAutocomplete

        apiKey={process.env.REACT_APP_LOB_API_KEY}

        onSelection={handleAddressSelection}

        placeholder="Start typing your address..."

      />

      {verificationStatus && (

        <div className={`status-${verificationStatus}`}>

          Address status: {verificationStatus}

        </div>

      )}

    </div>

  );

}

Handle advanced use cases

More complex applications may require additional address-handling logic.

International address support
Lob primarily supports U.S. address autocomplete. For international addresses, you can use Lob’s international address verification with a valid country code.

Custom validation rules
You can layer business-specific logic on top of verification results, such as restricting deliveries to certain states or regions.

const handleCustomValidation = (verifiedAddress) => {

  if (verifiedAddress.components.state !== 'CA') {

    return { valid: false, message: 'We only deliver to California' };

  }

  return { valid: true };

};

Autocomplete address verification helps React applications collect cleaner data, reduce form errors, and create more reliable user experiences. By combining real-time suggestions with verification, you can ensure addresses are standardized and deliverable before they enter your systems.

If you want to see how Lob supports address verification at scale, book a demo.

Frequently asked questions about React address autocomplete

FAQs

Does Lob’s React address autocomplete work outside the United States?

Autocomplete primarily supports U.S. addresses. International addresses can be verified using Lob’s international verification endpoints.

How do I switch from test API keys to live API keys?

Replace your test API key with a live key in your environment variables. Live keys provide full address coverage and complete verification results.

Why are autocomplete results limited when using test keys?

Test API keys intentionally limit results to support development and testing workflows.

What’s the difference between address autocomplete and address verification?

Autocomplete suggests addresses as users type. Verification confirms that the selected address is valid and deliverable.

Answered by:

Continue Reading