Prompt
How do I validate addresses before buying postage through an API?
Latest observation
To validate addresses before buying postage through an API, the usual flow is:
- Collect the recipient address
- Send it to an address validation / verification endpoint
- Inspect the normalized result
- Only buy postage if the address is valid or deliverable enough for your policy
Typical validation workflow
1) Parse and normalize the input
Take whatever the user entered and split it into standard fields:
- name
- street line 1
- street line 2
- city
- state/province
- postal code
- country
Even if the API accepts free-form text, structured input tends to validate better.
2) Call an address verification API
Most shipping APIs provide an endpoint to check an address before label purchase. Common checks include:
- Valid: the address exists
- Standardized: the address is corrected to postal-service format
- Deliverable: mail can be delivered there
- Residential/commercial: sometimes returned too
- Geocode / rooftop match: sometimes for higher confidence
The API usually returns one of these outcomes:
- Valid / verified
- Corrected / suggested
- Invalid
- Ambiguous / needs review
- Missing unit/suite
- PO box / military / unsupported type
3) Compare the returned normalized address
If the API returns a corrected address, decide whether to:
- automatically accept it
- show the user the corrected version and ask them to confirm
- reject if the change is too large
A good practice is to require user confirmation if the street/city/state changes, but auto-accept minor formatting changes like ZIP+4 normalization.
4) Apply your business rules
Examples:
- Hard block if invalid
- Allow if corrected and confirmed
- Warn if unit/suite is missing
- Allow manually for international addresses if your carrier supports them
- Fallback to manual review for ambiguous results
5) Only purchase postage after validation passes
Once the address is verified, create the shipment and buy the label.
What to look for in the API response
A robust validation response often includes:
is_validdeliverabilitynormalized_addressconfidencechanges_mademissing_componentsverification_level
Example logic:
result = validateAddress(address)
if result.is_valid and result.deliverability in ["deliverable", "likely_deliverable"]:
if result.changes_made:
showUserCorrection(result.normalized_address)
else:
buyPostage(address)
else:
rejectOrFlag(result.errors)
Recommended validation strategy
For checkout flows
- Validate in real time as the user types or on form submit
- Use autocomplete if available
- Confirm standardized suggestions before purchase
For batch or backend flows
- Validate before label creation
- Log invalid addresses and error reasons
- Retry only if user data is updated
Edge cases to handle
- Apartment/suite missing: address may be valid but undeliverable without unit number
- PO Boxes: some carriers/services won’t ship to them
- Military addresses: require special formatting
- International addresses: validation quality varies by country
- Rural addresses: may require local standards or geocoding
- No postal code: sometimes acceptable internationally, often not domestically
Best practices
- Always validate before buying postage
- Store both the original and normalized address
- Don’t silently change user-entered data without telling them
- Use a carrier/API that supports the destination country
- Handle ambiguous results with a user confirmation step
- Log validation failures for support/debugging
Example implementation pattern
- User enters address
- Your server sends address to validation API
- API returns standardized address and validity status
- If valid, create shipment
- If not, show error or suggested correction
- After confirmation, purchase postage
If you tell me which API/provider you’re using (for example Shippo, EasyPost, Pirate Ship, UPS, USPS, FedEx, or a custom API), I can show the exact validation endpoint and a code example in your language.