- 16 Jul 2024
- 1 Minute to read
- PDF
Ruby Example
- Updated on 16 Jul 2024
- 1 Minute to read
- PDF
Follow these necessary steps to write and run a simple Ruby program that queries the Red Canary API.
Prerequisites
Before running any code, ensure you have the following:
Create your Ruby program
Create a Ruby program called example.rb
, which fetches a list of endpoints from a subdomain.
In your working directory, create a new file called
example.rb
, and then copy the following code into it.require "net/http" # Build a GET request. uri = URI("https://.my.redcanary.co/openapi/v3/endpoints") key = "" request = Net::HTTP::GET.new(uri) request["X-API-Key"] = key # Perform the request and save the response. response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(request) } # Print the response body. puts response.body
In
example.rb
, replace with the Red Canary subdomain you want to query.In
example.rb
, replace with your API key.Save
example.rb
.
Build and run the example
Run your example with the following command.
ruby example.rb
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.rb
using the per_page
parameter.
Open
example.rb
, and then find the URL passed toURI()
.Add the following to the end of the URL:
?per_page=1
This sets the number of requested endpoints to one.
Save
example.rb
, and then run the example. The program should print a JSON object containing exactly one endpoint.