> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stackfront.digital/llms.txt
> Use this file to discover all available pages before exploring further.

# Addresses

The customer address book: list, default address, and the full create /
update / delete / set-default CRUD — all against the Shopify Storefront API.

```tsx theme={null}
import {
  useAddresses,
  useAddress,
  useDefaultAddress,
  validateAddress,
} from '@stackfront/react-native-shopify';
```

## Address book

```tsx theme={null}
const {
  addresses,
  defaultAddress,
  loading,
  isFetching,
  error,
  refresh,
  createAddress,
  updateAddress,
  deleteAddress,
  setDefaultAddress,
} = useAddresses();
```

| Member | Meaning |
| - | - |
| `addresses` | `CustomerAddress[]`, each with `isDefault` |
| `defaultAddress` | the address marked default, or `null` |
| `loading` | first load (or a manual `refresh`) in flight |
| `isFetching` | any fetch, including background invalidation refetches |
| `refresh()` | re-reads the address book |
| `createAddress(input)` | `MailingAddressInput` → the created address |
| `updateAddress(id, input)` | partial update → the updated address |
| `deleteAddress(id)` | → the deleted id |
| `setDefaultAddress(id)` | → the new default address |

Every mutation invalidates the address cache **and** the cached customer
profile, so all mounted screens agree immediately.

The list and the default address come from **one** Storefront request. While
signed out the hook never fetches and returns empty data.

### Create form

```tsx theme={null}
const [form, setForm] = useState({
  firstName: '',
  lastName: '',
  address1: '',
  city: '',
  country: 'US',
  province: '',
  zip: '',
  phone: '',
});

const validation = validateAddress(form);

const save = async () => {
  if (!validation.isValid) return setErrors(validation.errors);
  await createAddress(form);
  navigation.goBack();
};
```

`validateAddress(input)` is pure and returns
`{ isValid, errors: Record<field, message> }` — the same shape the reference
package used, so form code ports over directly. Required fields: first name,
last name, address line 1, city, country, zip. Province is optional (countries
without subdivisions) and only format-checked when supplied.

`validateAddressUpdate(input)` checks partial update payloads: blank required
fields are rejected when present, formats are validated, untouched fields are
ignored. The service calls the right validator automatically; an invalid input
never reaches the network.

## Single address and default

```tsx theme={null}
const { address, loading } = useAddress(addressId);
const { defaultAddress } = useDefaultAddress();
```

`useAddress` matches by **base GID** (see the note below), and shares the
address-book cache — opening a detail screen from the list does not refetch.

## Address model

```ts theme={null}
interface CustomerAddress {
  id: string;
  firstName: string;
  lastName: string;
  company?: string;
  address1: string;
  address2?: string;
  city: string;
  province: string;
  provinceCode?: string;
  country: string;
  countryCode: string; // Shopify's countryCodeV2
  zip: string;
  phone?: string;
  isDefault: boolean;
}
```

Helpers exported for rendering:

* `formatAddress(address)` — multiline display string
* `formatPhone(phone)` — `(555) 123-4567` for 10-digit US numbers
* `normalizeAddress(raw, defaultAddressId?)` — raw node → model
* `addressBaseId(id)` / `sameAddressId(a, b)` — token-safe comparison

> [!WARNING]
>
> Shopify's `MailingAddress.id` looks like
> `gid://shopify/MailingAddress/123?model_name=CustomerAddress&customer_access_token=…`
> and the token **changes on every response**. Always pass an id back to the SDK
> as you received it; never compare ids with `===`. The SDK compares base GIDs
> internally, and `isDefault` is computed the same way.

## Service API

```ts theme={null}
const { addresses: addressService } = useStackfront();

const list = await addressService.getAddresses();      // CustomerAddress[]
const one  = await addressService.getAddress(id);       // CustomerAddress | null
const created = await addressService.create(input);
const updated = await addressService.update(id, { city: 'Paris' });
await addressService.delete(id);
await addressService.setDefault(id);
```

All methods renew a near-expiry customer token first and throw
`'Not authenticated'` without a session. Shopify-side rejections throw an
`AddressUserError` whose `userErrors` array keeps the `code`, `field` and
`message` for each problem, so a form can map messages to inputs.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.