Guides
Gold prices in a Flutter app
Read gold prices by karat in Dart, keep them a few minutes, and show when they were updated.
GET /v1/gold returns every karat per gram in EGP, the gold pound, and the ounce in dollars. It works on the Free plan.
Read the prices
Dart
import 'dart:convert';
import 'package:http/http.dart' as http;
class GoldPrice {
final String karat;
final double buy, sell;
GoldPrice(this.karat, this.buy, this.sell);
}
DateTime? _at;
List<GoldPrice> _cache = [];
Future<List<GoldPrice>> goldPrices(String key) async {
// Prices change a few times an hour, so keep them 5 minutes.
if (_at != null && DateTime.now().difference(_at!).inMinutes < 5) return _cache;
final res = await http.get(Uri.parse('https://api.geneh.app/v1/gold'),
headers: {'Authorization': 'Bearer $key'});
final data = jsonDecode(res.body) as Map<String, dynamic>;
_cache = [
for (final p in data['prices'])
if ((p['instrument'] as String).endsWith('k_g'))
GoldPrice(p['instrument'], (p['buy'] as num).toDouble(), (p['sell'] as num).toDouble()),
];
_at = DateTime.now();
return _cache;
}Keep your key safe
A key inside an app can be read by anyone who opens the app. For a public app, call the API from your own server and cache the answer there. Then one key serves every user and stays within your plan.
Always show updated_at next to the prices, so people know how fresh they are.
Gold prices