1. Addresses
  2. Autocomplete

Addresses

Autocomplete

Autocomplete allows you to submit a piece of an address string and have the API respond with the best possible options. For better user experience, the API does not respond with unit numbers but instead expects the UI to include another input field for the user to enter their line2 address.

region: Region is an optional parameter that can be used to filter the results to a specific country. The region parameter is a two-letter country code that can be used to filter the results to a specific country. If the region parameter is not provided, the API will return results from all countries.

Request

POST
`/v1/address/autocomplete`
js
        const data = {
  address: "500 7th Ave",
  region: "us",
};

fetch("https://api.packagex.io/v1/address/autocomplete", {
  method: "POST",
  headers: {
    "PX-API-KEY": process.env.PX_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(data),
});

      

Response

You'll then be returned the best five predictions that the API has for your input. The JSON response would look something like this:

        {
  "message": "5 predictions returned",
  "data": [
    {
      "address": "500 7th Avenue, San Diego, CA, USA",
      "matched_text": "500 7th Avenue",
      "autocomplete_text": "San Diego, CA, USA",
      "description": null
    },
    {
      "address": "500 7th Avenue, New York, NY, USA",
      "matched_text": "500 7th Avenue",
      "autocomplete_text": "New York, NY, USA",
      "description": null
    },
    {
      "address": "500 South 7th Avenue, City of Industry, CA, USA",
      "matched_text": "500 South 7th Avenue",
      "autocomplete_text": "City of Industry, CA, USA",
      "description": null
    },
    {
      "address": "500 7th Avenue, San Francisco, CA, USA",
      "matched_text": "500 7th Avenue",
      "autocomplete_text": "San Francisco, CA, USA",
      "description": null
    },
    {
      "address": "500 7th Avenue South, Kirkland, WA, USA",
      "matched_text": "500 7th Avenue South",
      "autocomplete_text": "Kirkland, WA, USA",
      "description": null
    }
  ],
  "status": 200,
  "errors": [],
  "code": null,
  "pagination": null,
  "endpoint": "/v1/address/autocomplete"
}

      

Usage

If using autocomplete on the frontend, make sure that you use a debouncing mechanism to ensure that requests are sent at reasonable time intervals so that you don't encounter any rate limits on your account.

In addition, many addresses also have a unit, suite, floor, etc. that does not get returned in the autocomplete API. All of our address APIs, when creating a shipment, contact, etc., enable you to add the secondary address line in a property called address_line2. As a result, you'll typically want to also have a secondary input in your form to collect this value.

Commonly after autocompletion the value, you'll then subsequently pass the address and address_line2 properties to the address parsing API to receive back the PackageX address model.


Previous <- Model