Fundamentals, chapter 06 of 07
Addresses & Geocoding
Capturing, validating, geocoding and storing addresses: the capture flow, what vendor licenses let you keep, and an architecture that scales from day one.
An address looks like a string and does at least four jobs: it tells a carrier where to deliver, a driver where to park, a tax engine which jurisdictions apply, and an identity check whether this person is who they say. Each job needs a different part of it, at a different precision, and a single address TEXT column can't serve any of them well. A shipping label, a know-your-customer check, a property listing and a service visit all start with the same pipeline: capture, standardize, validate, geocode, and store only what you're allowed to keep.
Capture#
Stop bad addresses at the keyboard, and keep what was typed.
- Use the browser's autofill tokens. The HTML standard defines
address-line1toaddress-line3,address-level1toaddress-level4,postal-codeandcountry, and notes that the order of levels varies by country: a US town (level 2) comes before the state (level 1), while in Japan the prefecture (level 1) comes first (WHATWG: Autofill). Forms & Validation covers the rest of the form. - Offer autocomplete. Google's Place Autocomplete suggests addresses as the user types and returns a place ID for the one picked. For flows that need only an address and coordinates, Google suggests skipping session tokens and calling Geocoding on the selected prediction, which it says costs less when users pick within about four requests.
- Collect the unit separately. The same page notes that apartment and unit addresses can come back as partial predictions. The unit is the part that decides whether a parcel reaches the right door, so give it its own field and validate the whole address afterwards.
- Never block on autocomplete. Let people type an address the suggestions don't contain. Some of them are right and the data is late (see Addresses No Vendor Knows).
Standardize#
Standardizing turns one address written five ways into one form, so that it compares equal to itself and prints the way the post office expects.
In the US, USPS Publication 28 (current edition October 2024) defines the standard delivery line and last line, street suffix abbreviations and secondary unit designators such as APT and STE. Software that matches addresses against the USPS database is certified under CASS, which tests ZIP+4 and delivery point coding, DPV, LACSLink, SuiteLink and RDI; the current cycle's certified products guides expire July 31, 2028. Buy CASS processing, don't build it.
US rules don't generalize. The Universal Postal Union counts more than 200 address formats in the world and publishes the S42 standard of address components and per-country templates (UPU: Addressing solutions). Some countries have no postcodes, some put the postcode before the city, some address by block rather than street. So:
- Model the address as ordered lines plus administrative levels, not
street,city,state,zip. A requiredstatefield is a bug for most of the world. - Store the country as an ISO 3166-1 alpha-2 code on every address, and format for display per country.
- Parse and normalize with a library for international data. libpostal (MIT) parses free-form addresses into components with a model trained on OpenStreetMap and OpenAddresses, and expands abbreviations so "St" and "Street" compare equal. It's for matching and search, not for producing a deliverable address.
Validate#
Validation answers "is this a real, deliverable address?" and returns a cleaned version with a verdict. I default to Google's Address Validation API when the product already uses Google Maps, Smarty for US-heavy workloads where storage terms matter, and Loqate when coverage outside North America is the point. Read each verdict, not just the cleaned address:
| Vendor | What it returns |
|---|---|
| Google Address Validation | Corrected, standardized components and coordinates; a verdict with granularity and flags for inferred, replaced and unconfirmed components; CASS only for US and Puerto Rico, opt-in with enableUspsCass |
| Smarty US Street API | dpv_match_code: Y confirmed, S unit not recognized, D unit missing, N not confirmed; enhanced_match for non-postal matches |
| Loqate Address Verify | An Address Verification Code such as V44-I44-P3-100: status V verified, P partial, A ambiguous, R reverted, U unverified, with match levels down to premise |
D and its equivalents are the failure worth designing for: an apartment building without the unit, which delivers to the leasing office.
What you do with the verdict depends on the job. Google's validation logic guide frames it as fix (the address may not be deliverable), confirm (components were inferred or changed) or accept (confidence is high). A newsletter signup can accept almost anything; checkout should confirm a changed ZIP before shipping; a KYC flow should treat an unverified address as a reason for more evidence, not a typo to auto-correct.
Geocode and Precision#
A geocode is a coordinate plus a statement of how good it is, and the second part matters as much as the first. Google's Geocoding API reports location_type as ROOFTOP, RANGE_INTERPOLATED (an estimate between two known points), GEOMETRIC_CENTER (the center of a street or region) or APPROXIMATE, plus partial_match when only part of the address matched. Smarty reports precision from Zip5 through Street to Rooftop. The US Census Bureau runs a free Census Geocoder for US addresses, including batches.
Store the precision beside the point and gate uses on it (the coordinate itself, its order and how to store it are in Coordinates & Geometry). A ZIP-level point is fine for a regional sales report and wrong for a delivery route, a flood-zone lookup or a tax boundary test. When precision is low and the location matters, get a better one from a person: the customer drops a pin, a courier or field worker confirms it on the first trip, and that pin becomes the location you route to.
What You May Store#
Read the license before you design the table. Google's Address Validation policies generally restrict caching or storing its content, "except for the place ID which can be stored indefinitely," and results shown outside a Google Map need Google's attribution (Google: Address Validation policies). Place IDs themselves can change, and Google recommends refreshing any older than 12 months; a NOT_FOUND means the ID is obsolete (Google: Place IDs). Smarty's high-accuracy geocodes are for internal business use and may be kept "only during the Subscription Term," with copies deleted within 30 days after it ends (Smarty: Product Terms).
Coordinates are where this bites, because the obvious design (geocode once, store the point, query it forever) isn't allowed with Google's data. The Geocoding, Places and Address Validation policies each allow caching latitude and longitude "for up to 30 consecutive calendar days," then require deleting them. The same policies forbid creating content based on Google Maps content, and Google's own example is using Places coordinates for point-in-polygon analysis. A permanent location column filled from Google, queried for territories, radius searches or reports, is exactly what the terms rule out. Address Validation's standardized address follows the same 30-day limit, with one way out: after 30 days you may replace it with data the end user confirmed or corrected, and that version is yours to keep.
So the shape that holds up with Google is: store the place ID as the durable reference, keep what the user typed and your own decision about it, and fetch the cleaned address and coordinates again when you need them, holding them no longer than 30 days. Coordinates you must keep and query for years need a source licensed for storage: a pin a person dropped, the Census Geocoder, or a vendor contract that permits it. That's what location_source in the table below records.
CREATE TABLE addresses (
id UUID PRIMARY KEY DEFAULT uuidv7(),
country TEXT NOT NULL, -- ISO 3166-1 alpha-2
input JSONB NOT NULL, -- what was typed, always yours to keep
match_key TEXT NOT NULL UNIQUE, -- normalized country, lines and unit; one row per place
google_place_xid TEXT, -- storable indefinitely; refresh yearly
place_checked_at TIMESTAMPTZ, -- drives the rolling refresh
standardized JSONB, -- only from a source licensed for storage
validation_status TEXT NOT NULL CHECK (validation_status IN
('validated', 'customer_asserted', 'overridden', 'needs_review', 'unvalidated')),
deliverability TEXT, -- vendor verdict, e.g. DPV Y, S, D or N
location GEOGRAPHY(POINT), -- from a source licensed for storage
location_source TEXT CHECK (location_source IN ('person_pin', 'census', 'vendor')),
location_precision TEXT CHECK (location_precision IN ('rooftop', 'parcel', 'street', 'postal_code')),
geocoded_at TIMESTAMPTZ, -- null until the background geocode lands
time_zone TEXT, -- IANA name, from the location
validated_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
The Capture Flow#
Four steps (choose, search, confirm, save) make a capture flow that works for the customer typing and the courier, crew or visitor arriving. Built with care, it also settles most of the storage problem in What You May Store: the location you keep is one the customer confirmed, on a map seeded from a source you're allowed to store. The drawing shows the three screens of the main path plus manual entry, which rejoins at confirm; saving returns to the address book.
- Start from the address book. A returning customer picks a saved address rather than typing one: a list with their own labels (home, work or a name they choose), the current choice marked, an edit control on each entry, and the search field above it. Choosing an address is one tap.
- Search with autocomplete. Suggestions arrive as the customer types, each with the street on the first line and the city, region and postcode under it, ranked toward where the customer is. The last entry in the list is always "enter address manually."
- Confirm the details. Picking a suggestion opens a form with the chosen address at the top and the parts no geocoder knows:
- The type of place (house, apartment, office, hotel), which decides what else to ask.
- Unit or floor, building name, and gate or door code.
- A map with a pin and an "adjust pin" control to drag it to the entrance a courier should use. Even a rooftop geocode lands on the building or a parcel's center; the entrance can be around the corner. Seed the pin from one geocode you may store, made when the customer picks the address (One Geocode in the Request): a pin that starts at a Google geocode and is only confirmed is still Google's coordinate. Let it move only within a radius of that seed, sized by the seed's precision (small around a rooftop match, larger around a street-level one) and drawn on the map so the customer sees the limit. A slipped finger or a bad actor then can't send the delivery to another block. On save, check the same radius on the server:
ST_DWithin(saved_addresses.entrance, addresses.location, r), whereris the radius you set for the address'slocation_precision(Coordinates & Geometry). - How to hand over (leave at the door, leave in the lobby, hand to me) and free-text instructions, with a hint saying what belongs there and what doesn't (order changes go elsewhere).
- A personal label that only the customer sees.
- Save. The address joins the book, and every order copies it (see below).
Manual entry asks for the country first, because the country decides which fields exist and in what order (Standardize). Don't preselect a state or province: a default that looks filled in gets submitted unchanged. Validate it and make the seed geocode when the customer continues, then send them to step 3 with the map on the typed address, so it gets a confirmed pin within the same radius as any other. If nothing geocodes it (new construction is the usual reason), show the map without a radius, let the customer place the pin, and save the address as customer_asserted (Addresses No Vendor Knows).
What Each Step Stores
| Data | Comes from | May you keep it? |
|---|---|---|
| What the customer typed | The customer | Yes |
| The address picked from Google Autocomplete | The customer's selection | For that customer's transaction: Google exempts "the end user's selected address" from its content restrictions, but not the list of suggestions (Places policies) |
| Google place ID | Indefinitely; refresh after 12 months | |
| Coordinates from Google | Up to 30 days, then delete (What You May Store) | |
| Coordinates from a storage-licensed geocoder | Esri with forStorage, Mapbox permanent, Smarty for the subscription term, the Census Geocoder |
Yes, on the vendor's terms (Where the Stored Point Comes From) |
| The confirmed pin | The customer, on a storable seed | Yes |
| Unit, building, type, instructions, label | The customer | Yes |
| Gate or door code | The customer | Yes, encrypted, shown only during an active job |
Each customer's use of a place gets its own row beside the place:
CREATE TABLE saved_addresses ( -- one customer's use of an address
id UUID PRIMARY KEY DEFAULT uuidv7(),
customer_id UUID NOT NULL REFERENCES customers (id),
address_id UUID NOT NULL REFERENCES addresses (id),
label TEXT, -- 'home', 'work' or the customer's own; private to them
place_type TEXT CHECK (place_type IN ('house', 'apartment', 'office', 'hotel', 'other')),
building_name TEXT,
entrance GEOGRAPHY(POINT), -- the customer's confirmed pin, seeded from a storable source
access_code BYTEA, -- gate or door code, encrypted in the application
handoff TEXT CHECK (handoff IN ('leave_at_door', 'leave_in_lobby', 'hand_to_me')),
instructions TEXT,
last_used_at TIMESTAMPTZ, -- orders the address book, most recent first
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
The unit stays in addresses, with the lines it belongs to, because it changes deliverability. Everything about how one customer uses the place, including the entrance pin and the gate code, lives in saved_addresses. The entrance stays per customer, since two tenants of one building can use two doors. It becomes the place's own location, with source person_pin, only when nothing else geocodes the place (as with new construction) or a courier or crew confirms it on site.
A gate code opens a gate. Encrypt it in the application (Encryption at Rest), show it to the courier or technician only during an active job, never log it, and leave it out of analytics exports. Free-text instructions deserve the same caution in exports, since people type phone numbers and names into them.
Orders copy the address. At checkout, write the address as it stands (lines, unit, pin, the encrypted access code, instructions) onto the order. Editing the address book afterwards then changes future orders without rewriting receipts, disputes or the record of where a delivery was sent.
Architecture That Scales From Day One#
None of this needs a big system on day one, but a few decisions are cheap at the first address and expensive at the ten-millionth: one row per place, everything but one seed geocode off the request path, a stored point you're licensed to keep, refreshes that trickle instead of stampede, and vendors behind an interface.
One Address, Many Customers
A building with two hundred tenants is one place and two hundred saved addresses. Keep addresses as one row per place (lines, unit and country) and saved_addresses as each customer's use of it, and the geocoding, validation and refresh work scales with places, not with customers or orders.
Deduplicate on a match_key built only from data you're allowed to keep: the customer's input normalized with a parser such as libpostal (case, abbreviations, punctuation and unit designators made uniform), or a standardized form from a source licensed for storage. A key built from Google's standardized address would have to be deleted after 30 days along with the address it came from. Insert with the key under a unique constraint (INSERT ... ON CONFLICT (match_key) DO NOTHING, then read the row), so two customers saving the same building at the same moment end up on one row (Match Before Create).
One Geocode in the Request, the Rest in the Background
Two calls belong in the customer's request: autocomplete, which runs in the client, and one seed geocode from a storable source when the customer picks or types an address, which centers the confirm screen's map and its radius. Saving writes the row with that point, the customer's confirmed pin and a job in the same transaction (Enqueue Transactionally). The worker does the rest (validation, standardization, the time zone), retries a seed geocode that failed, and writes the point, precision and source back. A slow or failing vendor then delays a background job instead of checkout, and the job retries with backoff (Retries & Timeouts).
Key the job by address_id, so a retried or duplicated job geocodes a place once. Batch where the vendor allows it (Mapbox takes up to 1,000 queries per request, the Census Geocoder 10,000 records per file), and cap the workers' concurrency at the vendor's rate limit, which the queue enforces better than any caller can.
Where the Stored Point Comes From
The 30-day rule applies to Google's data, not to addresses. Keeping millions of geocoded addresses doesn't require keeping Google's coordinates: use Google, or another vendor, for the typing experience, and get the stored point from a source that allows it:
- A geocoder licensed for storage. Esri's geocoding service says applications "are contractually prohibited" from storing results unless the request sets
forStorage=true. Mapbox's Geocoding API defaults to temporary results, which may not be cached; withpermanent=true, results "are allowed to be cached and stored indefinitely," which requires a credit card on file or an enterprise contract. Smarty's terms allow keeping its geocodes for the subscription term (What You May Store). Geocode once, at capture or in a batch, and keep the point with its precision and source. - Public data where it covers you. The Census Geocoder is free for the United States, Puerto Rico and the Island Areas, with batches of up to 10,000 records per file. OpenStreetMap data is open under the ODbL, which requires attribution and puts any database you build from it and distribute under the same license. The public Nominatim service isn't for volume: its usage policy sets an absolute maximum of 1 request per second and forbids systematic queries, so bulk geocoding on OpenStreetMap means running your own Nominatim.
- The customer's pin. Every address that goes through step 3 of the capture flow ends with a point the customer confirmed or moved, on a storable seed. Over time these pins replace vendor geocodes for every address that matters.
- The place ID for anything Google shows. Keep it, and fetch fresh Google details when a customer looks at the place.
What doesn't work is re-geocoding everything with Google every 29 days. It's expensive at millions of rows, and it doesn't touch the other rule: building your own territories, radius searches or reports on Google's coordinates is creating content from Google Maps content, however fresh the coordinates are.
My default for a delivery or field-service product: Google Autocomplete (or the autocomplete of whichever map the product shows) for typing, a storage-licensed geocoder or the Census Geocoder for the stored point with its precision, the customer's pin overriding both, and the place ID kept for display.
Refresh on a Rolling Schedule
Place IDs want refreshing after 12 months, customer_asserted addresses want re-checking until a vendor knows them, and vendor data improves. Done as one yearly job, that's every row at once, a rate-limit wall and a bill spike. Instead, run a daily job that takes the oldest slice of each kind, sized so the whole table cycles within the window: place IDs by place_checked_at (WHERE place_checked_at < now() - interval '12 months' ORDER BY place_checked_at LIMIT n), and customer_asserted addresses by validated_at. The load is flat from the first thousand rows to the hundred-millionth, and a backlog is visible as the age of the oldest row.
Keep Vendors Swappable, and Off the Read Path
- One geocoder interface. Workers call your own
Geocode(address) -> (point, precision, source, license), with an adapter per vendor. Changing vendors, or using different ones per country, is then a configuration change plus a backfill through the same queue. - Every point records its source and license. That's what tells you, the day a contract ends, which rows to re-geocode or delete. Keep each source's answer as an observation rather than overwriting (Observations, Not Overwrites).
- Readers never call a vendor. Dispatch, routing, tax, territories and reports read
addressesandsaved_addresses, through a spatial index (Storing Geometry: PostGIS). The read path's latency and availability are then your database's, not a vendor's, and an address the background geocode hasn't reached yet (geocoded_atnull) is a state the reader handles, not a call it makes. - Deleting a saved address deletes the customer's data, not the place. The
saved_addressesrow, with its label, pin, code and instructions, goes on request (Data Subject Rights); theaddressesrow stays while other customers or past orders use it.
Addresses No Vendor Knows#
New construction, new streets, rural routes and private roads fail validation for months while the customer is standing in the house. Google's guide anticipates it: a user "might have entered an address not in the system, such as for new construction," and it calls it good practice to accept the original entry when the user doesn't respond to prompts. Refusing the order because a vendor hasn't caught up is the wrong answer. Accept it, and mark it for what it is:
- A distinct status:
customer_asserted, notvalidatedand not an error. It's used as typed and visible to whoever ships, routes or bills. - A location you can reach: a pin from the customer, a driver or the office, stored as a person-sourced point.
- Evidence when the address has legal weight (a KYC file, a permit, a warranty): a utility bill, closing documents, a parcel number.
- Tell the vendor which version you used. Google's
provideValidationFeedbacktakes theresponseIdfrom the first validation call and a conclusion:VALIDATED_VERSION_USED,USER_VERSION_USED,UNVALIDATED_VERSION_USEDorUNUSED. - Re-check on a schedule until a vendor confirms it, then retire the flag the way any override retires (Matching, Merging & Many Sources).
Postal City, Service Point, Jurisdiction#
A vendor can confidently return the "wrong" city, and usually it's answering a different question. The city in a postal address is where mail is delivered from. The Census Bureau notes that ZIP Codes are assigned to delivery routes, "a point-based dataset unsuitable for mapping," and builds ZIP Code Tabulation Areas as generalized polygons from census blocks instead (Census: ZCTAs). So a home inside one town's limits can carry a neighboring town's mailing name, and a ZIP Code can cross city and county lines.
| Question | Answer from |
|---|---|
| Where does the mail or parcel go? | The postal (standardized) address |
| Where does a courier, a crew or a visitor go, and which store, depot or territory serves it? | The service point: the best stored coordinate, tested against your own polygons |
| Which city, county and special districts tax, license or regulate it? | Legal boundaries, by point-in-polygon on the service point, never the postal city or ZIP |
| What does the customer call it, and what goes on the contract? | The customer, backed by documents |
Store these separately and never derive one from another. Sales tax by ZIP Code is the classic mistake: the ZIP is a mail route, and the tax belongs to whichever jurisdictions contain the point. When a customer disputes the vendor, record a field-scoped override with evidence rather than editing the vendor's answer away.
Time Zone From the Place#
An address in the future (a delivery window, an appointment, a statement date) needs a local time zone, and the zone belongs to the place, not the account, the user's browser or the office that serves it. Derive an IANA name from the coordinate and store it on the address: Smarty returns iana_time_zone on request, and Google's Time Zone API takes a location and a timestamp and returns the zone ID and its offsets at that moment. Store the name, never the offset, and compute instants from local times as Time & Date Formats describes.
Many Sources#
Over the years an address arrives from autocomplete, a partner's import, a parcel dataset, a different geocoder, an acquired company's database and a person's pin, and they disagree at the edges. Don't let the last writer win. Keep each source's answer as an observation with its key and license, choose each field by written rules, and match before creating a new record: Matching, Merging & Many Sources is the general practice, and Customers, Accounts & Properties applies it to service locations in one industry.
Checklist#
- Capture with autofill tokens and autocomplete, a separate unit field, and an escape hatch for addresses the suggestions don't know.
- Copy the four-step flow: saved addresses, autocomplete with a manual escape, details with an entrance pin, save; orders copy the address.
- Encrypt gate and door codes, and show them only during an active job.
- Keep the raw input beside every cleaned version.
- Standardize with CASS-certified processing in the US and a parser such as libpostal elsewhere; model addresses as lines plus levels and a country.
- Validate, read the verdict, and choose fix, confirm or accept by what the address is for.
- Store precision with every point, and replace low-precision points with a person's pin where location matters.
- Store only what the license allows: with Google, the place ID, refreshed yearly, and coordinates for at most 30 days; stored points from a storage-licensed geocoder, public data or the customer's pin.
- Accept
customer_assertedaddresses, send validation feedback, and re-check until confirmed. - Keep postal address, service point and jurisdictions separate, and never compute tax or territory from a ZIP Code.
- Derive the IANA time zone from the location.
References#
Specifications
- USPS: Publication 28, Postal Addressing Standards, standard US address form, suffix abbreviations and secondary unit designators (October 2024 edition)
- USPS: CASS, certification of address-matching software, what it tests, and the current cycle
- UPU: Addressing solutions, the variety of national address formats and the S42 standard
- WHATWG: HTML, Autofill, address autofill tokens and country-dependent level order
- US Census Bureau: ZIP Code Tabulation Areas, ZIP Codes as delivery routes rather than areas
Reference
- Google: Place Autocomplete (New), session tokens, address-only flows and partial predictions for units
- Google: Address Validation API overview, validation, standardization, geocoding and opt-in CASS for the US and Puerto Rico
- Google: Build your validation logic, fix, confirm and accept, and accepting new construction as entered
- Google: provideValidationFeedback, reporting which address version was used
- Google: Address Validation policies, storage restrictions, storable place IDs and attribution
- Google: Place IDs, changing IDs, yearly refresh and obsolete IDs
- Google: Geocoding API requests, location types and partial matches
- Google: Time Zone API, the zone and offsets for a location at a timestamp
- Smarty: US Street API, DPV match codes, geocode precision, coordinate licenses and IANA time zones
- Smarty: Product Terms, retention and deletion of high-accuracy geocode data
- Loqate: Address Verification Code, verification status, match levels and match score
- US Census Bureau: Census Geocoder, free single and batch geocoding of US addresses
- libpostal, open-source international address parsing and normalization
- Google: Places API policies, caching limits and the exception for an end user's selected address
- Google: Geocoding API policies, the 30-day limit on cached coordinates
- Esri: findAddressCandidates, the
forStorageparameter and the storage prohibition without it - Mapbox: Geocoding API, temporary versus permanent results
- US Census Bureau: Geocoding Services API, batch format, the 10,000-record limit and coverage
- OpenStreetMap: Copyright and License, the ODbL, attribution and share-alike
- OSMF: Nominatim Usage Policy, the request limit and bulk geocoding rules