0

Retrieve detailed geolocation information for a visitor’s or user’s IP address

by
Published Apr 8, 2025

This API allows you to retrieve detailed geolocation information for a visitor’s or user’s IP address. By using the IP Geolocation method, you can access data such as the user’s country, city, ISP, and more, enabling you to personalize user experiences, enhance security, and make data-driven decisions based on geographic insights.

Script greip Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Greip = {
3
  apiKey: string;
4
};
5
/**
6
 * Retrieve detailed geolocation information for a visitor’s or user’s IP address
7
 * This API allows you to retrieve detailed geolocation information for a visitor’s or user’s IP address.
8

9
By using the IP Geolocation method, you can access data such as the user’s country, city, ISP, and more, enabling you to personalize user experiences, enhance security, and make data-driven decisions based on geographic insights.
10
 */
11
export async function main(
12
  auth: Greip,
13
  params: string | undefined,
14
  lang: string | undefined,
15
  format: string | undefined,
16
  userID: string | undefined,
17
  mode: string | undefined,
18
  callback: string | undefined,
19
) {
20
  const url = new URL(`https://greipapi.com/geoip`);
21
  for (const [k, v] of [
22
    ["params", params],
23
    ["lang", lang],
24
    ["format", format],
25
    ["userID", userID],
26
    ["mode", mode],
27
    ["callback", callback],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      Authorization: "Bearer " + auth.apiKey,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.text();
45
}
46