This repository demonstrates a minimal example of using the Polygon.io JavaScript Client library to make API calls. It shows how to fetch cryptocurrency aggregate data with just a few lines of code.
This example is hosted at https://github.com/polygon-io/js-client-example.
Before getting started, ensure you have the following:
- Git: Install Git by following the Git Installation Guide.
- Verify installation by running the following in a terminal window:
git --version
- Verify installation by running the following in a terminal window:
- Node.js: Download and install the latest version from nodejs.org.
- Verify installation by running:
node --version npm --version
- Verify installation by running:
- An API Key from Polygon.io: Sign up at Polygon.io and generate your API key.
Use Git to clone this repository to your local machine:
git clone https://github.com/polygon-io/js-client-example.git
cd js-client-example
Install the required library by running:
npm install
This will install the @polygon.io/client-js
library as specified in the package.json
file.
Open the index.js
file and replace 'YOUR_API_KEY'
with your actual API key from Polygon.io. For example:
const rest = restClient('YOUR_ACTUAL_API_KEY', 'https://api.polygon.io');
Run the script using Node.js by entering the following command in your terminal from the root of the project:
node index.js
If everything is set up correctly, you'll see the aggregate cryptocurrency data for Bitcoin (BTC) vs. USD in the console.
When you run the script, you should see JSON output similar to this:
{
"ticker": "X:BTCUSD",
"queryCount": 5,
"resultsCount": 5,
"adjusted": true,
"results": [
{ "o": 23456.78, "h": 23987.65, "l": 23123.45, "c": 23890.12, "v": 1234, "t": 1674950400000 },
...
]
}
If there's an error (e.g., invalid API key or network issue), it will display an error message in the console.
- Invalid API Key: Ensure your API key is correct and not expired.
- Connection Issues: Ensure you have an active internet connection and can access
https://api.polygon.io
. - Dependencies Not Installed: Run
npm install
again to ensure@polygon.io/client-js
is installed properly.
Happy coding! 🚀