Deployed apps resolve a user's city / region / country - from their IP, or from lat/lon coordinates - through a first-party service endpoint. No third-party geocoder, no account, no API key.
Reach for this before pulling in BigDataCloud, Nominatim, ipapi, or any other external geocoder: this service is free, rate-limit-friendly, and keeps user coordinates off third parties.
This is the app service (deployed apps call it over HTTPS). To look up a location yourself while building, use
gipity location.
Endpoints
POST /api/<PROJECT_GUID>/services/location/ipPOST /api/<PROJECT_GUID>/services/location/geocode
Both are on the Gipity API host and take an X-App-Token header - the same auth pattern app-llm, app-image, and app-tts use. From the browser, Gipity.service('location/ip', {}) handles the host and token for you. No sign-in is needed: location is not billed.
/location/ip- body{}(the caller's IP) or{ ip: '8.8.8.8' }(a specific IP)/location/geocode- body{ lat, lon }to reverse-geocode coordinates into a place
Response shape
Both endpoints return the same unified object under data:
{
source: 'ip' | 'geocode',
city: string | null,
region: string | null,
country: string | null,
timezone: string | null,
lat: number | null,
lon: number | null,
}
Fields that can't be resolved are null. Private IPs (10.*, 192.168.*, 172.16-31.*, 127.0.0.1) return an error - there's no useful answer for those.
Whose IP gets resolved?
An IP lookup resolves whoever's IP it's given - and only the browser automatically knows the user's IP:
| Where | Call | Resolved IP |
|---|---|---|
| Browser (frontend) | /services/location/ip with {} |
The user's IP ✓ |
| Function (your backend) | location.ip() with no IP |
Nothing: it throws (the server can't see the user's IP) ✗ |
| Function (your backend) | location.ip({ ip: ctx.headers['x-forwarded-for']?.split(',')[0].trim() }) |
The user's IP ✓ |
If you just want "what city is the user from?" just have the browser call it (Pattern A) and pass the city into your function in the body. One call, no plumbing.
If you need a server-side lookup (to log or gate on it), call the injected location capability from your function and forward the user's IP from ctx.headers['x-forwarded-for']. See Pattern B.
Capability boundary: "current location" exists only where there's a browser. A workflow step and a scheduled (cron) run invoke your function with no caller IP -
ctx.headers['x-forwarded-for']is absent, andlocation.ip()with no public{ ip }throws. (Agipity testcall is the opposite: it is a real request from the test sandbox, so it DOES carry a caller IP and takes your IP-lookup branch - seeapp-testing.) A workflow therefore cannot resolve the user's current location on its own; design the feature so the browser captures the location (Pattern A ornavigator.geolocation) and stores it, and the workflow reads the stored position. Decide this before writing the workflow - it changes the data flow, not just a call site.
Verifying location headlessly? When you load an app through
gipity page inspect/page eval, a frontend/location/iplookup resolves the sandbox's egress IP, not your own - it typically reports somewhere like Boardman, Oregon (the cloud region the headless browser runs in), not the user's real city. That's expected: the only IP the browser knows in that context is the sandbox's. To verify real-user geolocation, load the app in a normal browser, or pass a known{ ip }explicitly.
Pattern A - Frontend JS (deployed web app)
// The SDK (auto-injected into every deployed page) finds this instance's API and handles the app token.
const data = await Gipity.service('location/ip', {}); // {} = caller's IP; or { ip: '8.8.8.8' }
// data = { source, city, region, country, timezone, lat, lon }
Reverse-geocode: POST /services/location/geocode with { lat, lon } - useful after a browser navigator.geolocation fix to turn coords into a city name.
Continuous tracking on mobile (run/ride/delivery/field apps)
Any phone-first app that tracks movement over time must survive a screen lock. Defaults fail: a locked screen throttles/suspends JS timers and the geolocation watch, wrecking distance/time.
- Watch, don't poll. Use
navigator.geolocation.watchPosition(onFix, onErr, { enableHighAccuracy: true, maximumAge: 0 })for live paths - one watch, not repeatedgetCurrentPosition.clearWatch(id)on stop. - Hold a Screen Wake Lock so the screen never locks. Feature-detect, re-acquire on
visibilitychange(the lock auto-releases when the tab hides), release on stop:
let lock = null;
async function acquireLock() {
if ('wakeLock' in navigator) lock = await navigator.wakeLock.request('screen');
}
document.addEventListener('visibilitychange', () => {
if (lock !== null && document.visibilityState === 'visible') acquireLock();
});
// start: await acquireLock(); stop: lock?.release(); lock = null;
- Derive elapsed/distance from fix timestamps, not timer ticks. Backgrounded tabs throttle
setInterval/setTimeout, so compute elapsed fromDate.now()deltas and distance from each fix'scoords+position.timestamp- never from counting ticks.
Handle the no-GPS path - use Gipity.geo.track(), don't hand-roll it
The trap that bites every tracker app: navigator.geolocation exists in every desktop browser, so a bare if (!navigator.geolocation) guard passes and the app "starts tracking" with no GPS behind it. A desktop returns only a coarse Wi-Fi/IP estimate (or nothing), your accuracy filter silently drops it, nothing plots, and the user stares at whatever the map defaulted to - which is why you must never seed the map with a hardcoded real-world location (a default center like [37.77, -122.41] reads as "you are here" and looks like a mislocation). Open on a neutral, zoomed-out view until you have a real fix.
The client SDK ships a headless helper that gets the gating right so you don't re-derive it. It gates on the first fix: locating until a usable fix arrives, then tracking; a first fix coarser than gpsAccuracyM (no real GPS - the desktop case), a permission denial, a timeout, or an unsupported device each end in one terminal onUnavailable({ reason, message }) with ready-to-show copy, and the watch is stopped for you. It owns no UI and no distance math - render the states and filter fixes in your app.
const handle = Gipity.geo.track({
gpsAccuracyM: 100, // first fix must be at least this accurate to count as GPS (default 100)
timeoutMs: 18000, // give up waiting for the first fix after this (default 18s)
onState: (s) => showOverlay(s === 'locating'), // 'locating' | 'tracking'
onFix: (f) => addPoint(f.lat, f.lng), // { lat, lng, accuracyM, speedKmh, heading, t }
onUnavailable: (info) => showMessage(info.message), // info.reason: 'no-gps'|'denied'|'timeout'|'unavailable'|'unsupported'
});
// stop on finish/unmount: handle.stop();
Gipity.geo.supported() returns whether the API is even present. Hand-roll watchPosition only if you need something the helper doesn't cover - and if you do, still apply the three rules above (gate on the first fix, treat a coarse-only fix as no-GPS, never fake a default location).
Pattern B - Serverless function (your own backend function)
location is injected as a capability on the function's second argument - location.ip({ ip }) and location.geocode({ lat, lon }). The call runs server-to-server in-process: no token, no fetch, no a.gipity.ai (which the sandbox blocks anyway), no project GUID to stash. Because the server can't see the user's IP, pass it explicitly from ctx.headers['x-forwarded-for']:
export default async function myFn(ctx, { location }) {
const userIp = ctx.headers['x-forwarded-for']?.split(',')[0].trim();
const geo = await location.ip({ ip: userIp }); // omit { ip } and the call throws
return { city: geo.city, country: geo.country };
}
Required gipity.yaml permissions (see deploy):
# inside the type: functions phase in gipity.yaml
functions:
- name: myFn
auth: public
services: ['location'] # required - without it, location.* throws. No fetch_domains needed.
Reverse-geocode from a function the same way:
await location.geocode({ lat, lon }).locationis not credit-billed; other app services (llm/image/...) needowner_paysto run from a function - see app-function-services.
Common mistakes to avoid
- From a function, don't
fetcha.gipity.aito reach location - the sandbox blocks it (fetch_domains: ['a.gipity.ai']won't help). Use the injectedlocation.ip()/location.geocode()capability and declareservices: ['location']. - Do not use relative URLs from the browser (
/api/token,/services/location/ip) - they hit the app CDN host, not the API. UseGipity.service, or read the API base off the SDK tag (document.querySelector('script[data-api-base]').dataset.apiBase); never hardcodea.gipity.ai, which is correct only on prod. - Calling
location.ip()from a function with no{ ip }throws: the server can't see the user's IP. Forwardctx.headers['x-forwarded-for']. - Expecting a workflow/scheduled run to know "where the user is now" - those contexts carry no caller IP and no browser, so there is nothing to resolve. Capture location browser-side, store it, and have the workflow read the stored value (see the capability-boundary note above).
Related skills
- app-development - writing functions and
gipity.yaml - app-function-services -
fetchpermissions and the injected app services