---
title: getGeolocationForIpAddress
summary: Retrieve geolocation information about the given IP address.
url: https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/geolocation/fn.getGeolocationForIpAddress/
---

**Kind:** Function  
**Module:** [geolocation](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/geolocation/)  
**Package:** @fastly/js-compute 4.0.1  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/geolocation.d.ts#L35

```javascript
getGeolocationForIpAddress(address: string): Geolocation | null
```

Retrieve geolocation information about the given IP address.

**Note**: Can only be used when processing requests, not during build-time
initialization.

**Parameters**

- `address`: `string` — The IPv4 or IPv6 address to query.

**Returns**

`Geolocation | null`

A [Geolocation](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/geolocation/interface.Geolocation/) object, or `null` if no geolocation data is
available for the address.

**Throws**

- {Error} If `address` is not a valid IPv4 or IPv6 address.

```js
import { getGeolocationForIpAddress } from "fastly:geolocation";

async function app(event) {
  const ip =
    new URL(event.request.url).searchParams.get("ip") ||
    event.client.address;
  const geo = getGeolocationForIpAddress(ip);
  return new Response(JSON.stringify(geo), {
    headers: { "Content-Type": "application/json" },
  });
}

addEventListener("fetch", (event) => event.respondWith(app(event)));
```
