- 16 Jul 2024
- 1 Minute to read
- PDF
Python Example
- Updated on 16 Jul 2024
- 1 Minute to read
- PDF
Follow these necessary steps to write and run a simple Python script that queries the Red Canary API.
Prerequisites
Before running any code, ensure you have the following:
Create your Python program
Create a Python program called example.py
, which fetches a list of endpoints from a subdomain.
In your working directory, create a new file called
example.py
, and then copy the following code into it.import requests # Build a GET request. url = 'https://.my.redcanary.co/openapi/v3/endpoints' headers = {'X-Api-Key': ''} # Send the request and save the response. response = requests.get(url, headers=headers) # Print the response body. print(response.content)
In
example.py
, replace with the Red Canary subdomain you want to query.In
example.py
, replace with your API key.Save
example.py
.
Run the example
Open the command line, and run your example.
python3 example.py
The program should print a JSON object containing the first page of endpoints associated with the subdomain.
Customize your request with parameters
You can use HTTP parameters to customize the results of your request. Visit the Red Canary API docs for a list of supported parameters for each API endpoint.
Example: Limit the number of endpoints returned
Limit the number of endpoints returned by example.py
using the per_page
parameter.
Open
example.py
, and then edit the GET request to match the following:# Build a GET request. url = 'https://.my.redcanary.co/openapi/v3/endpoints' headers = {'X-Api-Key': ''} params = {'per_page': '1'} # Send the request and save the response. response = requests.get(url, headers=headers, params=params)
This sets the number of requested endpoints to one.
Save
example.py
, and then run the example. The program should print a JSON object containing exactly one endpoint.