This article provides the steps necessary to write and run a simple Ruby program 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 Ruby. To download Ruby, see https://www.ruby-lang.org/en/downloads/.
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://<subdomain>.my.redcanary.co/openapi/v3/endpoints")
key = "<API 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<subdomain>
with the Red Canary subdomain you want to query. - In
example.rb
, replace<API key>
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.
Comments
0 comments
Please sign in to leave a comment.