Need to encode geographic coordinates or find nearby locations efficiently? This guide covers everything you need to know about geohash encoding and decoding via API, including precision levels, use cases, and implementation examples in multiple languages.
What is a Geohash?
A geohash is a compact string representation of a geographic location. It encodes latitude and longitude into a short alphanumeric string, where nearby locations share similar prefixes. This makes geohashes ideal for proximity searches and spatial indexing.
For example, the coordinates (40.7128, -74.0060) for New York City encode to geohash dr5ru7.
Geohash Precision Levels
Geohash length determines precision:
Short Geohashes (1-4 characters)
1 char: ~5,000km, 2 chars: ~1,250km, 3 chars: ~156km, 4 chars: ~39km. Good for regional searches.
Medium Geohashes (5-7 characters)
5 chars: ~4.9km, 6 chars: ~1.2km, 7 chars: ~153m. Ideal for city-level and neighborhood searches.
Long Geohashes (8+ characters)
8 chars: ~38m, 9 chars: ~4.8m, 10+ chars: sub-meter precision. Used for precise location matching.
Using the Geohash API
TinyFn provides endpoints for geohash encoding and decoding:
GET https://api.tinyfn.io/v1/geo/geohash/encode?lat=40.7128&lng=-74.0060&precision=6
Headers: X-API-Key: your-api-key
{
"latitude": 40.7128,
"longitude": -74.006,
"geohash": "dr5ru7",
"precision": 6,
"bounds": {
"minLat": 40.7098388671875,
"maxLat": 40.71533203125,
"minLng": -74.00634765625,
"maxLng": -73.99536132813
}
}
GET https://api.tinyfn.io/v1/geo/geohash/decode?geohash=dr5ru7
Headers: X-API-Key: your-api-key
Parameters
| Parameter | Type | Description |
|---|---|---|
lat |
number | Latitude (-90 to 90) for encoding |
lng |
number | Longitude (-180 to 180) for encoding |
precision |
integer | Geohash length 1-12 (default: 6) |
geohash |
string | Geohash string for decoding |
Code Examples
JavaScript / Node.js
// Encode coordinates to geohash
const encodeResponse = await fetch(
'https://api.tinyfn.io/v1/geo/geohash/encode?lat=40.7128&lng=-74.0060&precision=6',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const { geohash } = await encodeResponse.json();
console.log(geohash); // dr5ru7
// Decode geohash to coordinates
const decodeResponse = await fetch(
'https://api.tinyfn.io/v1/geo/geohash/decode?geohash=dr5ru7',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const { latitude, longitude } = await decodeResponse.json();
console.log(`${latitude}, ${longitude}`); // 40.7128, -74.0060
Python
import requests
# Encode coordinates
response = requests.get(
'https://api.tinyfn.io/v1/geo/geohash/encode',
params={'lat': 40.7128, 'lng': -74.0060, 'precision': 6},
headers={'X-API-Key': 'your-api-key'}
)
geohash = response.json()['geohash']
print(geohash) # dr5ru7
# Decode geohash
response = requests.get(
'https://api.tinyfn.io/v1/geo/geohash/decode',
params={'geohash': 'dr5ru7'},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(f"{data['latitude']}, {data['longitude']}") # 40.7128, -74.0060
cURL
# Encode coordinates
curl "https://api.tinyfn.io/v1/geo/geohash/encode?lat=40.7128&lng=-74.0060&precision=6" \
-H "X-API-Key: your-api-key"
# Decode geohash
curl "https://api.tinyfn.io/v1/geo/geohash/decode?geohash=dr5ru7" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Proximity Search: Find nearby users, stores, or points of interest efficiently
- Database Indexing: Index locations for fast spatial queries
- Caching: Cache location-based data using geohash keys
- Privacy: Share approximate location without revealing exact coordinates
- URL Shortening: Encode locations in short, shareable strings
Best Practices
- Choose appropriate precision: Balance accuracy against storage and performance needs
- Handle edge cases: Geohash boundaries can split nearby points; search adjacent cells
- Use for filtering, not sorting: Geohash proximity is approximate; calculate exact distances for final ordering
- Consider neighbors: Query neighboring geohash cells for complete proximity searches
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-geo": {
"url": "https://api.tinyfn.io/mcp/geo/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all geolocation tools available via MCP in our Geolocation MCP Tools for AI Agents guide.