Options Specific
Learn to fetch information about option contracts.
We'll cover the following
In an options contract, buyers have the right to buy or sell underlying securities within a specific time at a specific price. There are two types of options contracts:
Option calls: This contract gives buyers the right to buy assets.
Option puts: This contract gives buyers the right to sell assets.
Options contracts
Polygon API gives us free access to the information about active and expired options contracts within the previous two years through the options contracts endpoint.
The base URL for this endpoint is as follows:
https://api.polygon.io/v3/reference/options/contracts
Request Parameters
By default, the options contracts endpoint provides a list of 10 options contracts. However, we can filter our search by providing some additional request parameters.
The following table gives details about the request parameters for the options contracts endpoint:
Name | Type | Category | Description |
| String | Optional | The ticker associated with the options contract we are looking for. |
| String | Optional | The ticker associated with the underlying stock. |
| String | Optional | The type of options contract we are looking for. Accepted input: |
| String | Optional | This is used to define the expiration date. The accepted format is YYYY-MM-DD. |
| String | Optional | This is used to find contracts within the specified date in the format YYYY-MM-DD. |
| Integer | Optional | This is used to filter contracts by strike price, which is the price at which the contract is traded. |
| Boolean | Optional | If this is set to |
| String | Optional | The field used to sort the results. Accepted input: |
| String | Optional | The order in which the results will be sorted, depending on the input given in Accepted input: |
| Integer | Optional | This is the number of contracts required in the result. By default, it is set to |
We can provide additional filter parameters, such as .lt
(less than), .gt
(greater than), .lte
(less than equal to), and .gte
(greater than equal to), for the underlying_ticker
, expiration_date
, and strike_price
parameters. For example, if we want option contracts where the strike price is less than 200, we can give the following request parameter:
const queryParameters = new URLSearchParams({"strike_price.lt" : 200});
Let’s call the options contracts endpoint with its default parameters in the following code:
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v3/reference/options/contracts`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`};// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function OptionsContracts() {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 callOptionsContracts();
Here’s an explanation for the above code:
Line 8: We define the endpoint URL.
Lines 11–13: We define the API key in the header parameter.
Lines 16–19: We define the options parameters and set the request type as
GET
.Lines 22–32: We define the
OptionsContract
function, which calls the endpoint.Line 35: We call the
OptionsContract
function.
Now, let’s call this endpoint to fetch option contracts whose contract type is put
, and sort them in ascending order based on ticker
.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst apiKey = '{{API_KEY}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.polygon.io/v3/reference/options/contracts`);// Define Header Parameters hereconst headerParameters = {authorization: `Bearer ${apiKey}`,contentType: 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({contract_type: 'put',sort: 'ticker',order: 'asc',});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function OptionContract() {try {endpointUrl.search = queryParameters;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 callOptionContract();
In lines 17–21, we have provided contract_type
, sort
, and order
in the query parameters. We can change these to fetch different options contracts.
Response fields
The following table gives details about the attributes found in the response object:
Name | Type | Description |
| Array | This has the details about the option contracts sent as an input parameter. |
| String | The CFI code of the contract. |
| String | This is type of contract. It can be |
| Enum | This defines the class of the option. Possible output: |
| String | This is the expiration date of the contract given in the YYYY-MM-DD format. |
| String | The code of the primary exchange of the options contract. |
| Integer | The total number of shares in the contract. |
| Integer | The price at which the contract is traded. |
| String | The ticker associated with the option contract. |
| String | The ticker of the underlying asset to which the options contract is related. |
| Array | This contains information about the additional underlying asset of the options contract. |
| String | The response to our query. |
| String | The ID associated with our query. |
| String | The URL to the next page, if it exists. |