Skip to main content

A Python client for the Geocodio API

Project description

geocodio

The official Python client for the Geocodio API.

Features

  • Forward geocoding of single addresses or in batches (up to 10,000 lookups).
  • Reverse geocoding of coordinates (single or batch).
  • Append additional data fields (e.g. congressional districts, timezone, census data).
  • Automatic parsing of address components.
  • Simple exception handling for authentication, data, and server errors.

Installation

Install via pip:

pip install geocodio-library-python

Development Installation

  1. Clone the repository:

    git clone https://github.com/geocodio/geocodio-library-python.git
    cd geocodio-library-python
    
  2. Create and activate a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install development dependencies:

    pip install -e .
    pip install -r requirements-dev.txt
    

Usage

Geocoding

from geocodio import GeocodioClient

# Initialize the client with your API key
client = GeocodioClient("YOUR_API_KEY")

# Single forward geocode
response = client.geocode("1600 Pennsylvania Ave, Washington, DC")
print(response.results[0].formatted_address)

# Batch forward geocode
addresses = [
    "1600 Pennsylvania Ave, Washington, DC",
    "1 Infinite Loop, Cupertino, CA"
]
batch_response = client.geocode(addresses)
for result in batch_response.results:
    print(result.formatted_address)

# Single reverse geocode
rev = client.reverse("38.9002898,-76.9990361")
print(rev.results[0].formatted_address)

# Append additional fields
data = client.geocode(
    "1600 Pennsylvania Ave, Washington, DC",
    fields=["cd", "timezone"]
)
print(data.results[0].fields.timezone.name if data.results[0].fields.timezone else "No timezone data")

List API

The List API allows you to manage lists of addresses or coordinates for batch processing.

from geocodio import GeocodioClient

# Initialize the client with your API key
client = GeocodioClient("YOUR_API_KEY")

# Get all lists
lists = client.get_lists()
print(f"Found {len(lists.data)} lists")

# Create a new list from a file
with open("addresses.csv", "rb") as f:
    new_list = client.create_list(
        file=f,
        filename="addresses.csv",
        direction="forward"
    )
print(f"Created list: {new_list.id}")

# Get a specific list
list_details = client.get_list(new_list.id)
print(f"List status: {list_details.status}")

# Download a completed list
if list_details.status and list_details.status.get("state") == "COMPLETED":
    file_content = client.download(new_list.id, "downloaded_results.csv")
    print("List downloaded successfully")

# Delete a list
client.delete_list(new_list.id)

Error Handling

from geocodio import GeocodioClient
from geocodio.exceptions import AuthenticationError, InvalidRequestError

try:
    client = GeocodioClient("INVALID_API_KEY")
    response = client.geocode("1600 Pennsylvania Ave, Washington, DC")
except AuthenticationError as e:
    print(f"Authentication failed: {e}")

try:
    client = GeocodioClient("YOUR_API_KEY")
    response = client.geocode("")  # Empty address
except InvalidRequestError as e:
    print(f"Invalid request: {e}")

Geocodio Enterprise

To use this library with Geocodio Enterprise, pass api.enterprise.geocod.io as the hostname parameter when initializing the client:

from geocodio import GeocodioClient

# Initialize client for Geocodio Enterprise
client = GeocodioClient(
    "YOUR_API_KEY",
    hostname="api.enterprise.geocod.io"
)

# All methods work the same as with the standard API
response = client.geocode("1600 Pennsylvania Ave, Washington, DC")
print(response.results[0].formatted_address)

Documentation

Full documentation is available at https://www.geocod.io/docs/?python.

Contributing

Contributions are welcome! Please open issues and pull requests on GitHub.

Issues: https://github.com/geocodio/geocodio-library-python/issues

License

This project is licensed under the MIT License. See the LICENSE file for details.

CI & Publishing

  • CI runs unit tests and linting on every push. E2E tests run if GEOCODIO_API_KEY is set as a secret.
  • PyPI publishing workflow supports both TestPyPI and PyPI. See .github/workflows/publish.yml.
  • Use test_pypi_release.py for local packaging and dry-run upload.

Testing GitHub Actions Workflows

The project includes tests for GitHub Actions workflows using act for local development:

# Test all workflows (requires act and Docker)
pytest tests/test_workflows.py

# Test specific workflow
pytest tests/test_workflows.py::test_ci_workflow
pytest tests/test_workflows.py::test_publish_workflow

Prerequisites:

  • Install act for local GitHub Actions testing
  • Docker must be running
  • For publish workflow tests: Set TEST_PYPI_API_TOKEN environment variable

Note: Workflow tests are automatically skipped in CI environments.

Project details


Download files

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

Source Distribution

geocodio_library_python-0.1.0.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

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

geocodio_library_python-0.1.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file geocodio_library_python-0.1.0.tar.gz.

File metadata

  • Download URL: geocodio_library_python-0.1.0.tar.gz
  • Upload date:
  • Size: 50.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for geocodio_library_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b6c903eebbce2433a14253a18fa109c0787bff94dae371e683b10d0b2438517f
MD5 f073a0c6b2f993f88e445d78cb8b6b76
BLAKE2b-256 ac0596faef88aad1f5e74c434bcb698194e5bf24cfa1f80c1068471c7815ccf7

See more details on using hashes here.

File details

Details for the file geocodio_library_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for geocodio_library_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 deebf7f17f133ddbd1e8cd5937e92c121621061c989bec98014298ff6a8645c4
MD5 cc5c24e08a3c11192d747f6118ee798d
BLAKE2b-256 5839f79d54f5a21e0ab38556c9c7cab9ab84c92bba63209f4c09591ceb089b29

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 Pingdom Monitoring Sentry Error logging StatusPage Status page