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 <REPLACE_WITH_YOUR_KEY> with your unique API key.

import requests

API_KEY = "<REPLACE_WITH_YOUR_KEY>"
API_ENDPOINT = "https://api.craft.co/v1/query"
GRAPHQL_QUERY = """
    query getAlerts($first: Int!) { 
        alerts(first: $first) {
            id
            dataset
            variable
        }
    }
"""

request_headers= { "x-craft-api-key": API_KEY }
request_data = {
    "query": GRAPHQL_QUERY,
    "variables": { "first": 3 }
}

response = requests.post(API_ENDPOINT, json=request_data, headers=request_headers, timeout=30)
response_data = response.json()

print(response_data)

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

Last updated