10000 Update README with pagination and trace modes (#176) · polygon-io/client-js@e77bab0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e77bab0

Browse files
Update README with pagination and trace modes (#176)
1 parent d6a9520 commit e77bab0

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,78 @@ rest.stocks.snapshotAllTickers().then((data) => {
6464
See [full examples](./examples/rest/) for more details on how to use this client effectively.
6565
To run these examples from the command line, first check out this project and run ```npm i``` in the root directory to install dependencies, then run ```POLY_API_KEY=yourAPIKey node examples/rest/crypto-aggregates_bars.js```, replacing yourAPIKey with your Polygon API Key.
6666

67+
## Pagination
68+
69+
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.
70+
71+
```javascript
72+
import('@polygon.io/client-js').then(({ restClient }) => {
73+
const globalFetchOptions = {
74+
pagination: true,
75+
};
76+
const rest = restClient("XXXX", "https://api.polygon.io", globalFetchOptions);
77+
rest.stocks.aggregates("TSLA", 1, "minute", "2022-01-01", "2023-08-31", { limit: 50000 }).then((data) => {
78+
const resultCount = data.length;
79+
console.log("Result count:", resultCount);
80+
}).catch(e => {
81+
console.error('An error happened:', e);
82+
});
83+
});
84+
```
85+
86+
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.
87+
88+
## Debugging
89+
90+
Sometimes you may find it useful to see the actual request and response details while working with the API. The client allows for this through the `globalFetchOptions` by turning on the `trace: true` option.
91+
92+
### How to Enable Debug Mode
93+
94+
You can activate the debug mode as follows:
95+
96+
```javascript
97+
import('@polygon.io/client-js').then(({ restClient }) => {
98+
const globalFetchOptions = {
99+
trace: true,
100+
};
101+
const rest = restClient("XXXX", "https://api.polygon.io", globalFetchOptions);
102+
rest.stocks.aggregates("TSLA", 1, "minute", "2023-08-01", "2023-08-01", { limit: 50000 }).then((data) => {
103+
const resultCount = data.length;
104+
console.log("Result count:", resultCount);
105+
}).catch(e => {
106+
console.error('An error happened:', e);
107+
});
108+
});
109+
```
110+
111+
### What Does Debug Mode Do?
112+
113+
When debug mode is enabled, the client will print out useful debugging information for each API request. This includes: the request URL, the headers sent in the request, and the headers received in the response.
114+
115+
### Example Output
116+
117+
For instance, if you made a request for `TSLA` data for the date `2023-08-01`, you would see debug output similar to the following:
118+
119+
```
120+
Request URL: https://api.polygon.io/v2/aggs/ticker/TSLA/range/1/minute/2023-08-01/2023-08-01?limit=50000
121+
Request Headers: { Authorization: 'Bearer REDACTED' }
122+
Response Headers: Headers {
123+
[Symbol(map)]: [Object: null prototype] {
124+
server: [ 'nginx/1.19.2' ],
125+
date: [ 'Thu, 06 Jul 2023 18:34:27 GMT' ],
126+
'content-type': [ 'application/json' ],
127+
'transfer-encoding': [ 'chunked' ],
128+
connection: [ 'close' ],
129+
'content-encoding': [ 'gzip' ],
130+
vary: [ 'Accept-Encoding' ],
131+
'x-request-id': [ '06dc97920681d8335c0451894aa1f79f' ],
132+
'strict-transport-security': [ 'max-age=15724800; includeSubDomains' ]
133+
}
134+
}
135+
```
136+
137+
This can be an invaluable tool for debugging issues or understanding how the client interacts with the API.
138+
67139
## Launchpad Usage
68140

69141
Users of the Launchpad product will need to pass in certain headers in order to make API requests. Example can be found [here](./examples/rest/launchpad/README.md).

0 commit comments

Comments
 (0)
0