- Requirements:
pip install polygon-api-client.. automethod:: polygon.RESTClient.__init__
You can pass your API key via the environment variable POLYGON_API_KEY or as the first parameter to the RESTClient constructor:
from polygon import RESTClient
client = RESTClient() # POLYGON_API_KEY is used
client = RESTClient("api_key") # api_key is usedFor non-paginated endpoints call get_*:
.. literalinclude:: ../../examples/rest/simple-get.py
For paginated endpoints call list_* and use the provided iterator:
.. literalinclude:: ../../examples/rest/simple-list.py
Note
The number of network requests made by the iterator depends on the value of the parameter limit.
limit specifies how many results should be returned per network request.
You can see each network request by passing verbose = True to the client.
For endpoints that have a set of parameters you can use the provided :doc:`enums </Enums>`.
from polygon.rest.models import Sort
client.list_trades(..., sort=Sort.ASC)To handle the raw urllib3 response yourself pass raw=True:
.. literalinclude:: ../../examples/rest/raw-get.py
If it is a paginated list_* response it's up to you to handle the "next_url" iteration:
.. literalinclude:: ../../examples/rest/raw-list.py
To provide your own JSON processing library (exposing loads/dumps functions) pass custom_json=my_module:
.. literalinclude:: ../../examples/websocket/custom-json-get.py
.. automethod:: polygon.WebSocketClient.__init__
The simplest way to use the websocket client is to just provide a callback:
.. literalinclude:: ../../examples/websocket/simple.py
Note
Raises AuthError if invalid API key is provided.
If you want to capture state you can use a global variable inside the callback. Alternatively, you can wrap a class method in a closure.
.. literalinclude:: ../../examples/websocket/aggs.py
Under the hood our client uses an asynchronous runtime. To manage the runtime
yourself (including unsubscribing and subscribing) you can use asyncio and the
.connect method:
.. literalinclude:: ../../examples/websocket/async.py
To handle raw string or byte messages yourself pass raw=True:
.. literalinclude:: ../../examples/websocket/raw.py
To provide your own JSON processing library (exposing loads/dumps functions) pass custom_json=my_module:
.. literalinclude:: ../../examples/websocket/custom-json.py