When a user enters a 6-digit Indian PIN code during checkout or KYC onboarding, checking /^\d{6}$/ is not enough. A six-digit regex passes unallocated numbers like 110000, misses cases where one PIN spans multiple districts or post offices, and still leaves shoppers typing their state, district, and locality by hand, causing typos, failed deliveries, and Return to Origin (RTO) losses.
To solve this cleanly without maintaining your own postal database, you can pair a lightweight backend validation handler with India Pincode Data API on RapidAPI (Disclosure: This API is developed and is maintained by us).
In a single JSON POST request, it validates the PIN code and returns post office names, delivery status, district, state, postal division, circle, and latitude/longitude coordinates.
Developers building Indian e-commerce checkouts, logistics forms, or fintech onboarding screens usually hit the same four roadblocks:
110000 or 999999 pass a 6-digit numeric check even though no delivery post office exists for them.HO) and Sub Offices (PO), and can even cross administrative district boundaries. For example, 110051 maps to both SHAHDARA and EAST districts in DELHI.Non Delivery, which matters when verifying courier serviceability or prompting a buyer to confirm their exact delivery locality.The India Pincode Data API uses a POST endpoint with Content-Type: application/json and a single pincode field in the request body:
{
"pincode": "110051"
}
200 OK)When records match, the response returns the queried pincode, an array of post office objects under data, and error: null:
{
"pincode": "110051",
"data": [
{
"circleName": "Delhi Circle",
"regionName": "DivReportingCircle",
"divisionName": "Delhi East Division",
"officeName": "Krishna Nagar HO",
"pincode": "110051",
"officeType": "HO",
"delivery": "Delivery",
"district": "SHAHDARA",
"stateName": "DELHI",
"latitude": "28.6457222",
"longitude": "77.2998056"
},
{
"circleName": "Delhi Circle",
"regionName": "DivReportingCircle",
"divisionName": "Delhi East Division",
"officeName": "Govind Pura SO",
"pincode": "110051",
"officeType": "PO",
"delivery": "Non Delivery",
"district": "EAST",
"stateName": "DELHI",
"latitude": "28.6453611",
"longitude": "77.2871111"
}
],
"error": null
}
If a user sends letters or special characters (such as "abc0111"), data comes back empty and error returns "Invalid Pincode":
{
"pincode": "abc0111",
"data": [],
"error": "Invalid Pincode"
}
If a 6-digit PIN code has no matching post offices in the directory (such as "110000"), data is empty and error remains null:
{
"pincode": "110000",
"data": [],
"error": null
}
Separating error: "Invalid Pincode" from data: [] with error: null lets your frontend show an inline format error for bad input while falling back to manual state/district entry if a newly allocated PIN is missing.
Never expose your RapidAPI key inside client-side JavaScript. Instead, create a small server endpoint (Node.js, PHP, Python, or Laravel) that validates the 6-digit input, queries the API, normalizes duplicate district/state names, and caches results.
async function lookupIndiaPincode(rawPincode, { rapidApiUrl, rapidApiKey, rapidApiHost }) {
const pincode = String(rawPincode ?? "").trim();
if (!/^\d{6}$/.test(pincode)) {
return {
status: "invalid_format",
pincode,
message: "Invalid Pincode",
states: [],
districts: [],
offices: []
};
}
const response = await fetch(rapidApiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-RapidAPI-Key": rapidApiKey,
"X-RapidAPI-Host": rapidApiHost
},
body: JSON.stringify({ pincode })
});
if (!response.ok) {
throw new Error(`Pincode lookup failed with HTTP ${response.status}`);
}
const payload = await response.json();
if (payload.error) {
return {
status: "invalid_format",
pincode: payload.pincode || pincode,
message: payload.error,
states: [],
districts: [],
offices: []
};
}
const records = Array.isArray(payload.data) ? payload.data : [];
if (records.length === 0) {
return {
status: "not_found",
pincode: payload.pincode || pincode,
message: "No post offices found for this PIN code",
states: [],
districts: [],
offices: []
};
}
const states = [...new Set(records.map((item) => item.stateName).filter(Boolean))];
const districts = [...new Set(records.map((item) => item.district).filter(Boolean))];
return {
status: "ok",
pincode: payload.pincode || pincode,
message: null,
states,
districts,
offices: records
};
}
Trigger the lookup as soon as the user enters six digits, cancel outdated requests with AbortController, and populate a post office <select> dropdown alongside the auto-filled state and district inputs:
<form id="checkout-address">
<label for="pincode">PIN Code</label>
<input id="pincode" name="pincode" type="tel" inputmode="numeric" maxlength="6" required />
<small id="pincode-status" aria-live="polite"></small>
<label for="state">State</label>
<input id="state" name="state" type="text" required />
<label for="district">District</label>
<input id="district" name="district" type="text" required />
<label for="locality">Post Office / Locality</label>
<select id="locality" name="locality">
<option value="">Select post office</option>
</select>
</form>
<script>
const pincodeInput = document.getElementById("pincode");
const statusEl = document.getElementById("pincode-status");
const stateInput = document.getElementById("state");
const districtInput = document.getElementById("district");
const localitySelect = document.getElementById("locality");
let activeRequest = null;
pincodeInput.addEventListener("input", async () => {
const pincode = pincodeInput.value.trim();
if (pincode.length < 6) {
statusEl.textContent = "";
return;
}
if (activeRequest) activeRequest.abort();
activeRequest = new AbortController();
statusEl.textContent = "Checking PIN code...";
try {
const res = await fetch("/api/pincode-lookup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ pincode }),
signal: activeRequest.signal
});
const result = await res.json();
localitySelect.innerHTML = '<option value="">Select post office</option>';
if (result.status === "ok") {
stateInput.value = result.states[0] || "";
districtInput.value = result.districts[0] || "";
result.offices.forEach((office) => {
const option = document.createElement("option");
option.value = office.officeName;
option.textContent = `${office.officeName} (${office.delivery})`;
localitySelect.appendChild(option);
});
statusEl.textContent =
result.districts.length > 1
? `Multiple districts matched (${result.districts.join(", ")}). Please verify.`
: "State and district auto-filled.";
} else if (result.status === "invalid_format") {
statusEl.textContent = result.message;
} else {
statusEl.textContent = "PIN code not found. Please enter state and district manually.";
}
} catch (err) {
if (err.name !== "AbortError") {
statusEl.textContent = "Could not verify PIN code right now. Please enter address manually.";
}
}
});
</script>
status: "ok" responses in Redis or an in-memory cache for 7–30 days cuts latency to near zero for repeat metro PINs and stretches your monthly request quota.not_found lookups briefly: A 1-to-6 hour TTL stops repeated typos from exhausting external limits.To start testing or move into production without building your own postal ETL pipeline, you can grab an API key from the India Pincode Data API on RapidAPI and scale your quota tier as your checkout or store-locator traffic grows.