Skip to main content

countrystatecity-postal-codes

PyPI Python Version License Type Checked postal-codes postal-codes

Official Python package for postal/ZIP code data — 844,248 postcode/locality records representing 672,370 distinct codes across 125 countries, plus format metadata for 250 countries and validation regexes for 189. Part of the countrystatecity ecosystem.

Like the countries package, postcode data is lazy-loaded per country — installing the package doesn't load anything into memory until you ask for a specific country's data.

From offline prototype to production

This package provides a versioned offline snapshot. For regularly updated data, server-side search and filtering, field-selected responses, or managed availability and support, use the Country State City API.

Get a free API key · API docs · Pricing · Migration guide

Keep API keys in server-side environment variables, never in client-side code or source control.

Installation

pip install countrystatecity-postal-codes

Quick Start

from countrystatecity_postal_codes import (
    get_countries_with_postal_data,
    get_postal_info_by_country,
    get_postcodes_of_country,
    get_postcode_by_code,
    get_postcodes_by_code,
    search_postcodes,
    validate_postcode,
)

# Postal code format/regex for a country
us_info = get_postal_info_by_country("US")
# CountryPostalInfo(countryCode="US", postalCodeFormat="#####-####", postalCodeRegex="^\\d{5}(-\\d{4})?$", ...)

# Validate a postcode against the country's known format
validate_postcode("US", "10001")   # True
validate_postcode("US", "abcde")   # False

# All postcodes for a country (lazy loaded)
postcodes = get_postcodes_of_country("AD")
# [Postcode(code="AD100", localityName="Canillo", ...), ...]

# Look up the first entry for a postcode
pc = get_postcode_by_code("AD", "AD100")
print(pc.localityName)  # Canillo

# Get every locality when a postcode is shared
matches = get_postcodes_by_code("BB", "BB18000")
# [Postcode(localityName="Crane", ...), Postcode(localityName="Six Cross Roads", ...)]

# Search by code or locality name within a country
results = search_postcodes("AD", "canillo")

# List postal-format metadata for every country
all_countries = get_countries_with_postal_data()

Data Model

class CountryPostalInfo(BaseModel):
    countryCode: str               # ISO2 country code (e.g., "US")
    countryName: str                # Country name (e.g., "United States")
    postalCodeFormat: Optional[str] # Format pattern (e.g., "#####")
    postalCodeRegex: Optional[str]  # Validation regex (e.g., "^\\d{5}$")
    postcodeCount: int              # Number of individual postcodes available for this country

class Postcode(BaseModel):
    code: str                       # The postal code value (e.g., "10001")
    countryCode: str                # ISO2 country code (e.g., "US")
    stateCode: Optional[str]        # State/province code, if known
    localityName: Optional[str]     # Human-readable place name
    type: Optional[str]             # Granularity: full | outward | sector | district | area
    latitude: Optional[float]
    longitude: Optional[float]

API Reference

Function Description
get_countries_with_postal_data() Get postal format/regex metadata for all countries
get_postal_info_by_country(code) Get postal format/regex metadata for an ISO2 country code
get_postcodes_of_country(code) Get all postcodes for a country (lazy loaded)
get_postcode_by_code(code, postcode) Get the first entry for a postcode within a country
get_postcodes_by_code(code, postcode) Get every entry when a postcode covers multiple localities
search_postcodes(code, query) Search postcodes within a country by code or locality name
validate_postcode(code, postcode) Validate a postcode against the country's known format

Coverage

Individual postcode listings are available for 125 countries with source data upstream: 844,248 postcode/locality records representing 672,370 distinct codes, with varying granularity per country. get_postal_info_by_country() returns format metadata for all 250 countries and validation regexes for the 189 countries where one is known, even if per-postcode listings aren't available. Check postcodeCount to see how many records are available for a given country.

License

ODbL-1.0 — see LICENSE.

Other Packages in this Ecosystem

Package Description
countrystatecity-countries 250 countries, 5,308 states, 171,938 cities
countrystatecity-timezones 432 IANA timezones with country associations and time conversion
countrystatecity-currencies 249 country/currency associations
countrystatecity-translations 4,724 translations in 19 languages
countrystatecity-phonecodes International phone/dialing codes for 250 countries
countrystatecity-regions Region and subregion associations for 250 countries

Data sourced from countries-states-cities-database.


Made with ❤️ by dr5hn

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

countrystatecity_postal_codes-1.0.2.tar.gz (6.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

countrystatecity_postal_codes-1.0.2-py3-none-any.whl (7.4 MB view details)

Uploaded Python 3

File details

Details for the file countrystatecity_postal_codes-1.0.2.tar.gz.

File metadata

File hashes

Hashes for countrystatecity_postal_codes-1.0.2.tar.gz
Algorithm Hash digest
SHA256 97f9d35ca44d9044427e1896f63952d59a38d0d3915cb48a08d65759823939e1
MD5 2cb62d6dfe96c4308185aa4db47ea197
BLAKE2b-256 d961877c29f3460db0aaa161505f2cb3e28ac1e0d2478d30235450a8527cd6a7

See more details on using hashes here.

File details

Details for the file countrystatecity_postal_codes-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for countrystatecity_postal_codes-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cd03af529060a39f59ea4e3ee3b47ec03f4dc328d2964952c529ae12ceea22ab
MD5 9fc421c77fead6071304c5c9fe6e8db3
BLAKE2b-256 9690a8141bda0741b27b862063d41c19be796bbe20b581fda27a5798026f26f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page