Giter Club home page Giter Club logo

crypto-sheets-api's Introduction

CI Docker Cloud Build Status GitHub deployments

Cryptocurrency data API for Google Sheets

Using CoinGecko API in Sheets to get cryptocurrency price data through a proxy API.

Basic example

Use ImportXML to get bitcoin's current price in USD:

=ImportXML("https://your-proxy-api/xml/coingecko/simple/price?
    ids=bitcoin&vs_currencies=usd", "result/bitcoin/usd")

Live example API and Sheet:

Usage for CoingGecko routes

Use /xml/coingecko or /value/coingecko to import data from any CoinGecko API route.

For full documentation of endpoints (live OpenAPI) go to https://your-proxy-api/docs (Example on Render)

Use any route on CoinGecko API live docs to create your target path.

Example: Use /simple/price to get current bitcoin price in usd: /simple/price?ids=bitcoin&vs_currencies=usd. See the options below for usage in Sheets.

Using ImportXML in Sheets:

=ImportXML("https://your-proxy-api/xml/coingecko/simple/price?
    ids=bitcoin&vs_currencies=usd", "result/bitcoin/usd")
Detailed instruction

Xpath expression can be used more easilty since the full XML is directly visible as output of the proxy API.

  1. Check the proxy API's output XML by going to the proxy URL (e.g. https://your-proxy-api/xml/coingecko/simple/price?ids=bitcoin&vs_currencies=usd in the browser)
  2. Use XPath syntax to create an XPath expression to extract your data (example: result/bitcoin/usd)

Using ImportXML in Sheets with JSONPath:

=ImportXML("https://your-proxy-api/xml/coingecko/simple/price?
    ids=bitcoin&vs_currencies=usd&jsonpath=bitcoin.usd","result")
Detailed instructions

JSONPath should be preferred because not every valid JSON can be converted into XML (e.g. if some keys start with numbers).

  1. Check CoinGecko's output JSON by going to the target URL in the browser (example: https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd).
  2. Use JSONPath syntax to create a JSONPath expression to get to your value (example: bitcoin.usd).

Using ImportDATA in Sheets:

=ImportDATA("https://your-proxy-api/value/coingecko/simple/price?
    ids=bitcoin&vs_currencies=usd&jsonpath=bitcoin.usd")
Detailed instructions

ImportDATA is limited to 50 calls per sheet, so should be used in small sheets only.

The /value/coingecko endpoint can be used to return just the value as plain text which allows using ImportDATA Sheets function instead of ImportXML.

Follow the same steps as for JSONPath with ImportXML above, but use a /value/coingecko proxy route and ImportDATA instead of ImportXML.

Usage for any other API (not necessarily CoinGecko)

Use the generic /xml/any or /value/any to import data from any other API URL that returns a JSON. Intead of CoinGecko routes, use the full target URL.

For example, in /xml/coingecko/ example we used:

https://your-proxy-api/xml/coingecko/simple/price?ids=bitcoin&vs_currencies=usd

The generic equivalent would be:

https://your-proxy-api/xml/any/https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd (add jsonpath as needed)

Running the API

For the API to be accessible from Sheets it needs to be publicly accessible (because Google is making the requests not from your local machine).

Host API on Render

This option is best for actual usage (the free tier should be enough). Also best in terms of privacy

Deploy to Render

Run API locally and expose publicly via ngrok:

This option is best for development or temporary usage (free as well).

1. Run the API locally:

Local python option
  1. Install in local virtual env after cloning: make install
  2. Run local server: make server
Docker with local code option
  1. After cloning: make docker-server
Docker without cloning repo option
  1. docker run -it --rm -p 9000:9000 artdgn/crypto-sheets-api (or -p 1234:9000 to run on different port)

2. Set up tunnelling:

Tunnelling with ngrok
  • After setting up an ngrok account and local client:
  • Run /path/to/ngrok http <port-number> to run ngrok (e.g. ~/ngrok/ngrok http 9000 if ngrok lives in ~/ngrok/ and you're using the default port of 9000. If you have the local repo, you can also just make ngrok to run this command.

Alternative solutions

ImportJSON seems to also work, and doesn't require any external resources (I only found it after I've already implemented this proxy API because initially I found only non-working solutions of that type).

Some other options that didn't work for me (why I've implemented this)
  • CRYPTOFINANCE stopped working. In general trying many of the Google App Scripts solutions (like IMPORTJSON or like the updated CRYPTOFINANCE) didn't work for me because of Auth issues.
  • Other Google Sheet add-ons like Apipheny were either paid or required API keys (so registration, or additional Yak-Shaving).

Privacy thoughts

Privacy related thoughts

TL;DR: probably best to host your own.

  1. I don't think there's a way to know which accounts are making any of the requests.
  2. Hosting your own proxy API (e.g. on Render) is probably the best option since your requests will be visible only to your proxy (and Render).
  3. Hosting a local proxy API via tunnelling (the "ngrok" option) will mean that requests to CoinGecko (or any other API you're using through this) will come from your machine.
  4. Using my example deployment means that I can see the request parameters in the logs (but with no idea about the google accounts).

Related resources

  • I've added a more generalised version of this that supports POST requests, and doesn't have crypto-currency related endpoints, or examples. It aims to be useful for any target API, and not specifically for crypto-currency data.

crypto-sheets-api's People

Contributors

artdgn avatar

Watchers

 avatar  avatar

Forkers

gazsim

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.