Getting started with Python
Alerts API querying example with Python

Request code

To query the API, you can use any HTTP client of choice or any specialized GraphQL API Client.
Make sure to replace qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN with your unique API key.
1
import requests
2
3
API_KEY = "qfNfdijpFhbhPhA7j2ZxvtEGkfv8DftTtmTEbnWN"
4
API_ENDPOINT = "https://api.craft.co/v1/query"
5
GRAPHQL_QUERY = """
6
query getAlerts($first: Int!) {
7
alerts(first: $first) {
8
id
9
dataset
10
variable
11
}
12
}
13
"""
14
15
request_headers= { "x-craft-api-key": API_KEY }
16
request_data = {
17
"query": GRAPHQL_QUERY,
18
"variables": { "first": 3 }
19
}
20
21
response = requests.post(API_ENDPOINT, json=request_data, headers=request_headers, timeout=30)
22
response_data = response.json()
23
24
print(response_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