Home / JavaScript integration guide
JavaScript integration guide
Call the LatLongAPI from JavaScript with a JSON POST request, server-side license configuration, and HTTP error handling.
Configure your application
Use Node.js 18 or later with built-in fetch. Save the example as example.mjs.
In a Bash or compatible shell, set LATLONG_API_BASE_URL to your assigned HTTPS origin and LATLONG_API_LICENSE to your license. For testing, use the test origin below with a test license. Replace YOUR_TEST_LICENSE with the license issued for that environment. Keep the license outside source control and browser code.
export LATLONG_API_BASE_URL="https://v1-dev.latlongapi.com"
export LATLONG_API_LICENSE="YOUR_TEST_LICENSE"Request an address
// Node.js 18+; run on your server, keeping the license private.
const base = process.env.LATLONG_API_BASE_URL;
const api = process.env.LATLONG_API_LICENSE;
if (!base || !api) throw new Error('Set API base URL and license');
const response = await fetch(new URL('/api/v1/reverse', base), {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({lat: 51.5007, long: -0.1246, api}),
signal: AbortSignal.timeout(10000)
});
if (!response.ok) throw new Error('HTTP ' + response.status);
const data = await response.json();
console.log(data.result.short_address);Run the example
node example.mjsUse the result
The example prints result.short_address. Read result.long_address for a longer description. Check nullable city and country fields before displaying them.
Handle failures in your service
Set a timeout, catch request errors, and record the status and request identifier without logging the license. Retry temporary failures with bounded backoff. A 401 requires a valid license; a 422 requires correcting the input.