Pulling Crypto Data from KuCoin's API into Google Sheets

I’m working in google script app attempting to pull all the KuCoin ticker symbols, the current day’s price, the six-month low, the six-month high, and post it to a google sheet.

So far, I have:
‘’’
function fetchPriceData() {
var response = UrlFetchApp.fetch(“https://api.kucoin.com/api/v1/accounts”, options);
var json = JSON.parse(response.getContentText());

var currentPrice = json.current_price;
var sixMonthLow = json.six_month_low;
var sixMonthHigh = json.six_month_high;

var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(“A1”).setValue(“Current Price”);
sheet.getRange(“A2”).setValue(“Six Month Low”);
sheet.getRange(“A3”).setValue(“Six Month High”);
sheet.getRange(“B1”).setValue(currentPrice);
sheet.getRange(“B2”).setValue(sixMonthLow);
sheet.getRange(“B3”).setValue(sixMonthHigh);
}
‘’’

This is the error message I’m receiving:
‘’’
Error
ReferenceError: options is not defined
fetchPriceData
‘’’
Any idea what I’m missing?

I guess the obvious question is did you define an options object?

If you look at the documentation (Class UrlFetchApp  |  Apps Script  |  Google for Developers) for the fetch(url, params) method, you can see an example:

var options = {
  'method' : 'post',
  'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('https://httpbin.org/post', options);

In the documentation, you can see the types and description of the Advanced Parameters which can be included in the options object.

1 Like

To pull crypto data from KuCoin’s API into Google Sheets, you can follow these general steps:

  1. Set up a Google Sheets document: Create a new Google Sheets document or open an existing one where you want to import the data.
  2. Enable the Google Sheets API: Go to the Google Cloud Platform Console (console.cloud.google.com), create a new project (if you haven’t already), and enable the Google Sheets API for your project.
  3. Obtain API credentials: In the Google Cloud Platform Console, create API credentials (OAuth 2.0 client ID) for your project. Make sure to set the redirect URI to oauthplayground
  4. Authorize access to your Google Sheets: Use the OAuth 2.0 Playground
    oauthplayground to obtain an access token and refresh token. Follow the steps below:
  5. a. In the OAuth 2.0 Playground, enter spreadsheets in the Input your own scopes field. b. Click on the “Authorize APIs” button and go through the authorization flow. c. Exchange the authorization code for tokens by clicking on the “Exchange authorization code for tokens” button. d. Save the “Refresh token” for later use.
  6. Retrieve data from KuCoin’s API: Use your preferred programming language (such as Python) to make requests to KuCoin’s API and retrieve the desired crypto data. You’ll need to use the appropriate endpoints and provide any required authentication, such as an API key and secret.
  7. Format and write data to Google Sheets: Once you have the data from KuCoin’s API, format it as per your requirements and use the Google Sheets API to write the data to the desired sheet in your Google Sheets document. You’ll need to use the access token and refresh token obtained earlier for authentication.
  8. Automate the process (optional): If you want to regularly update the data in Google Sheets, you can set up a cron job or a scheduled task to run your script at specified intervals.

Please note that this is a high-level overview, and the implementation details may vary depending on your programming language and the specific libraries or frameworks you choose to use. You may need to refer to the documentation for the Google Sheets API, KuCoin’s API, and any programming libraries you use for more detailed instructions.

Remember to handle any rate limits, data transformations, and error handling as necessary in your script.