Skip to content

Commit 24f34f8

Browse files
ariordan-docforceaaron-ruby-df
authored andcommitted
PCX-1086 Analytics GraphQL Getting started update
1 parent 9eec9e6 commit 24f34f8

23 files changed

+351
-59
lines changed

products/analytics/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"cloudflare-docs-engine": "git+https://github.com/cloudflare/cloudflare-docs-engine.git"
55
},
66
"scripts": {
7-
"bootstrap": "node_modules/cloudflare-docs-engine/bin/commands.sh bootstrap",
8-
"build": "node_modules/cloudflare-docs-engine/bin/commands.sh build",
9-
"develop": "node_modules/cloudflare-docs-engine/bin/commands.sh develop",
10-
"ghactionsbootstrap": "node_modules/cloudflare-docs-engine/bin/commands.sh ghactionsbootstrap",
11-
"savechanges": "node_modules/cloudflare-docs-engine/bin/commands.sh savechanges",
12-
"serve": "node_modules/cloudflare-docs-engine/bin/commands.sh serve"
7+
"bootstrap": "sh ./node_modules/cloudflare-docs-engine/bin/commands.sh bootstrap",
8+
"build": "sh ./node_modules/cloudflare-docs-engine/bin/commands.sh build",
9+
"develop": "sh ./node_modules/cloudflare-docs-engine/bin/commands.sh develop",
10+
"ghactionsbootstrap": "sh ./node_modules/cloudflare-docs-engine/bin/commands.sh ghactionsbootstrap",
11+
"savechanges": "sh ./node_modules/cloudflare-docs-engine/bin/commands.sh savechanges",
12+
"serve": "sh ./node_modules/cloudflare-docs-engine/bin/commands.sh serve"
1313
}
1414
}
Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Authenticate with a Cloudflare API key
3-
order: 20
3+
order: 30
44
---
55

66
# Authenticate with a Cloudflare API key
@@ -9,35 +9,3 @@ API keys are unique to each Cloudflare user and used only for authentication. An
99

1010
* Learn how to [find your API Key](https://support.cloudflare.com/hc/articles/200167836) in the Cloudflare Dashboard
1111
* Learn how to [retrieve your API Key](https://api.cloudflare.com/#getting-started-requests) using the Cloudflare API
12-
13-
## Edit HTTP headers for GraphiQL authentication
14-
15-
1. Launch _GraphiQL_
16-
17-
2. Click **Edit HTTP Headers**
18-
![ ](../../../static/images/GraphiQL-edit-http-headers.png)
19-
The _Edit HTTP Headers_ window appears.
20-
![ ](../../../static/images/GraphiQL-edit-http-headers-window.png)
21-
22-
3. Click **Add Header**
23-
![ ](../../../static/images/GraphiQL-add-header.png)
24-
25-
4. Enter **X-AUTH-EMAIL** in the _Header name_ field and your email address registered with Cloudflare in the _Header value_ field, and click **Save**
26-
27-
5. Click **Add Header**
28-
29-
6. Enter **X-AUTH-KEY** in the _Header Name_ field, then paste your Global API Key in the _Header value_ field, and click **Save**
30-
![ ](../../../static/images/GraphiQL-edit-http-headers-complete.png)
31-
32-
7. Click anywhere outside the _Edit HTTP Headers_ window in _GraphiQL_ to return to the _Untitled Query 1_ tab
33-
34-
8. Enter `https://api.cloudflare.com/client/v4/graphql` in the _GraphQL Endpoint_ field
35-
![ ](../../../static/images/GraphiQL-response-pane.png)
36-
37-
<Aside type='note' header='Note'>
38-
39-
The right-side response pane is empty when you enter your information correctly. An error displays when there are problems with your header credentials.
40-
41-
</Aside>
42-
43-
Now that you have configured authentication with a Cloudflare API key, you are ready to run queries using _GraphiQL_.

products/analytics/src/content/graphql-api/getting-started/authentication/api-token-auth.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Configure an Analytics API token
3-
order: 30
3+
order: 20
44
---
55

66
# Configure an Analytics API token
@@ -48,7 +48,7 @@ To configure a custom token, follow these steps:
4848

4949
![Create Custom Token page](../../../static/images/create-custom-api-token.png)
5050

51-
1. Use the **Token name** text input to enter a descriptive name for your token.
51+
1. Enter a descriptive name for your token in the **Token name** text input field.
5252

5353
1. To configure access to the GraphQL Analytics API, use the **Permissions** drop-down lists. To set permissions for the GraphQL Analytics API, select _Analytics_ from the second drop-down list.
5454

@@ -70,7 +70,7 @@ To configure a custom token, follow these steps:
7070

7171
1. Click **Continue to summary**.
7272

73-
The next section of this walk through shows you how to [review and test your API token](#review-and-test-your-api-token).
73+
The next section of this walkthrough shows you how to [review and test your API token](#review-and-test-your-api-token).
7474

7575
## Review and create your API token
7676

@@ -92,7 +92,7 @@ To copy the token to your device's clipboard, click the **Copy** button.
9292

9393
<Aside type='note' header='Note'>
9494

95-
The confirmation page is the only time that you can see the token, so make sure you store it safely, since anyone who has token can use it to access your data.
95+
The confirmation page is the only time that you can see the token, so make sure you store it safely, since anyone who has the token can use it to access your data.
9696

9797
If you lose the secret, you can [regenerate the token from the API Tokens page](https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys#12345681), so that you do not have to configure all the permissions again.
9898

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Configure GraphQL client endpoint and HTTP headers
3+
order: 40
4+
---
5+
6+
# Configure GraphQL client endpoint and HTTP headers
7+
8+
1. Launch _GraphiQL_.
9+
1. Click **Edit HTTP Headers**.
10+
![Click Edit HTTP Headers](../../../static/images/GraphiQL-edit-http-headers.png)
11+
The _Edit HTTP Headers_ window appears.
12+
![Edit HTTP Headers Window](../../../static/images/GraphiQL-edit-http-headers-window.png)
13+
1. Click **Add Header**.
14+
![Click Add Header](../../../static/images/GraphiQL-add-header.png)
15+
1. Enter **X-AUTH-EMAIL** in the _Header name_ field and your email address registered with Cloudflare in the _Header value_ field, and click **Save**.
16+
1. To configure authentication, click **Add Header**. You can use Cloudflare Analytics API token authentication (recommended) or Cloudflare API key authentication.
17+
* **Token authentication**:
18+
Enter **Authorization** in the _Header Name_ field, and enter **Bearer {your-analytics-token}** in the _Header value_ field, then click **Save**.
19+
![HTTP Headers](../../../static/images/GraphiQL-edit-http-headers-token.png)
20+
* **Key authentication**:
21+
Enter **X-AUTH-KEY** in the _Header Name_ field, and paste your Global API Key in the _Header value_ field, then click **Save**.
22+
![HTTP Headers](../../../static/images/GraphiQL-edit-http-headers-complete.png)
23+
1. Click anywhere outside the _Edit HTTP Headers_ window in _GraphiQL_ to close it and return to the main _GraphiQL_ display.
24+
1. Enter `https://api.cloudflare.com/client/v4/graphql` in the _GraphQL Endpoint_ field
25+
![Edit GraphQL Endpoint](../../../static/images/GraphiQL-response-pane.png)
26+
27+
<Aside type='note' header='Note'>
28+
29+
The right-side response pane is empty when you enter your information correctly. An error displays when there are problems with your header credentials.
30+
31+
</Aside>
32+
33+
Now that you have configured authentication with a Cloudflare API key, you are ready to run queries using _GraphiQL_.

products/analytics/src/content/graphql-api/getting-started/authentication/index.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ order: 10
55

66
# Authentication
77

8-
Cloudflare separates service configuration by zone. When there are multiple accounts each with many zones, it is important to restrict GraphQL Analytics API access to only those account and zone resources that are relevant for the task at hand.
8+
Cloudflare separates service configuration by zone. When there are multiple accounts, each with many zones, it is important to restrict GraphQL Analytics API access to only those account and zone resources that are relevant for the task at hand.
99

10-
To secure access your GraphQL Analytics data, use a Cloudflare API key or token to authenticate an API request.
10+
To secure access to your GraphQL Analytics data, use a Cloudflare API key or token to authenticate an API request.
1111

1212
This table outlines the differences between Cloudflare API keys and tokens:
1313

@@ -21,20 +21,20 @@ This table outlines the differences between Cloudflare API keys and tokens:
2121
</tr>
2222
</thead>
2323
<tbody>
24+
<tr>
25+
<td><a href='https://developers.cloudflare.com/api/tokens/create'>API Tokens</a></td>
26+
<td>Cloudflare recommends API Tokens as the preferred way to interact with Cloudflare APIs. You can configure the scope of tokens to limit access to account and zone resources, and you can define the Cloudflare APIs to which the token authorizes access.</td>
27+
</tr>
2428
<tr>
2529
<td><a href='https://developers.cloudflare.com/api/keys'>API Keys</a></td>
2630
<td><p>Unique to each Cloudflare user and used only for authentication. API keys do not authorize access to accounts or zones.</p>
2731
<p>Use the Global API Key for authentication. Only use the Origin CA Key when you create origin certificates through the API.</p></td>
2832
</tr>
29-
<tr>
30-
<td><a href='https://developers.cloudflare.com/api/tokens/create'>API Tokens</a></td>
31-
<td>Cloudflare recommends API Tokens as the preferred way to interact with Cloudflare APIs. You can configure the scope of tokens to limit access to account and zone resources, and you can define the Cloudflare APIs to which the token authorizes access.</td>
32-
</tr>
3333
</tbody>
3434
</table>
3535

3636
</TableWrap>
3737

38-
To find and retrieve API keys, as well as edit HTTP headers for authentication in GraphiQL, see [_Authenticate with a Cloudflare API key_](/graphql-api/getting-started/authentication/api-key-auth/).
39-
4038
To create and configure GraphQL Analytics API tokens, see [_Configure an Analytics API token_](/graphql-api/getting-started/authentication/api-token-auth/).
39+
40+
To find and retrieve API keys, as well as edit HTTP headers for authentication in GraphiQL, see [_Authenticate with a Cloudflare API key_](/graphql-api/getting-started/authentication/api-key-auth/).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Create a query in a GraphQL client
3+
order: 50
4+
---
5+
6+
# Create a query in a GraphQL client
7+
8+
You can use a GraphQL client to build and execute queries to the GraphQL API endpoint. The example below uses the GraphiQL client.
9+
10+
Before you begin, configure the API endpoint and HTTP headers in the GraphQL client.
11+
12+
Click on the editing pane of GraphiQL and add the following base query:
13+
14+
```json
15+
query {
16+
viewer {
17+
zones(filter: { zoneTag: "{zone-id}"}) {
18+
19+
}
20+
}
21+
}
22+
```
23+
24+
To explore the documentation for the nodes and fields in the Cloudflare GraphQL schema, click **Docs** to open the _Documentation Explorer_ pane.
25+
26+
The GraphiQL client has auto completion, to assist query building. Place your cursor on the line under "zones" and start typing a word: a popup window appears, listing all the possible fields that contain that word. For example, if you type `firewall`, the popup displays the fields that return firewall information:
27+
28+
![firewall nodes](../../static/images/create-query-fw-data-set.png)
29+
30+
The text at the bottom of the list displays a short description of the data that the node returns.
31+
32+
Select the field you want to use and insert it into your query: either click the item in the list, or scroll using arrow keys and hit return.
33+
34+
Hover your mouse over a field to open a popup window showing the documentation for that field. The following popup window appears when you hover over the `firewallEventsAdaptive` field:
35+
36+
![Autocomplete popup](../../static/images/create-query-fw-data-set-description.png)
37+
38+
Click on the node name (blue text) to display information about the field, including the required parameters, in the _Documentation Explorer_ pane.
39+
40+
Click on the type definition (gold text) to display information about the field type in the _Documentation Explorer_ pane.
41+
42+
Add the required parameters for the field in the editing pane: type the open parenthesis character, `(`, after the field name. GraphiQL inserts the closing parenthesis and opens a popup window with a list of required parameters:
43+
44+
![GraphiQL filter popup](../../static/images/create-query-fw-data-set-filter.png)
45+
46+
Configure the required parameters.
47+
48+
To add the data fields that you want to display, type the opening bracket `{` after the closing parenthesis containing parameters. GraphiQL automatically inserts the closing bracket. For readability, hit the return key between the brackets.
49+
50+
Click the field type definition to display the list of fields in in the _Documentation Explorer_ pane:
51+
52+
![List of fields](../../static/images/create-query-fw-data-set-field-type.png)
53+
54+
Start typing the name of a field that you want to fetch, to open an auto-complete list. Once you have entered all the fields you want, click the **Play** button to submit the query. The response pane contains the data fetched from the Cloudflare GraphQL API endpoint.
55+
56+
The GraphiQL client allows you to create variables to use in the query.
57+
In the following example, the _Query variables_ pane contains a variable that represents a zone ID:
58+
59+
```json
60+
{"zoneTag":"xxxxxxxxx"}
61+
```
62+
63+
Use `$zoneTag` to refer to the variable in a query.
64+
65+
![GraphiQL response](../../static/images/create-query-fw-data-set-play.png)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: Use curl to query the GraphQL API
3+
order: 60
4+
---
5+
6+
# Use CURL to query the GraphQL API
7+
8+
You can submit a query you built up in the GraphiQL client as a payload in the `data` field of a POST request to the GraphQL API.
9+
10+
The advantage of executing the request as a CURL request is that you can redirect the response to a file, and execute other post processing methods.
11+
12+
The GraphQL endpoint requires valid JSON, so you must pass the query as the _value_ part of a JSON _key/value_ pair with a key named "query".
13+
14+
Pass the list of variables in another JSON _key:value_ pair with a key named "variables".
15+
16+
The script below returns the firewall events in one zone over the last 24 hours:
17+
18+
```
19+
#!/bin/bash
20+
#
21+
# This script fetches the last 24 hours of firewall events for the ZoneID passed
22+
# in as the first parameter using the global key passed in as the second parameter.
23+
######################################################################################
24+
25+
ZoneID="$1"
26+
global_key="$2"
27+
28+
#
29+
# Calculate 24 hours back and produce the start and end times in the appropriate format.
30+
back_seconds=60*60*24 # 24 hours
31+
end_epoch=$(date +'%s')
32+
let start_epoch=$end_epoch-$back_seconds
33+
start_date=$(date --date="@$start_epoch" +'%Y-%m-%dT%H:%m:%SZ')
34+
end_date=$(date --date="@$end_epoch" +'%Y-%m-%dT%H:%m:%SZ')
35+
36+
PAYLOAD='{ "query":
37+
"query {
38+
viewer {
39+
zones(filter: { zoneTag: $zoneTag }) {
40+
firewallEventsAdaptive(
41+
filter: $filter
42+
limit: 10000
43+
orderBy: [datetime_DESC, rayName_DESC]
44+
) {
45+
action,
46+
datetime,
47+
rayName,
48+
clientRequestHTTPHost,
49+
userAgent
50+
}
51+
}
52+
}
53+
}",'
54+
PAYLOAD="$PAYLOAD
55+
56+
\"variables\": {
57+
\"zoneTag\": \"$ZoneID\",
58+
\"filter\": {
59+
\"datetime_gt\": \"$start_date\",
60+
\"datetime_leq\": \"$end_date\"
61+
}
62+
}
63+
}"
64+
65+
# Run query to GraphQL API endpoint
66+
67+
curl -s -X POST -H "Content-Type: application/json" -H "X-Auth-Email: $Email" -H X-Auth-Key: $global_key --data "$(echo $PAYLOAD)" https://api.cloudflare.com/client/v4/graphql/
68+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Explore the GraphQL schema
3+
order: 40
4+
---
5+
6+
# Explore the GraphQL schema in the GraphQL client
7+
8+
You can explore the schema of the Coudflare GraphQL endpoint in the the GraphQL client. The examples below use the GraphiQL client.
9+
10+
Before you begin, configure the API endpoint and HTTP headers in the GraphQL client.
11+
12+
Click **Docs** to open the _Documentation Explorer_ pane.
13+
14+
A list of available nodes displays. The nodes in the list follow this syntax:
15+
16+
```
17+
node-name: node-type-definition
18+
```
19+
20+
Click on the _type definition_ of a node to view the fields that it provides. The _Documentation Explorer_ pane also displays descriptions of the nodes.
21+
22+
When you first open the _Documentation Explorer_ pane, the **mutation** and **query** nodes display:
23+
24+
![Mutation and query nodes](../../static/images/docs-query.png)
25+
26+
* **query** is the name of the node.
27+
* **Query** is the type definition of the node.
28+
29+
Click the **Query** type definition. The _Documentation Explorer_ panel displays the fields that the **query** node provides: **cost** and **viewer**.
30+
31+
![Cost and viewer fields](../../static/images/docs-viewer.png)
32+
33+
Click on the type definition of **viewer** to list the fields of the **viewer** node. The **viewer** node fields include nodes that allow you to query **accounts** or **zones** data:
34+
35+
![viewer fields](../../static/images/docs-zone-filter.png)
36+
37+
The **accounts** and **zones** nodes take arguments to specify what data set to query.
38+
For the **zones** node you can provide a **filter** of **ZoneFilter_InputObject** type. To view the fields to specify in the filter, click **ZoneFilter_InputObject**.
39+
40+
To limit the amount of search results that the query returns, click **limit**.
41+
42+
To view a list of the fields within a zone, click on the **zones** type definition after the colon in the **zones** field:
43+
44+
![Zones type definition](../../static/images/docs-zone.png)
45+
46+
A list of _search nodes_ appears, with a brief description of their behavior and a list of valid arguments.
47+
Arguments that end with an exclamation mark ("!") are required.
48+
49+
![Search nodes](../../static/images/docs-fw-data-set.png)
50+
51+
Refer to [Data Sets (tables)](/graphql-api/features/data-sets) for details on nomenclature and behavior of these nodes.
52+
53+
To view the fields that a node provides, click on its type definition. If you click on the **ZoneFirewallEventsAdaptive** type definition in the **firewallEventsAdaptive** node, a list of fields is displayed:
54+
55+
![ZoneFirewallEventsAdaptive type definition](../../static/images/docs-fw-fields-list.png)

products/analytics/src/content/graphql-api/getting-started/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ Use these articles to get started with the Cloudflare GraphQL Analytics API:
99

1010
* [_Authentication_](/graphql-api/getting-started/authentication) walks you through the options and the steps required to successfully set up your access to Cloudflare API.
1111
* [_Querying basics_](/graphql-api/getting-started/querying-basics) brings you simple query examples for you to start exploring the GraphQL Analytics API.
12+
* [_Explore the GraphQL schema in the GraphQL client_](/graphql-api/getting-started/explore-graphql-schema/) introduces the introspective documentation in the GraphQL API.
13+
* [_Create a query in a GraphQL client_](/graphql-api/getting-started/compose-graphql-query/) describes how to build and run a query against the Cloudflare GraphQL API in the GraphiQL client.
14+
* [_Use curl to query the GraphQL API_](/graphql-api/getting-started/execute-graphql-query/) walks you through running a query against the Cloudflare GraphQL API from the command line.
1215

1316
For examples of how to build your own GraphQL Analytics dashboard and query specific information, such as Firewall and Workers events, see [_Tutorials_](/graphql-api/tutorials).

0 commit comments

Comments
 (0)