Skip to content

The code example below shows how to make a request for options data using JavaScript. This code should work both in the browser, and Node.js. More information about the Fetch API can be found on the Mozilla Developer site. The full code can be downloaded here.

REQUIRED

The Theta Terminal must be running to access data.

js
// all endpoints use this URL base
const BASE_URL = 'http://127.0.0.1:25510/v2';
const params = new URLSearchParams({
  root: 'MSFT',
  exp: '0',
  use_csv: 'true',
});

let url = `${BASE_URL}/bulk_snapshot/option/ohlc?${params.toString()}`;

try {
  while (url) {
    const response = await fetch(url);

    // Throw if the request failed
    if (!response.ok) {
      throw new Error(`Response status: ${response.status}`);
    }

    const data = await response.text();
    console.log(data); // Process or use the data here

    const nextPage = response.headers.get('Next-Page');
    if (nextPage && nextPage !== 'null') {
      url = nextPage;
    } else {
      url = null;
    }
  }
} catch (error) {
  // Handle errors
  console.error('Error fetching data:', error);
}