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.
1
require 'net/http'
2
require 'json'
3
4
API_KEY = "qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN"
5
API_ENDPOINT = "https://api.craft.co/v1/query"
6
GRAPHQL_QUERY = "query getAlerts($first: Int!) { alerts(first: $first) { id dataset variable } }"
7
8
request_headers= { "Content-Type": "application/json", "x-craft-api-key": API_KEY }
9
request_data = {
10
"query": GRAPHQL_QUERY,
11
"variables": { "first": 3 }
12
}
13
14
response = Net::HTTP.post(URI(API_ENDPOINT), request_data.to_json, request_headers)
15
puts response.body
Copied!

Response

The above command returns JSON structured like this:
1
{
2
"data": {
3
"alerts": [
4
{
5
"id": "8da58037-d0b9-8365-75b4-6754aed4e9b0",
6
"dataset": "news",
7
"variable": "news_article"
8
},
9
{
10
"id": "8489a57b-7be9-0364-a95d-65ad84ed430a",
11
"dataset": "news",
12
"variable": "news_article"
13
},
14
{
15
"id": "8489a57b-7be9-0364-a90d-65ad84ed430a",
16
"dataset": "news",
17
"variable": "news_article"
18
},
19
...
20
]
21
}
22
}
Copied!

Read more

Last modified 5mo ago
Copy link