With Polygon.io, a competitor to Alpaca, I can place a simple API request from a URL.
The only information I need is stock price. Installing the alpaca packages seems like overkill.
Here is the entire string I use to access the polygon api:
const tempURL = https://api.polygon.io/v3/snapshot/options/
+ SYM +?strike_price=
+ strikePrice +&expiration_date=
+ dateToday_api +&apiKey=
+ APIKEY;
When copied into Browser address bar, this returns my JSON data.
I then use axios, a node.js package, to make the call to the API.
How would I build a URL to continue making the request with my preferred package?
To rephrase, where do I find the URL to enter into address bar to make a request?
Thanks!
@sethschaffner While Alpaca data requests can be made as simply as requests in Polygon, there is one subtle difference. Notice the Polygon API key is part of the query parameters (ie it’s in the URL after the “?”). That can be entered easily into a browser. Alpaca however requires the API keys to be passed in the header portion of the request. There isn’t a direct way to modify the header information in most browsers (though there are some extensions or developer tools which can be used).
One can make API calls directly from node.js with the correct URL and header (ie API key) data. Take a look at the docs to see how to format the request.
1 Like
Instead of copying the entire string into the browser address bar, you’ll use your preferred package (like Axios) to construct the URL dynamically. The package will allow you to specify the base URL and then define each parameter as a key-value pair.
For example, with Axios, your code might look something like this:
const axios = require(‘axios’);
// Replace with your actual values
const apiKey = ‘YOUR_API_KEY’;
const symbol = ‘AAPL’;
const strikePrice = 150;
// Assuming you have a function to format the date as required by Polygon.io
const dateTodayAPI = formatDateForAPI(new Date());
const url = https://api.polygon.io/v3/snapshot/options/${symbol}?strike_price=${strikePrice}&expiration_date=${dateTodayAPI}&apiKey=${apiKey}
;
axios.get(url)
.then(response => {
// Handle successful response with the JSON data
console.log(response.data);
})
.catch(error => {
// Handle errors during the API call
console.error(error);
});