A full-stack, responsive web application built with Next.js 14 (App Router), TypeScript, Tailwind CSS, and MongoDB with Prisma ORM. Powered by India Post's official live public API (
api.postalpincode.in) paired with a cache-first database strategy.
- Production Deployment: https://pin-code-sandy.vercel.app/
- Local Development:
http://localhost:3000
The application uses a Cache-First, API-Fallback architecture:
[ User Search Request ]
โ
โผ
โโโโโโโโโโโโโโโโโโโโ Cache Hit (< 30 days)
โ Next.js API โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโบ [ Return Cached Response ] (HTTP 200, X-Cache: HIT)
โ Proxy Route โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โ Cache Miss / Expired
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ India Post Live Public REST API โ
โ (https://api.postalpincode.in) โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโบ Filter to Bangalore/Karnataka
โ
โโโโบ Asynchronously Write to MongoDB (PincodeCache Table)
โ
โผ
[ Return Fresh Response ] (HTTP 200, X-Cache: MISS)
- Performance: Reduces round-trip latency from ~500ms (live API call) down to ~15ms for cached lookups.
- Reliability & Rate-Limiting Protection: Protects India Post's free public endpoint from being overwhelmed during heavy traffic spikes while keeping the app online even if the external service experiences downtime.
- Framework: Next.js 14 (App Router) with TypeScript
- Live External API: India Post Public API (
https://api.postalpincode.in) โ No API Key Required - Database Caching Layer: MongoDB with Prisma ORM (
PincodeCachemodel) - Styling: Tailwind CSS with sleek dark/light design system
- State Management: React
useState&useCallbackwith 300ms debouncing - Icons: Lucide React
- Testing:
- Jest (
ts-jest) withglobal.fetchmocks for unit testing API routes & caching paths. - Cypress for End-to-End (E2E) search flow & error handling validation.
- Jest (
- Live India Post Integration:
- Pincode Lookup:
GET /api/pincodes/[code]-> fetcheshttps://api.postalpincode.in/pincode/{code}. - Area Reverse Lookup:
GET /api/pincodes/search?area=koramangala-> fetcheshttps://api.postalpincode.in/postoffice/{area}.
- Pincode Lookup:
- First-Load Cached Preview:
GET /api/pincodes/cachedpopulates the homepage on initial load so the interface is immediately populated with popular/recently searched Bangalore postal codes before the user types.
- Smart Filter:
- Automatically filters all raw India Post responses to ensure only authentic Bangalore / Bengaluru / Karnataka entries are returned.
- Distinct Error States:
- 400 Bad Request: Invalid pincode format (rejected before calling external API).
- 404 Not Found: No matching post office or area found.
- 503 Service Unavailable: India Post API timeout or service downtime.
- Modern Accessible UI:
- Mobile-first layout (375px+ responsive).
- Switchable Cards grid and Table view.
- Skeleton loading states while network calls resolve.
- Copy pincode to clipboard with toast feedback.
- ARIA live region (
aria-live="polite") announcing result counts for screen readers.
git clone https://github.com/ayshrosine/Pin_Code.git
cd Pin_Code
npm installCreate a .env file in the root directory:
# MongoDB Connection String
DATABASE_URL="mongodb://localhost:27017/pincode_db"npx prisma generatenpm run devOpen http://localhost:3000 in your browser.
Fetches pincode details. Checks MongoDB cache first; if cache miss, queries India Post live API and caches result.
-
Parameters:
code(6-digit numeric string) -
Header Response:
X-Cache: HITorX-Cache: MISS -
Example:
GET /api/pincodes/560034 -
Success Response (
200 OK):{ "data": [ { "id": "66be18a123f1a23456789034", "code": "560034", "areaName": "Koramangala", "district": "Bangalore", "state": "Karnataka" } ], "count": 1, "source": "cache", "query": "560034" } -
Invalid Format Error (
400 Bad Request):{ "error": "Invalid pincode format. Must be a 6-digit numeric string." } -
Not Found Error (
404 Not Found):{ "error": "Pincode '560099' not found." } -
Service Error (
503 Service Unavailable):{ "error": "Service temporarily unavailable, please try again." }
Searches post offices by area name.
- Query Parameters:
area(string, e.g.,Koramangala,Whitefield) - Example:
GET /api/pincodes/search?area=Koramangala - Success Response (
200 OK):{ "data": [ { "code": "560034", "areaName": "Koramangala", "district": "Bangalore", "state": "Karnataka" } ], "count": 1, "source": "live-api", "query": "Koramangala" }
Returns all currently cached Bangalore pincodes for instant first-load presentation.
- Example:
GET /api/pincodes/cached - Success Response (
200 OK):{ "data": [...], "count": 16, "message": "Recently searched & cached pincodes retrieved successfully." }
Run unit tests verifying format validation, fetch mocking, 404 mapping, and cache-hit bypass:
npm testStart dev server (npm run dev) and run Cypress:
npx cypress runbangalore-pincode-explorer/
โโโ app/
โ โโโ api/
โ โ โโโ pincodes/
โ โ โโโ [code]/
โ โ โ โโโ route.ts # GET /api/pincodes/[code] (cache-first + live API)
โ โ โโโ search/
โ โ โ โโโ route.ts # GET /api/pincodes/search?area=... (area lookup)
โ โ โโโ cached/
โ โ โโโ route.ts # GET /api/pincodes/cached (initial feed)
โ โโโ globals.css # Tailwind styles
โ โโโ layout.tsx # Metadata & Root layout
โ โโโ page.tsx # Main interactive SPA
โโโ components/
โ โโโ SearchBar.tsx # Debounced search bar with mode toggle
โ โโโ ResultCard.tsx # Responsive card / table row
โ โโโ ErrorState.tsx # Distinct 400, 404, 503 error UI
โโโ lib/
โ โโโ db.ts # MongoDB cache functions & fallback data
โ โโโ postalApi.ts # Live India Post API wrapper & timeout filter
โ โโโ prisma.ts # Prisma client singleton
โโโ prisma/
โ โโโ schema.prisma # PincodeCache MongoDB model
โโโ __tests__/
โ โโโ pincode-api.test.ts # Jest test suite (fetch mocks & cache tests)
โโโ cypress/
โ โโโ e2e/
โ โโโ search.cy.ts # Cypress E2E search flow & error tests
โโโ cypress.config.ts # Cypress configuration
โโโ jest.config.js # Jest configuration
โโโ README.md # Project documentation
- India Post Rate Limits: India Post does not publish formal API rate limit documentation. The caching layer actively mitigates rate-limit risks by serving repeat queries directly from MongoDB.
- District Matching: India Post data sometimes uses
"Bangalore","Bengaluru", or"BANGALORE URBAN". ThefilterBangaloreOfficeshelper normalizes these variations.