DEV Community

Hasfiyat
Hasfiyat

Posted on

Building a real-time gold & FX price ticker with WebSocket (Socket.IO)

If you build apps for jewelers, fintech dashboards, or e-commerce price automation, you eventually need one thing: reliable, low-latency gold and currency prices. Scraping fragile sources breaks constantly. A dedicated price API solves this. In this post I'll show how to consume real-time gold (gram, quarter, coin) and FX rates over both REST and WebSocket (Socket.IO) using the Hasfiyat Gold & Currency API.

Why a price API instead of scraping?

  • Stability — a documented contract instead of HTML that changes without notice.
  • Low latency — prices are pushed as the market moves, not on a slow cron.
  • Multiple sources with failover — if one provider drops, the feed keeps flowing.

1. Polling with REST

The simplest integration: request the prices you need with your API key.

curl -X GET \
  'https://api.hasfiyat.com/api/prices?symbols=HAS,GRAM,CEYREK' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Accept: application/json'
Enter fullscreen mode Exit fullscreen mode
// Node.js
const res = await fetch(
  "https://api.hasfiyat.com/api/prices?symbols=HAS,GRAM,CEYREK",
  { headers: { Authorization: "Bearer YOUR_API_KEY" } }
);
const data = await res.json();
console.log(data);
Enter fullscreen mode Exit fullscreen mode

REST is ideal for periodic reporting, server-side jobs, and updating e-commerce product prices.

2. Live updates with Socket.IO

For price screens, signage, and mobile apps where every tick matters, keep a connection open and let the server push changes:

import { io } from "socket.io-client";

const socket = io("https://api.hasfiyat.com", {
  auth: { token: "YOUR_API_KEY" }
});

socket.on("gold_prices", (data) => {
  // { symbol: "HAS", type: "Has Altın", buy: 2450.85, sell: 2455.10, timestamp: "14:32:01.045" }
  console.log(data);
});
Enter fullscreen mode Exit fullscreen mode

No polling, no hammering the server — each market move arrives instantly.

3. A minimal live ticker in the browser

<div id="gold"></div>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
<script>
  const socket = io("https://api.hasfiyat.com", { auth: { token: "YOUR_API_KEY" } });
  socket.on("gold_prices", (p) => {
    document.getElementById("gold").textContent =
      p.type + ": " + p.sell + " TRY";
  });
</script>
Enter fullscreen mode Exit fullscreen mode

That's enough to power a shop-window price board.

REST vs WebSocket — which one?

Use case Best fit
Price screen, signage, mobile ticker WebSocket
Server-side sync, reporting, e-commerce price update REST
Both (recommended) WebSocket for live, REST for verification

Most teams use both: WebSocket for the live stream, REST for validation and batch jobs.

Security notes

Authenticate every request with a Bearer token, and restrict access with an IP/domain whitelist so a leaked key can't be used from anywhere. All traffic runs over HTTPS/WSS.

Wrapping up

With a real-time gold & FX API you skip building (and babysitting) your own price infrastructure. Sample code and the full endpoint reference are here:

If you're building anything that displays gold or currency prices in Turkey, give it a try and let me know how it goes in the comments.

Top comments (0)