SpotCode for developers

Every SpotCode address comes from one open-source package. Install it from npm and convert addresses in your own app: no API key, no network calls, no usage limits.

npmGitHubMIT licenseTypeScript types included

Install

npm install @alvarosilva/hex-address

Also works with pnpm add, yarn add and bun add. It runs in browsers and in Node.js 16 or later, and ships both ES modules and CommonJS. Its only dependency is h3-js.

Coordinates to address, and back

TypeScript / JavaScript
import { coordinateToAddress, addressToCoordinate } from '@alvarosilva/hex-address';

// Eiffel Tower
const address = coordinateToAddress(48.8584, 2.2945);
// 'dofekohepatetupi'

const [lat, lng] = addressToCoordinate(address);
// [48.858388, 2.294495]  (the centre of the ~3 m cell)

An address is the cell, not the exact point: converting it back gives the centre of a hexagon about 3 metres across. These are the same addresses the SpotCode map shows.

Check an address before using it

import { isValidAddress } from '@alvarosilva/hex-address';

isValidAddress('dofekohepatetupi'); // true
isValidAddress('hello');            // false

// Pass the config name and true for the reason and suggestions
const result = isValidAddress('dofekohepatetuxa', 'ascii-elomr', true);
// result.isValid             → false
// result.errors[0].message   → 'Invalid syllable "xa" at position 8. Invalid consonant "x"'
// result.errors[0].suggestions → ['da', 'fa', 'ha']

Use the detailed form when people type addresses: it says which syllable is wrong and suggests the closest valid ones.

Distances and partial addresses

import { calculateDistance, estimateLocationFromPartial } from '@alvarosilva/hex-address';

// Eiffel Tower → Louvre
calculateDistance('dofekohepatetupi', 'dofekoherufanesi'); // 3.16 (km)

// Only the first 4 syllables: an area of about 500 km² around Paris
const area = estimateLocationFromPartial('dofekohe');
// area.centerCoordinate, area.bounds, area.estimatedAreaKm2

Because nearby places share the start of their address, the first syllables alone already narrow down the area. How the grid works explains why.

Main functions

coordinateToAddress(lat, lng)Coordinates to an 8-syllable address.
addressToCoordinate(address)An address to the [lat, lng] of its cell's centre.
isValidAddress(address)Whether the address is a real place. Add 'ascii-elomr', true for errors and suggestions.
calculateDistance(a, b)Distance between two addresses, in km.
findNearbyAddresses(address, radiusKm)Addresses around a point, with their distance and coordinates.
getAddressBounds(address)The north, south, east and west edges of an address's cell.
estimateLocationFromPartial(prefix)The area covered by the first few syllables of an address.
clusterAddresses(addresses, maxKm)Groups addresses that are close to each other.

The full reference, with every option and type, is in the README on GitHub.

Building something bigger?

For help with an integration or a managed setup, write to alvaro@sanluca.cc.