Fetch live Indian pincode details using PostalPincode.in API
Project description
pincode_package
A Python package to fetch Indian Pincode and Post Office details using the official India Post API, with offline cache fallback, retry logic, async support, and a CLI tool.
🔍 Search by Pincode
🏤 Search by Post Office Name
💾 Offline cache fallback (OS-safe cache)
⏱ Cache expiry (TTL) to avoid stale data
🔁 Retry with exponential backoff for network resilience
⚡ Async & Sync APIs
🖥 Command Line Interface (CLI)
🌐 FastAPI integration ready
🧪 Fully unit-tested
📦 PyPI-ready structure
Install from PyPI
pip install pincode_package
Install from source (development)
git clone https://github.com/your-username/pincode_package.git
cd pincode_package
pip install -e .
This project uses pyproject.toml (PEP 621) — no requirements.txt needed.
Sync Usage
from pincode_package import fetch_by_pincode, fetch_by_postoffice_name
postoffice = fetch_by_pincode("682001")
pin = fetch_by_postoffice_name("Ernakulam")
print(postoffice)
print(pin)
Async Usage
import asyncio
from pincode_package.api import fetch_by_pincode_async
async def main():
data = await fetch_by_pincode_async("682001")
print(data)
asyncio.run(main())
Command-Line Interface (CLI)
pincode 682001
pincode Ernakulam
Exmaple Output
Ernakulam | N/A | Ernakulam | Kerala | Ernakulam | 682001
Missing fields are safely displayed as N/A.
Cache Behaviour
Cache location :
~/.cache/pincode_package/cache.json
Cache key: Pincode
Cache value: Post Office list
Cache expiry (TTL): 7 days
Cache used automatically when API is unavailable
Retry & Backoff
Automatic retries on network failure
Exponential backoff:
✔ Retry 1 → 1s
✔ Retry 2 → 2s
✔ Retry 3 → 4s
Improves reliability for unstable networks.
Error Handling
The package uses custom exceptions:
from pincode_package.exceptions import (
APIUnavailableError,
PincodeNotFoundError,
PostOfficeNotFoundError
)
Logging
The package uses Python’s standard logging module. Enable logs in your application:
import logging
logging.basicConfig(level=logging.INFO)
FastAPI Integration Example
from fastapi import FastAPI
from pincode_package.api import fetch_by_pincode_async
app = FastAPI()
@app.get("/pincode/{pincode}")
async def pincode_lookup(pincode: str):
return await fetch_by_pincode_async(pincode)
Run:
uvicorn api_server:app --reload
Testing
Run tests with:
pytest -v
✔ API calls mocked ✔ Cache mocked ✔ Error paths tested
🛠 Requirements
Python 3.8+
requests
httpx
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pincode_package-1.0.tar.gz.
File metadata
- Download URL: pincode_package-1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9cbe9843e405d3ff3db267b944206b447944e316394ab54d19f232f06190fd4
|
|
| MD5 |
d920609497ad3a5204e7ee91d510e1c1
|
|
| BLAKE2b-256 |
e9141693dace76c1b35cb7da40da6ad9a3f6925d57c759b9ab4fa1ecabd16b14
|
File details
Details for the file pincode_package-1.0-py3-none-any.whl.
File metadata
- Download URL: pincode_package-1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2c62f06ab3ac8365ac783f9847dc3f495513043904e14426d90ea634606f904
|
|
| MD5 |
e7c1a52022807e8203866b6e3f353cb7
|
|
| BLAKE2b-256 |
909ec7dd14c44a766ded3aab9f0905d056dff41a70f391a21aeedfa3ba25eb1d
|