Bar Charts
Learn to retrieve historical data to create charts.
Aggregates endpoint
The aggregate endpoint available in Polygon provides aggregate bars of a security over a specified time window. We can use this data to analyze growth or decline in a security's values.
The base URL for the aggregates endpoint is as follows:
https://api.polygon.io/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}
Request parameters
The aggregates endpoint requires some mandatory and optional parameters to filter our output. The following table gives details about these parameters.
Name | Type | Category | Description |
| String | Mandatory | The ticker associated with the security we want. |
| String | Mandatory | The time over which the results are recorded. Possible input: So, with a multiplier of two and a timespan of an hour, we will get data for every two hours. |
| Integer | Mandatory | The size of the timespan multiplier. |
| String | Mandatory | This defines the starting date YYYY-MM-DD or timestamp in milliseconds of the time window. |
| String | Mandatory | This defines the ending date YYYY-MM-DD or timestamp in milliseconds of the time window. |
| String | Optional | This is set to |
| Integer | Optional | This defines the number of results required. By default, it is set to |
| String | Optional | This is used to sort the results based on Accepted input: |
Let’s try to call this endpoint in the following code:
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';//Required parametersconst ticker = "AAAU";const multiplier = 1;const timespan = 'day';const from_ = '2021-07-07';const to = '2021-07-14';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v2/aggs/ticker/${ticker}/range/${multiplier}/${timespan}/${from_}/${to}`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`,contentType: 'application/json',};// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function Aggregates() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callAggregates();
Here’s an explanation for the above code:
Lines 8–12: We define the required parameters.
Line 15: We define the endpoint URL.
Lines 18–21: We define the API key in the header parameter.
Lines 24–27: We define the options parameters and set the request type as
GET
.Lines 30–40: We define the
Aggregates
function, which calls the endpoint.Line 43: We call the
Aggregates
function.
Response fields
The following table provides details of the attributes in the response object.
Name | Type | Description |
| String | The ticker given in the request parameter. |
| Integer | The number of aggregates used to generate the response. |
| Integer | The total number of results given. |
| Boolean | This tells us if the response was adjusted for splits or not. |
| Object | This contains the details of our query. |
| Integer | The volume of the ticker in the given time window. |
| Integer | The weighted average volume in the given time window. |
| Integer | The opening price of the ticker. |
| Integer | The closing price of the ticker. |
| Integer | The highest price of the ticker. |
| Integer | The lowest price of the ticker. |
| Integer | The time since the start of the aggregate window in milliseconds. |
| Integer | The total number of transactions in the aggregate window. |
| String | The status of our query. |
| String | The ID associated with our request. |
| Integer | The number of results returned. |
Grouped daily endpoint
The grouped daily endpoint gives details about the opening, closing, highest, and lowest prices of the entire market on a specified date. This helps traders to compare the prices of different securities and make decisions based on them.
The base URL for the grouped daily endpoint is given below:
https://api.polygon.io/v2/aggs/grouped/locale/{locale_type}/market/{security_type/{date}
Request parameters
The following table provides an overview of the parameters required to call the grouped daily endpoint:
Name | Type | Category | Description |
| String | Mandatory | Filter results based on location. Available options: |
| String | Mandatory | The security whose data we want. Possible input: |
| String | Mandatory | The starting date for the aggregate window. |
| String | Optional | This is set to |
| Boolean | Optional | This parameter is only used in case our |
Let’s try to call this endpoint in the code widget given below:
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';//Required paramentersconst locale_type = 'global';const security_type = 'crypto';const date = '2021-07-07';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v2/aggs/grouped/locale/${locale_type}/market/${security_type}/${date}`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`,contentType: 'application/json',};// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function GroupedDaily() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callGroupedDaily();
Here’s an explanation for the above code:
Lines 8–10: We define the required parameters.
Line 8: We set the
locale_type
toglobal
.Line 9: We set the
security_type
tocrypto
to get results related to cryptocurrency.Line 10: We set a date to specify the starting point of the aggregate window.
Line 13: We define the endpoint URL.
Lines 16–19: We define the API key in the header parameter.
Lines 22–25: We define the options parameters and set the request type as
GET
.Lines 28–38: We define the
GroupedDaily
function, which calls the endpoint.Line 41: We call the
GroupedDaily
function.
Change the required parameters’ values in lines 8–10 to get different results.
Response fields
The following table gives an overview of the attributes found in the response object of the grouped daily endpoint:
Name | Type | Description |
| Integer | The number of aggregates used to generate a response to our query. |
| Integer | The total number of results for our query. |
| Boolean | This tells us if the result is adjusted for splits or not. |
| Object | This object contains details about our query. |
| String | This is the given ticker. |
| Integer | The volume of the ticker in the given time window. |
| Integer | The weighted average volume in the given time window. |
| Integer | The opening price of the ticker. |
| Integer | The closing price of the ticker. |
| Integer | The highest price of the ticker. |
| Integer | The lowest price of the ticker. |
| Integer | The time since the start of the aggregate window in milliseconds. |
| Integer | The total number of transactions in the aggregate window. |
| String | The response status to our query. |
| String | The ID assigned to our request by the server. |
| Integer | The number of results to our query. |