DEV Community

Rajat Gupta
Rajat Gupta

Posted on

Introducing digipinjs: India’s Digital Postal Index Number (DIGIPIN) Library

India’s Department of Posts (Govt of India) has launched DIGIPIN – an open-source, nationwide geo-coded addressing system. DIGIPIN assigns a unique 10-character alphanumeric code to every ~4×4 m grid in India. In other words, each small geographic cell gets its own “digital PIN”, derived from its latitude/longitude. Unlike traditional 6-digit PIN codes that cover broad areas, a DIGIPIN code pinpoints the exact location of a property. For example, DIGIPIN codes are precise down to ~4 m squares, making them ideal for e-commerce deliveries, emergency services, navigation, and any application needing accurate geolocation.

Why DIGIPIN Matters

DIGIPIN bridges India’s physical addresses and their digital representation. It is designed to improve last-mile delivery and address management. Key benefits include:

  • Exact location accuracy: Each DIGIPIN corresponds to a ~4 × 4 m grid, ensuring precise positioning (much finer than area-based PINs).
  • Streamlined services: By encoding precise coordinates, DIGIPIN helps logistics and delivery companies avoid errors. Online shoppers and delivery platforms (e.g. Amazon, Flipkart) can use DIGIPIN codes to ensure quick, accurate deliveries.
  • Emergency response: Ambulances, police, and firefighters can use DIGIPIN to reach exact sites faster, reducing response times.
  • Inclusivity & Standardization: DIGIPIN works everywhere in India (urban or remote) and provides an extra Address-as-a-Service layer, helping areas with unstructured or no traditional addresses.
  • Privacy: A DIGIPIN encodes only location – no personal data is stored or required.

In short, DIGIPIN supports modern services and a digital transformation of India’s address system. It is open-source and interoperable, enabling easy integration with web, mobile, GIS and emergency-response systems.

Overview of digipinjs

digipinjs is a comprehensive TypeScript library for the DIGIPIN system. It provides developers with simple functions and tools to convert between geographic coordinates (latitude/longitude) and DIGIPIN codes. Core capabilities include: encoding lat/lng to DIGIPIN and decoding a DIGIPIN back to coordinates. It also includes validation (ensuring coordinates are within India’s bounds) and clear error handling when inputs are invalid.

Key highlights of the digipinjs library:

  • Official Geocoding Support: Implements the government-defined DIGIPIN algorithm, covering India’s latitudes (2.5°N–38.5°N) and longitudes (63.5°E–99.5°E). You can trust it to follow the official 10-char code format and character set.
  • Full TypeScript Support: Written in TypeScript with complete type definitions, making it safe and easy to use in modern JS/TS projects.
  • Lightweight & Fast: Optimized for performance with minimal overhead. The underlying code is similar to the official Node.js API and is designed for speed and low memory (using an LRU cache for repeated lookups).
  • CLI Tool: A command-line interface (digipin-cli) lets you quickly encode/decode coordinates without writing code. Useful for one-off conversions or scripting.
  • Batch Processing & Caching: Process lists of locations efficiently and cache recent results to speed up repeated queries.
  • Express Middleware: Built-in middleware for Express.js can automatically add DIGIPIN info to HTTP responses or requests, enabling seamless integration in web apps and APIs.
  • Grid Generation & Reverse Geocoding: Functions to generate offline DIGIPIN grids or reverse-geocode (convert DIGIPIN → lat/lng) are included.
  • Cross-Sector Ready: By adopting DIGIPIN via this library, your application aligns with a cross-sector digital address standard used in e-commerce, logistics, BFSI (banking/finance), governance, and more.

Installation

Install via your favorite package manager:

  • npm:
  npm install digipinjs
Enter fullscreen mode Exit fullscreen mode
  • yarn:
  yarn add digipinjs
Enter fullscreen mode Exit fullscreen mode
  • pnpm:
  pnpm add digipinjs
Enter fullscreen mode Exit fullscreen mode

Getting Started

Using the CLI

After installation, the CLI tool makes it easy to encode or decode locations from the command line:

# Encode coordinates (latitude, longitude) to DIGIPIN
npx digipin-cli encode --lat 28.6139 --lng 77.2090
# Example output: 39J-438-TJC7

# Decode a DIGIPIN back to coordinates
npx digipin-cli decode --pin 39J-438-TJC7
# Output: Latitude: 28.613901°, Longitude: 77.208998°
Enter fullscreen mode Exit fullscreen mode

You can also enable verbose mode or change output format (e.g. DMS coordinates) via additional flags.

Using the Library in Code

Import the library functions and use them in your TypeScript or JavaScript code:

import { getDigiPin, getLatLngFromDigiPin } from 'digipinjs';

// Encode coordinates to DIGIPIN
const delhiPin = getDigiPin(28.6139, 77.2090);
console.log(`Delhi DIGIPIN: ${delhiPin}`); // e.g. "39J-438-TJC7"

// Decode DIGIPIN to coordinates
const coords = getLatLngFromDigiPin('39J-438-TJC7');
console.log(coords); // { latitude: 28.613901, longitude: 77.208998 }
Enter fullscreen mode Exit fullscreen mode

For batch conversions:

import { batchEncode, batchDecode } from 'digipinjs';

const locations = [
  { lat: 19.0760, lng: 72.8777 },  // Mumbai
  { lat: 12.9716, lng: 77.5946 },  // Bengaluru
];
const pins = batchEncode(locations);
console.log(pins); 
// e.g. ["4FK-595-8823", "2L7-3K9-8P2F"]
Enter fullscreen mode Exit fullscreen mode

Integrating with Express is straightforward:

import express from 'express';
import { digiPinMiddleware } from 'digipinjs';

const app = express();
app.use(digiPinMiddleware());
// The middleware adds an `X-DIGIPIN` header (or similar) based on request coordinates

// ... define routes ...
Enter fullscreen mode Exit fullscreen mode

The library also exports helper functions like getCached/setCached (LRU caching), reverseGeocode, etc., giving you full programmatic control.

Example Use Cases

Developers and businesses in many domains can benefit from digipinjs and DIGIPIN:

  • E-commerce & Delivery: Encode customer or warehouse locations to DIGIPIN to improve delivery routing and tracking. For example, an online store can store DIGIPIN codes for addresses to reduce misdeliveries.
  • Real Estate / Maps: Store a property’s DIGIPIN to give a precise location code for listings or map services.
  • Emergency Services: Enable 911/112 services to quickly locate callers via DIGIPIN. First responders can use the code to find victims faster.
  • Government & NGOs: Use DIGIPIN in census, social services, and disaster relief to identify precise locations even in rural or informal settlements.
  • Banking & KYC: Banks and insurers can attach DIGIPIN to customer records for precise address verification in KYC processes.
  • Smart Cities / GIS: Integrate into mapping systems or smart-city dashboards as a standard location identifier.

In essence, digipinjs makes it easy to adopt DIGIPIN anywhere: from personal scripting to large-scale enterprise applications. By encoding lat/long to a compact, human-friendly code (e.g. FC9-8J3-27K4), it simplifies address management and aligns with India’s national geospatial initiatives.

Getting Help & Contributing

digipinjs is open-source. The core algorithm and reference code are public domain (originally from CEPT-VZG/India Post) and the library is MIT-licensed. You can view the source on GitHub, report issues, or contribute improvements.

  • Repository: github.com/rajatguptaa/digipinjs
  • Issues & Discussions: Use GitHub Issues/Discussions for questions or feature requests.
  • License: MIT for the wrapper; the underlying DIGIPIN code is Apache 2.0 (per India Post’s open-source release).

Conclusion

With digipinjs, any JavaScript or TypeScript project can leverage India’s official DIGIPIN system. It provides a full toolset (code library, CLI, middleware) to encode/decode precise digital addresses easily. By using digipinjs, developers align with India Post’s vision of “Address as a Service” and help build a more efficient, error-free delivery and emergency response network.

Whether you’re a logistics developer, data scientist, or hobbyist building a mapping app, digipinjs streamlines working with India’s geographic addressing. Try it out to add DIGIPIN support to your projects and join the move toward precise, digital-first address systems in India.

References: Government of India (India Post) and news media on DIGIPIN.

Top comments (0)