0

Get the full information of a country

by
Published Apr 8, 2025

This method allows you to retrieve detailed information about a country by providing its country code in the request. The API returns valuable data, such as the country’s name, capital, population, region, and other relevant attributes, enabling you to enrich your applications with geographic context and enhance user experiences.

Script greip Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Greip = {
3
  apiKey: string;
4
};
5
/**
6
 * Get the full information of a country
7
 * This method allows you to retrieve detailed information about a country by providing its country code in the request.
8

9
The API returns valuable data, such as the country’s name, capital, population, region, and other relevant attributes, enabling you to enrich your applications with geographic context and enhance user experiences.
10
 */
11
export async function main(
12
  auth: Greip,
13
  CountryCode: string,
14
  params: string | undefined,
15
  format: string | undefined,
16
  mode: string | undefined,
17
  callback: string | undefined,
18
) {
19
  const url = new URL(`https://greipapi.com/lookup/country`);
20
  for (const [k, v] of [
21
    ["CountryCode", CountryCode],
22
    ["params", params],
23
    ["format", format],
24
    ["mode", mode],
25
    ["callback", callback],
26
  ]) {
27
    if (v !== undefined && v !== "" && k !== undefined) {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + auth.apiKey,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.text();
43
}
44