A simple REST API server that generates H3 hexagon indices using the h3-js library.
npm installStart the server:
node server.jsThe server will run on port 3000 by default (or the PORT environment variable).
Returns API documentation and available endpoints.
Generate an H3 hex from latitude, longitude, and resolution using query parameters.
Parameters:
lat(required): Latitude (-90 to 90)lon(required): Longitude (-180 to 180)resolution(required): H3 resolution level (0 to 15)
Example:
GET /h3?lat=37.7749&lon=-122.4194&resolution=9
Response:
{
"input": {
"latitude": 37.7749,
"longitude": -122.4194,
"resolution": 9
},
"h3Index": "8928308280fffff",
"hexCenter": {
"lat": 37.77493584772029,
"lng": -122.41938644651138
},
"boundary": [
{"lat": 37.77805318, "lng": -122.41613896},
{"lat": 37.77715130, "lng": -122.41254478},
{"lat": 37.77181851, "lng": -122.41263392},
{"lat": 37.77181851, "lng": -122.42263392},
{"lat": 37.77715130, "lng": -122.42622810},
{"lat": 37.77805318, "lng": -122.41973380}
],
"isPentagon": false,
"metadata": {
"baseCellNumber": 20,
"isResClassIII": true
}
}Generate an H3 hex from latitude, longitude, and resolution using JSON body.
Body:
{
"lat": 37.7749,
"lon": -122.4194,
"resolution": 9
}Response: Same as GET /h3
Get the neighbors of an H3 hex within k distance.
Parameters:
h3Index(path): Valid H3 indexk(query, optional): Distance (default: 1, max: 10)
Example:
GET /h3/8928308280fffff/neighbors?k=1
Response:
{
"center": "8928308280fffff",
"k": 1,
"neighbors": [
"8928308280fffff",
"8928308280bffff",
"89283082807ffff",
"89283082877ffff",
"89283082803ffff",
"89283082873ffff",
"8928308283bffff"
],
"count": 7
}- 0-2: Country/continent level
- 3-5: State/province level
- 6-8: City level
- 9-11: Neighborhood level
- 12-15: Building/block level
Higher resolution numbers create smaller hexagons with more precision.
All error responses follow this format:
{
"error": "Error type",
"message": "Detailed error message"
}Common HTTP status codes:
400: Bad Request (invalid parameters)404: Not Found (invalid endpoint)500: Internal Server Error
# GET request
curl "http://localhost:3000/h3?lat=37.7749&lon=-122.4194&resolution=9"
# POST request
curl -X POST http://localhost:3000/h3 \
-H "Content-Type: application/json" \
-d '{"lat": 37.7749, "lon": -122.4194, "resolution": 9}'
# Get neighbors
curl "http://localhost:3000/h3/8928308280fffff/neighbors?k=2"// GET request
const response = await fetch('/h3?lat=37.7749&lon=-122.4194&resolution=9');
const data = await response.json();
// POST request
const response = await fetch('/h3', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
lat: 37.7749,
lon: -122.4194,
resolution: 9
})
});
const data = await response.json();