Ruby Example

Prev Next

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.

  1. 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
  2. In example.rb, replace with the Red Canary subdomain you want to query.

  3. In example.rb, replace with your API key.

  4. 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.

  1. Open example.rb, and then find the URL passed to URI().

  2. Add the following to the end of the URL:

    ?per_page=1

    This sets the number of requested endpoints to one.

  3. Save example.rb, and then run the example. The program should print a JSON object containing exactly one endpoint.