Show events near a user
Show a user what's happening around them. The browser asks for the user's location and sends it to your server; your server calls Events by Geo Coordinates with your key and returns the events.
You need an API key and a server that can run JavaScript (Node.js 18 or later). The key stays on the server: the browser only talks to your own route.
1. Try the API
latitude, longitude and radius (whole kilometres, up to 100) are required. Coordinates can't be 0.
curl "$AE_BASE_URL/events/geo?latitude=30.2672&longitude=-97.7431&radius=10&rows=10" \ -H "X-API-Key: $AE_API_KEY"2. Add a server route
This handler uses the standard Request and Response objects, so it works as a Next.js route handler (app/api/nearby/route.js) and in most other JavaScript servers.
// app/api/nearby/route.js (server-side only)const BASE = process.env.AE_BASE_URL; // https://developer.allevents.in/v1const KEY = process.env.AE_API_KEY;// Rounding to 2 decimals (about 1 km) makes nearby users send the same call,// which is then answered from the cache at 0 credits.const round = (v) => Math.round(Number(v) * 100) / 100;export async function GET(request) { const url = new URL(request.url); const lat = round(url.searchParams.get("lat")); const lng = round(url.searchParams.get("lng")); if (!Number.isFinite(lat) || !Number.isFinite(lng) || Math.abs(lat) > 90 || Math.abs(lng) > 180 || (lat === 0 && lng === 0)) { return Response.json({ error: "Send lat and lng" }, { status: 400 }); } const params = new URLSearchParams({ latitude: String(lat), longitude: String(lng), radius: "10", rows: "10" }); const response = await fetch(`${BASE}/events/geo?${params}`, { headers: { "X-API-Key": KEY }, signal: AbortSignal.timeout(45_000), }); if (response.status === 429) { return Response.json({ error: "Busy, try again soon" }, { status: 503, headers: { "retry-after": response.headers.get("retry-after") ?? "60" } }); } const body = await response.json(); // 200 with error > 0 means nothing was found nearby, or the point is outside your coverage (0 credits). const events = response.ok && body.error === 0 ? body.data : []; return Response.json( events.map((e) => ({ id: e.event_id, name: e.eventname, when: e.display.short, starts: e.start_at, venue: e.venue.name, url: e.event_url })), { headers: { "cache-control": "private, max-age=300" } }, );}Only the fields your page needs go back to the browser, never the key or the raw API response headers.
3. Call it from the browser
// In your page's script. No API key here: it calls your own route.const list = document.querySelector("#nearby");navigator.geolocation.getCurrentPosition( async ({ coords }) => { const response = await fetch(`/api/nearby?lat=${coords.latitude}&lng=${coords.longitude}`); const events = response.ok ? await response.json() : []; list.replaceChildren( ...events.map((e) => { const item = document.createElement("li"); const link = Object.assign(document.createElement("a"), { href: e.url, textContent: e.name }); item.append(link, ` · ${e.when} · ${e.venue ?? ""}`); return item; }), ); if (events.length === 0) list.textContent = "No events found nearby."; }, () => (list.textContent = "Allow location access to see events near you, or search by city."),);when is display.short, ready-made text in the venue's time, so it's shown as is. starts (start_at) is ISO 8601 with the venue's offset, for sorting or your own formatting; venue.name can be null (see Dates and times).
Things to know
- Ask for the location only after the user does something, such as pressing "Events near me". If they say no, fall back to a city search with City Suggestions.
- With limited coverage, results outside it are left out and a page can be short or empty; a point outside your coverage answers
error: 1(This location is not in your coverage.) at once. See Coverage. - At most 10 events come back per call (
rowsup to 10); usepagefor more. - Show "Data from AllEvents" with a link to allevents.in wherever the events are public (see the API Terms of Use in the developer portal).