> For the complete documentation index, see [llms.txt](https://docs.mapbox.com/help/llms.txt)

# Add Address Autofill to a form on a website

In this tutorial, you will learn how to add [Mapbox Address Autofill](https://docs.mapbox.com/mapbox-search-js/api/web/autofill/) to a form on your website. Address Autofill is a feature provided by the [Mapbox Search JS](https://docs.mapbox.com/mapbox-search-js/) library. It helps users fill out address forms by suggesting addresses as they type. This can significantly improve the user experience on your website and prevent errors in address input, which is especially important for e-commerce sites and other applications that require address input.

You will learn how to:

-   Set up an HTML address form
-   Add autocomplete attributes
-   Import Mapbox Search JS
-   Initialize Address Autofill

This tutorial is geared towards e-commerce websites and other simple applications with HTML forms. For complex single-page applications (SPAs) or React, consider using [Mapbox Search JS React](https://docs.mapbox.com/mapbox-search-js/api/react/) or wrapping the HTML Custom Element for Address Autofill.

> **Related content (related): [Search JS React](https://docs.mapbox.com/mapbox-search-js/api/react/)**
> 
> Integrate rich UI components in React, built on top of Mapbox Search JS. This is the recommended entry-point for large-scale applications.

> **Related content (example): [Autofill suggestions (custom elements)](https://docs.mapbox.com/mapbox-search-js/example/simple-autofill-custom-elements/)**
> 
> Use HTML Custom Elements to add autofill suggestions with Address Autofill in this interactive demo.

## Prerequisites

To complete this tutorial, you will need:

-   **A Mapbox access token**: Find yours on the [Access token page](https://console.mapbox.com/account/access-tokens/) of your Developer Console.
-   **Ability to change your site.** Like other frameworks (for example, analytics), you should have a way to add "tags" or code snippets to your website for use at checkout.
    -   If you do not have direct HTML access, [Google Tag Manager](https://developers.google.com/tag-platform/tag-manager/web) allows you to run JavaScript on your site without changing the HTML.

## Find the HTML for your address input form

Your address form will have a `<form>` element with several `<input>` elements for the address fields. The form should include fields for the address, apartment number, city, state, country, and postcode.

If you don't have an address form in your website, you can add one using the following HTML snippet as a starting point. This is a simple form with the necessary fields for Address Autofill, along with some basic styling.

Title: `index.html`

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Mapbox Autofill Form</title>
  <style>
    body {
      font-family: sans-serif;
      padding: 2rem;
      background: #f5f5f5;
    }

    form {
      background: #fff;
      padding: 1.5rem;
      max-width: 800px;
      margin: auto;
      border-radius: 6px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .field-group {
      display: flex;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .field-group > div {
      flex: 1;
      min-width: 100px;
    }

    label {
      display: block;
      margin-top: 1rem;
      margin-bottom: 0.3rem;
      font-weight: 500;
    }

    input {
      width: 100%;
      padding: 0.5rem;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <form>
    <h3>Shipping Address</h3>
    <div class="field-group">
      <div>
        <label for="address">Address</label>
        <input name="address" type="text" />
      </div>
    </div>
    <div class="field-group">
      <div>
        <label for="city">City</label>
        <input id="city" name="city" type="text" />
      </div>
      <div>
        <label for="state">State</label>
        <input id="state" name="state" type="text" />
      </div>
      <div>
        <label for="postcode">Postcode</label>
        <input id="postcode" name="postcode" type="text" />
      </div>
    </div>
  </form>
</body>
</html>
```

## Set up autocomplete attributes

Address Autofill requires an HTML [`autocomplete`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) attribute on each of the inputs of your address form. It uses these attributes to know which values to place in each field when the user selects an address from the suggestions (for example, the street address in the address field, the city in the city field, etc.).

You may already have autocomplete attributes set up, but if not, you can add them to your form inputs.

### Check for existing autocomplete attributes

You can check for the existence of autocomplete attributes on your website using the JavaScript Console. After opening the Console, run the following command:

```javascript
document.querySelector('[autocomplete~="street-address"], [autocomplete~="address-line1"]') != null
```

The output will read `true` if autocomplete attributes are present.

If the output is `false`, you will need to add the autocomplete attributes to your form inputs.

### Add autocomplete attributes

If autocomplete attributes are not present, you'll have to add them to each HTML input you'd like to autofill.

Here are common autocomplete attributes for address forms. This is a non-exhaustive list, see the full list at [whatwg.org](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).

| Autocomplete tag | Description | Example |
| --- | --- | --- |
| `autocomplete="address-line1"` | The first line of an address, usually includes the street number and street. | 740 15th St NW |
| `autocomplete="address-line2"` | The second line of an address, usually an apartment number. | Apartment 700 |
| `autocomplete="address-level1"` | The **least** specific administrative level. In the United States, this is the state or territory. | District of Columbia |
| `autocomplete="address-level2"` | The second **least** specific administrative level. In the United States, this is the city. | Washington |
| `autocomplete="address-level3"` | In countries with more than two administrative levels. |  |
| `autocomplete="postal-code"` | The postal code of the administrative level. | 20005 |
| `autocomplete="country"` | The standardized country code. | us |
| `autocomplete="country-name"` | The country name. *Note: These are non-standardized.* | United States |

Here is the form from the previous step with the appropriate autocomplete attributes added to each input. If you have different fields in your form, make sure to adjust the autocomplete attributes to match your inputs.

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Mapbox Autofill Form</title>
  <style>
    body {
      font-family: sans-serif;
      padding: 2rem;
      background: #f5f5f5;
    }

    form {
      background: #fff;
      padding: 1.5rem;
      max-width: 800px;
      margin: auto;
      border-radius: 6px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .field-group {
      display: flex;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .field-group > div {
      flex: 1;
      min-width: 100px;
    }

    label {
      display: block;
      margin-top: 1rem;
      margin-bottom: 0.3rem;
      font-weight: 500;
    }

    input {
      width: 100%;
      padding: 0.5rem;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <form>
    <h3>Shipping Address</h3>
    <div class="field-group">
      <div>
        <label for="address">Address</label>
        <input name="address" type="text" autocomplete="address-line1" />
      </div>
    </div>
    <div class="field-group">
      <div>
        <label for="city">City</label>
        <input id="city" name="city" type="text" autocomplete="address-level2" />
      </div>
      <div>
        <label for="state">State</label>
        <input id="state" name="state" type="text" autocomplete="address-level1" />
      </div>
      <div>
        <label for="postcode">Postcode</label>
        <input id="postcode" name="postcode" type="text" autocomplete="postal-code" />
      </div>
    </div>
  </form>
</body>
</html>
```

## Add Mapbox Search JS

Now that your form has autocomplete attributes in place, you can add Mapbox Search JS and implement Address Autofill. To do this, you will add two `<script>` elements to your site's HTML. The first `<script>` element loads the Mapbox Search JS library, and the second `<script>` element initializes Address Autofill.

Note that you must provide your own Mapbox access token in the `accessToken` parameter of the `mapboxsearch.autofill()` function. This associates your usage of Address Autofill with your Mapbox account for billing purposes.

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Mapbox Autofill Form</title>
  <style>
    body {
      font-family: sans-serif;
      padding: 2rem;
      background: #f5f5f5;
    }

    form {
      background: #fff;
      padding: 1.5rem;
      max-width: 800px;
      margin: auto;
      border-radius: 6px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .field-group {
      display: flex;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .field-group > div {
      flex: 1;
      min-width: 100px;
    }

    label {
      display: block;
      margin-top: 1rem;
      margin-bottom: 0.3rem;
      font-weight: 500;
    }

    input {
      width: 100%;
      padding: 0.5rem;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
  </style>
  <script
      id="search-js"
      defer
      src="https://api.mapbox.com/search-js/v1.2.0/web.js"
      >
  </script>
  <script>
  const script = document.getElementById('search-js');
  script.onload = function() {
      mapboxsearch.autofill({
          accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN'
      });
  };
  </script>
</head>
<body>
  <form>
    <h3>Shipping Address</h3>
    <div class="field-group">
      <div>
        <label for="address">Address</label>
        <input name="address" type="text" autocomplete="address-line1" />
      </div>
    </div>
    <div class="field-group">
      <div>
        <label for="city">City</label>
        <input id="city" name="city" type="text" autocomplete="address-level2" />
      </div>
      <div>
        <label for="state">State</label>
        <input id="state" name="state" type="text" autocomplete="address-level1" />
      </div>
      <div>
        <label for="postcode">Postcode</label>
        <input id="postcode" name="postcode" type="text" autocomplete="postal-code" />
      </div>
    </div>
  </form>
</body>
</html>
```

Note that the Mapbox Search JS `<script>` element includes the `defer` attribute, making sure it doesn't load until after critical elements of your page.

After Mapbox Search JS is loaded, the second script calls [`mapboxsearch.autofill()`](https://docs.mapbox.com/mapbox-search-js/api/web/autofill/#autofill), which adds Address Autofill to your address form.

When the user starts typing in the address field, Address Autofill will suggest addresses based on what they type. When the user selects an address, Address Autofill will fill in the rest of the fields with the address information.

![gif showing Address Autofill in action](https://docs.mapbox.com/help/assets/ideal-img/tutorials--add-address-autofill-to-your-website--final.480.gif)

## Final product

With Mapbox Search JS installed and Address Autofill initialized, your address form is now ready to use. When a user starts typing an address in the input field, Address Autofill will suggest addresses based on what they type. When the user selects an address from the suggestions, the other fields in the form will be automatically filled in with the relevant information.

A working demo based on the form HTML and JavaScript shown in the previous steps is available below.

## Next steps

**Congratulations!** You have integrated Mapbox Address Autofill into your site simplifying the tedious multi-field address form into a single autocomplete search box.

### What we covered

-   Set up an HTML address form
-   Add autocomplete attributes
-   Import Mapbox Search JS
-   Initialize Address Autofill

### Things to try

With address autofill set up, you can try the following:

-   **Add search options**: You can pass in options to customize the behavior of Address Autofill. For example, you can change the language of the suggestions, or specify proximity to get results closer to a specified location. See the [Address Autofill documentation](https://docs.mapbox.com/mapbox-search-js/api/web/autofill/#autofill) for more information.
-   **Add a theme**: You can customize the appearance of the Address Autofill suggestions by adding a theme. This allows you to match the suggestions to your site's design. See the [Address Autofill theming documentation](https://docs.mapbox.com/mapbox-search-js/api/web/autofill/#autofill-theming) for more information.

### Learn More

Now that you've setup Address Autofill on a website you can explore other [Mapbox Search JS](https://docs.mapbox.com/mapbox-search-js/) examples like

-   [Address Confirmation](https://docs.mapbox.com/mapbox-search-js/example/confirm-address/)
-   [Minimap](https://docs.mapbox.com/mapbox-search-js/example/minimap/)
-   [Add a search box to a web map](https://docs.mapbox.com/mapbox-search-js/example/add-search-box-to-map/)