/client-js

The official JS client library for the Polygon REST and WebSocket API.

Primary LanguageTypeScriptMIT LicenseMIT

code style: prettier

Upgrading to Version 7

See the Release Notes for instructions on upgrading to Version 7.

Install

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

usage

Authentication

  • call the desired client with your api key to initialize it
import { polygonClient, restClient, websocketClient } from "@polygon.io/client-js";
const rest = restClient("API KEY");

// you can use the api now

rest.forex
  .previousClose("C:EURUSD")
  .then(/* your success handler */)
  .catch(/* your error handler*/);
  • import the rest submodule
import { restClient } from "@polygon.io/client-js";

const rest = restClient("API KEY");

rest.forex.previousClose("C:EURUSD").then(/* your success handler */);
  • import a specific submodule
import { referenceClient } from "@polygon.io/client-js";

const reference = referenceClient("API KEY");

reference.tickers().then(/* your success handler */);
reference.conditions({ asset_class: 'stocks', data_type: 'trades', sort: 'id' }).then(/* your success handler */)

You can get preauthenticated websocket clients for the 3 topics.

import { websocketClient } from "@polygon.io/client-js";

const stocksWS = websocketClient("API KEY").stocks();

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

  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" });

documentation

  • Generate the package documentation
npm run generate-doc