EUR/USD 1.14362 +0.08% GBP/USD 1.34609 +0.11% USD/JPY 162.390 -0.08% USD/CHF 0.80721 -0.15% AUD/USD 0.69865 +0.12% USD/CAD 1.40094 -0.06% NZD/USD 0.58489 +0.18% BTC/USD 64,061.90 -0.94% ETH/USD 1,854.93 -0.83% · EUR/USD 1.14362 +0.08% GBP/USD 1.34609 +0.11% USD/JPY 162.390 -0.08% USD/CHF 0.80721 -0.15% AUD/USD 0.69865 +0.12% USD/CAD 1.40094 -0.06% NZD/USD 0.58489 +0.18% BTC/USD 64,061.90 -0.94% ETH/USD 1,854.93 -0.83% ·
3 min read forex api javascript

How to Retrieve JSON forex data in Javascript

In this article we will talk about a very practical approach: How to obtain real-time forex rates in JSON format for a solution developed in Javascript.

How to Retrieve JSON forex data in Javascript

Fetching real-time forex data in JavaScript is one of the most common integration patterns Live-Rates developers work with. Whether the goal is a trading dashboard, a fintech widget, or a data pipeline, the workflow starts with parsing a JSON feed and ends with persisting it somewhere useful.

This guide walks through that path using Node.js: pulling rates from the Live-Rates endpoint, then inserting them into MySQL.

Key takeaways

  • Live-Rates exposes real-time forex, commodities, crypto, and stock data as JSON.
  • Node's built-in https module is enough to consume the feed at https://www.live-rates.com/rates.
  • The free tier is capped at 3 requests per hour; higher frequency requires a paid subscription.
  • Each instrument returns bid, ask, high, low, open, close, and timestamp fields ready for storage.
  • A short loop is sufficient to persist the response into a MySQL table.

Fetching JSON Rates with Node.js

The first step is to parse the data returned by the Live-Rates endpoint. Node's core https module handles the request without any external dependencies:

const https = require('https');

https.get('https://www.live-rates.com/rates', (resp) => {
  var instruments = '';

  // Real-time Forex, Commodities, Crypto, Stocks, etc are received on this request
  resp.on('data', (chunk) => {
    instruments += chunk;
  });

Once the response completes, the instruments variable holds every quote returned by the API, ready for the next stage of the pipeline.

Rate Limits on the Free Tier

The free API allows up to 3 requests per hour. Applications that need higher polling frequency should move to a paid subscription.

Persisting Rates into MySQL

With the JSON payload in memory, the next step is storing it. The pseudo-code below iterates through each instrument in the response and inserts it into a MySQL table:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");

  // example output: instruments.first
  // {"currency":"ETH","bid":"143.74","ask":"145.19","high":"150.48","low":"128.74","open":"128.94","close":"143.74","timestamp":"1545652002803"}

  instruments.forEach(function(instrument) {
    var sql = "INSERT INTO currencies (currency, bid, ask, high, low, open, close, timestamp) VALUES (instrument.currency, instrument.bid, instrument.ask, instrument.high, instrument.low, instrument.open, instrument.close, instrument.timestamp)";
    con.query(sql, function (err, result) {
      if (err) throw err;
      console.log("1 record inserted");
    });
  });
});

Understanding the Payload

Each element in the array is a single instrument with fields for currency, bid, ask, high, low, open, close, and timestamp. The example above shows an ETH quote, but the same schema applies across every asset class returned by the endpoint.

Building on Top of the Feed

The combined snippet is all that is required to interact with the Live-Rates API from a JavaScript stack. The use cases are open-ended: dashboards, alerting systems, historical archives, or downstream analytics.

The mission behind the service is to provide a reliable source of financial data in a machine-readable format for whatever use case a developer needs to build.

Working in PHP Instead?

For teams working in PHP, the companion walkthrough How to use a very simple PHP Forex API covers the same workflow in that language.

FAQ

Which endpoint returns the JSON forex feed?

The feed is served from https://www.live-rates.com/rates and can be consumed using Node's built-in https module.

What asset classes are included in the response?

Each request returns real-time forex, commodities, crypto, and stock instruments in the same JSON payload.

How often can the free API be called?

The free tier is limited to 3 requests per hour. More frequent polling requires a paid subscription.

What fields are available per instrument?

Every instrument includes currency, bid, ask, high, low, open, close, and timestamp values, ready to be inserted into a database column of the same name.

Is there a guide for PHP developers?

Yes. The post "How to use a very simple PHP Forex API" covers the equivalent integration for PHP applications.

Ready to move beyond the free tier and stream rates at production frequency? Explore the options on the Live-Rates plans page.

Real-time forex rates for your app

Live bid/ask for the pairs you need, updated every second, with a simple JSON & XML API. Try it free for 7 days.