Skip to main content

Overview

The geolocation API provides functions to automatically detect a user’s location or resolve city/country queries. These functions are useful for providing location-based defaults in your application.

Auto-detect Location

guessLocation

Automatically detect the user’s location based on their IP address.
Promise<GeoLocation | null>
Location data or null if detection fails

How It Works

The guessLocation function tries multiple free IP geolocation services in sequence:
  1. ip-api.com - First attempt
  2. ipapi.co - Fallback if first fails
  3. ipwho.is - Final fallback
This redundancy ensures high reliability even if one service is down.
IP-based geolocation may not be accurate for users behind VPNs or proxies.

Resolve City Query

guessCityCountry

Resolve a city name or query string to get coordinates and timezone.
string
required
City name or search query (e.g., “London”, “New York”, “Tokyo”)
Promise<CityCountryGuess | null>
City data or null if not found

Geocoding Service

This function uses the Open-Meteo Geocoding API to resolve city queries. The API:
  • Returns the first (most relevant) match for ambiguous queries
  • Supports international city names
  • Provides accurate coordinates and timezone data
  • Is free and requires no API key

Usage Examples

Auto-detect and Save Location

Search for City

Fallback Chain

Validate User Input

Get Timezone for Prayer Times

Integration with Prayer Times API

Error Handling

Both functions return null on failure rather than throwing errors. Always check the return value:

TypeScript Types

Privacy Considerations

IP geolocation APIs typically collect:
  • User’s IP address
  • Request timestamp
  • User agent information
No personal data is sent by ramadan-cli. Users behind VPNs or proxies will see the VPN/proxy location.

Performance

  • guessLocation: ~500ms (tries up to 3 services)
  • guessCityCountry: ~200ms (single API call)
Consider caching results to avoid repeated lookups: