Build an Indian address form with a free PIN Code API
Auto-fill district and state the moment a user types their 6-digit pincode โ no paid service, no API key.
By the apibharat.com team ยท
The problem
Asking users to type their district and state by hand is slow and error-prone โ people misspell districts, pick the wrong state, or leave fields blank. In India the 6-digit PIN code already encodes the post office, district and state, so you can auto-fill those the moment the user finishes typing the pincode.
The endpoint
Use the free PIN Code API. No key, no signup:
GET https://api.apibharat.com/v1/pincode/{pincode}Response is JSON (application/json). A valid pincode is 6 digits and cannot start with 0.
A real request
curl https://api.apibharat.com/v1/pincode/560001{
"success": true,
"data": {
"pincode": "560001",
"district": "Bengaluru",
"state": "Karnataka",
"post_offices": [
{ "office_name": "Bangalore GPO", "region": "Bengaluru", "circle": "Karnataka" }
]
},
"message": "PIN code details found"
}Wiring it into a form
Listen for a complete 6-digit pincode, validate the format client-side first, then call the API and fill the district and state:
<input id="pincode" maxlength="6" inputmode="numeric" placeholder="Pincode">
<input id="district" placeholder="District" readonly>
<input id="state" placeholder="State" readonly>
<script>
const pin = document.getElementById("pincode");
pin.addEventListener("input", async () => {
const v = pin.value.trim();
// Validate before spending a request.
if (!/^[1-9][0-9]{5}$/.test(v)) return;
const res = await fetch(`https://api.apibharat.com/v1/pincode/${v}`);
const json = await res.json();
if (!json.success) {
// 404 = not found, 422 = bad format
document.getElementById("district").value = "";
document.getElementById("state").value = "";
return;
}
document.getElementById("district").value = json.data.district;
document.getElementById("state").value = json.data.state;
});
</script>Validation & errors
- Format: check
/^[1-9][0-9]{5}$/before calling โ it saves a round trip and gives instant feedback. - 422 Unprocessable: the pincode isn't 6 digits. Show an inline "enter a valid 6-digit pincode" message.
- 404 Not found: a well-formed pincode with no record. Let the user type district/state manually as a fallback.
- One pincode can map to several post offices โ the
post_officesarray lists them all if you want a locality dropdown.
Try it
Prefer to test without code? Use the PIN Code Finder (it also supports reverse search by area name), or read the full Pincode API reference.