X Enterprises

fastify-xgeocode

Fastify plugin for Geocodio API integration — address geocoding, reverse geocoding, distance calculation, and batch operations.

fastify-xgeocode

Fastify plugin for Geocodio API integration. Decorates the server with fastify.xGeocode providing address geocoding, reverse geocoding, distance calculation (Haversine formula), batch operations, and address validation.

Installation

npm install @xenterprises/fastify-xgeocode

Quick Start

import Fastify from "fastify";
import xGeocode from "@xenterprises/fastify-xgeocode";

const fastify = Fastify();

await fastify.register(xGeocode, {
  apiKey: process.env.GEOCODIO_API_KEY,
});

fastify.get("/geo/zip/:zip", async (request) => {
  return fastify.xGeocode.getLatLongByZip(request.params.zip);
});

await fastify.listen({ port: 3000 });

Options

NameTypeDefaultRequiredDescription
apiKeystringYesGeocodio API key
fieldsstring"cd,stateleg"NoComma-separated Geocodio field names appended to every request
activebooleantrueNoSet to false to skip plugin registration entirely

Methods

Error Reference

ErrorCause
[xGeocode] apiKey is required in optionsMissing apiKey at registration
[xGeocode] apiKey must be a stringapiKey is not a string
[xGeocode] fields must be a stringfields option is not a string
[xGeocode] Invalid input - zipCode must be a non-empty stringnull, non-string, or empty zip
[xGeocode] Invalid zip code formatZip not matching NNNNN or NNNNN-NNNN
[xGeocode] Invalid input - address must be a non-empty stringnull, non-string, or empty address
[xGeocode] Invalid address - minimum 3 characters requiredAddress shorter than 3 chars
[xGeocode] Invalid coordinatesNon-numeric lat/lng
[xGeocode] Invalid latitudeLatitude outside [-90, 90]
[xGeocode] Invalid longitudeLongitude outside [-180, 180]
[xGeocode] No results foundGeocodio returned zero results
[xGeocode] Geocoding API returned {status}Non-200 HTTP response from Geocodio
[xGeocode] locations must be an arraybatchGeocode given non-array
[xGeocode] batch size cannot exceed 100 locationsArray longer than 100

Environment Variables

VariableRequiredDescription
GEOCODIO_API_KEYYes (by convention)Geocodio API key — pass via apiKey option

How It Works

On registration the plugin validates apiKey and fields, then decorates the server with fastify.xGeocode. All async methods call the Geocodio REST API (api.geocod.io/v1.7/geocode for forward, .../reverse for reverse); inputs are validated locally before the network call so callers receive descriptive errors immediately. batchGeocode runs all requests concurrently with Promise.all and returns per-item error objects rather than rejecting the whole batch. getDistance is entirely local — it uses the Haversine formula and makes no API call.

AI Context

package: "@xenterprises/fastify-xgeocode"
type: fastify-plugin
use-when: Address geocoding, reverse geocoding, distance calculation, and address validation via Geocodio API
decorator: fastify.xGeocode
methods: getLatLongByZip, getLatLongByAddress, getReverseGeocode, getDistance (local Haversine), batchGeocode (up to 100), validateAddress
env: GEOCODIO_API_KEY (pass via apiKey option)
Copyright © 2026