Getting started with JavaScript
Alerts API querying example with JavaScript

Request code

To query the API, you can use any HTTP client of choice or any specialized GraphQL API Clients, like graphql-request, urql, or Apollo.
Using a client compatible with Fetch API, the request could be issued with the following code:
Make sure to replace qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN with your unique API key.
1
const fetch = require('isomorphic-fetch');
2
3
const API_KEY = 'qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN';
4
const API_ENDPOINT = 'https://api.craft.co/v1/query';
5
const GRAPHQL_QUERY = /* GraphQL */`
6
query getAlerts($first: Int!) {
7
alerts(first: $first) {
8
id
9
dataset
10
variable
11
}
12
}
13
`;
14
15
requestHeaders = { 'Content-Type': 'application/json', 'x-craft-api-key': API_KEY };
16
requestData = { 'query': GRAPHQL_QUERY, 'variables': { 'first': 3 } };
17
18
const result = await fetch(API_ENDPOINT, {
19
method: 'POST',
20
headers: requestHeaders,
21
body: JSON.stringify(requestData),
22
});
23
24
const { data, errors } = await result.json();
25
26
console.log(data);
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