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, params);

        // throw if the request didn't work
        if (!response.ok) {
            throw new Error(`Response status: ${response.status}`);
        }

        const data = await response.text()
        console.log(data);  // do something with  the data

        if (response.hasOwnProperty('Next-Page') && response['Next-Page'] !== 'null') {
            url = response['Next-Page'];
        } else {
            url = null;
        }
    }
} catch (error) {
    // handle any errors
    console.log(error);
}