Getting started with Ruby

Alerts API querying example with Ruby

Request code

To query the API, you can use any HTTP client of choice or any specialized GraphQL API Clients.

Make sure to replace qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN with your unique API key.

require 'net/http'
require 'json'
API_KEY = "qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN"
API_ENDPOINT = "https://api.craft.co/v1/query"
GRAPHQL_QUERY = "query getAlerts($first: Int!) { alerts(first: $first) { id dataset variable } }"
request_headers= { "Content-Type": "application/json", "x-craft-api-key": API_KEY }
request_data = {
"query": GRAPHQL_QUERY,
"variables": { "first": 3 }
}
response = Net::HTTP.post(URI(API_ENDPOINT), request_data.to_json, request_headers)
puts response.body

Response

The above command returns JSON structured like this:

{
"data": {
"alerts": [
{
"id": "8da58037-d0b9-8365-75b4-6754aed4e9b0",
"dataset": "news",
"variable": "news_article"
},
{
"id": "8489a57b-7be9-0364-a95d-65ad84ed430a",
"dataset": "news",
"variable": "news_article"
},
{
"id": "8489a57b-7be9-0364-a90d-65ad84ed430a",
"dataset": "news",
"variable": "news_article"
},
...
]
}
}

Read more