Socket
Book a DemoInstallSign in
Socket

@polygon.io/client-js

Package Overview
Dependencies
Maintainers
8
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polygon.io/client-js

Isomorphic Javascript client for Polygon.io Stocks, Forex, and Crypto APIs

8.2.0
latest
Source
npmnpm
Version published
Weekly downloads
8.9K
-6.81%
Maintainers
8
Weekly downloads
 
Created
Source

Polygon JS Client

code style: prettier

Welcome to the official JS client library for the Polygon REST and WebSocket API. To get started, please see the Getting Started section in our documentation, view the examples directory for code snippets, or the blog post with video tutorials to learn more. To generate the package documentation please run npm run generate-doc.

For upgrade instructions please see the Release Notes.

Getting the client

To get started, you'll need to install the client library:

npm install --save '@polygon.io/client-js'

Next, create a new client with your API key.

import { restClient } from '@polygon.io/client-js';
const rest = restClient(process.env.POLY_API_KEY);

Using the client

After creating the client, making calls to the Polygon API is easy. For example, here's how to get aggregates (bars):

rest.getStocksAggregates("AAPL", 1, GetStocksAggregatesTimespanEnum.Day, "2023-01-01", "2023-04-14").then((response) => {
	console.log(response);
}).catch(e => {
	console.error('An error happened:', e);
});

Or, maybe you want to get the last trades or quotes for a ticker:

// last trade
rest.getLastStocksTrade("AAPL").then((response) => {
	console.log(response);
}).catch(e => {
	console.error('An error happened:', e);
});

// last quote (NBBO)
rest.getLastStocksQuote("AAPL").then((response) => {
	console.log(response);
}).catch(e => {
	console.error('An error happened:', e);
});

Finally, maybe you want a market-wide snapshot of all tickers:

rest.getSnapshots().then((response) => {
	console.log(response);
}).catch(e => {
	console.error('An error happened:', e);
});

See full examples for more details on how to use this client effectively.

Pagination

The client can handle pagination for you through the globalFetchOptions by turning on the pagination: true option. The feature will automatically fetch all next_url pages of data when the API response indicates more data is available.

import { restClient } from '@polygon.io/client-js';

const globalFetchOptions = {
	pagination: true,
};
const rest = restClient(process.env.POLY_API_KEY, "https://api.polygon.io", globalFetchOptions);

rest.getStocksAggregates("AAPL", 1, GetStocksAggregatesTimespanEnum.Day, "2023-01-01", "2023-04-14").then((response) => {
	const data = response.data; // convert axios-wrapped response
	const resultCount = data.resultsCount;
	console.log("Result count:", resultCount);
}).catch(e => {
	console.error('An error happened:', e);
});

If there is a next_url field in the API response, the client will recursively fetch the next page for you, and then pass along the accumulated data.

WebSocket Client

Import the Websocket client and models packages to get started. You can get preauthenticated websocket clients for the 3 topics.

import { websocketClient } from "@polygon.io/client-js";
const stocksWS = websocketClient(process.env.POLY_API_KEY, 'wss://delayed.polygon.io').stocks();

stocksWS.onmessage = ({response}) => {
  const [message] = JSON.parse(response);

  stocksWS.send('{"action":"subscribe", "params":"AM.MSFT,A.MSFT"}');

  switch (message.ev) {
    case "AM":
      // your trade message handler
      break;
    case "A":
      // your trade message handler
      break;
  }
};

stocksWS.send({ action: "subscribe", params: "T.MSFT" });

See full examples for more details on how to use this client effectively.

Contributing

If you found a bug or have an idea for a new feature, please first discuss it with us by submitting a new issue. We will respond to issues within at most 3 weeks. We're also open to volunteers if you want to submit a PR for any open issues but please discuss it with us beforehand. PRs that aren't linked to an existing issue or discussed with us ahead of time will generally be declined. If you have more general feedback or want to discuss using this client with other users, feel free to reach out on our Slack channel.

Keywords

polygon.io

FAQs

Package last updated on 13 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.