JavaScript tutorial: fuel station prices near a user
This example builds a small Node.js function that returns the cheapest nearby stations for a given position — the core of a fuel-finder feature.
Install
npm install apify-client
Function
import { ApifyClient } from 'apify-client';
const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
export async function cheapestNearby({ lat, lon, fuel = 'diesel', radiusKm = 5, limit = 10 }) {
const run = await client.actor('sste/official-fuel-prices').call({
latitude: lat, longitude: lon, radiusKm, fuelTypes: [fuel], maxResults: limit,
});
const { items } = await client.dataset(run.defaultDatasetId).listItems();
return items.map((r) => ({
price: r.price, currency: r.currency, unit: r.unit,
name: r.stationName ?? r.brand ?? r.address,
address: r.address, distanceKm: r.distanceKm, updated: r.priceUpdatedAt,
}));
}
console.table(await cheapestNearby({ lat: 45.4642, lon: 9.19 }));
Notes
- Do not call the API from the browser with your token; call it from your server or a serverless function.
- Cache results for a few minutes: France updates about every 10 minutes, Spain every 30, Italy and WA daily.