This article provides the steps necessary to write and run a simple Python script that queries the Red Canary API.
Prerequisites
Before you run any code, make sure you have the following:
- Your API key. If you don't have your API key, or you need to reset an old key, see Reset your API key in the Red Canary Help Center.
- The latest recommended version of Python. To download Python, see https://www.python.org/downloads/.
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://<subdomain>.my.redcanary.co/openapi/v3/endpoints'
headers = {'X-Api-Key': '<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<subdomain>
with the Red Canary subdomain you want to query. - In
example.py
, replace<API key>
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.
This sets the number of requested endpoints to one.
url = 'https://<subdomain>.my.redcanary.co/openapi/v3/endpoints'
headers = {'X-Api-Key': '<API key>'}
params = {'per_page': '1'}
# Send the request and save the response.
response = requests.get(url, headers=headers, params=params) - Save
example.py
, and then run the example. The program should print a JSON object containing exactly one endpoint.
Comments
0 comments
Please sign in to leave a comment.