Skip to content

Commit 92b37c1

Browse files
aaron-ruby-dfariordan-docforcestumblefiend
authored
Pcx 1086 analytics graphql update getting started (#503)
* PCX-1086 Analytics GraphQL Getting started update * PCX-1086 Fix article on exploring schema * PCX-1086 Minor copy edits * PCX-1086 Edit Explore schema topic * PCX-1086 Edits for building a query in graphql * PCX-1086 Update header name * PCX-1086 Address comments and edits * PCX-1086 Incorporated comments Co-authored-by: Aine Riordan <[email protected]> Co-authored-by: Aaron Ruby <[email protected]> Co-authored-by: Jeremy Pugh <[email protected]>
1 parent 1135516 commit 92b37c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+447
-65
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: 6 additions & 6 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

@@ -90,11 +90,11 @@ When you create a new token, a confirmation page displays that includes your tok
9090

9191
To copy the token to your device's clipboard, click the **Copy** button.
9292

93-
<Aside type='note' header='Note'>
93+
<Aside type='warning' header='Warning'>
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 token displays only on the confirmation page, so copy the token and store it safely, since anyone who has the token can use it to access your data.
9696

97-
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.
97+
If you lose the token, you can [regenerate it 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

9999
</Aside>
100100

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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Create a query in GraphiQL
3+
order: 50
4+
---
5+
6+
# Create a query in GraphiQL
7+
8+
You can use a GraphQL client to build and execute queries to the GraphQL API endpoint. The example below uses the [GraphiQL](https://github.com/graphql/graphiql/tree/main/packages/graphiql#readme) client.
9+
10+
## Prerequisites
11+
12+
This article assumes that you are already familiar with [_Querying basics_](/graphql-api/getting-started/querying-basics).
13+
14+
Before you begin, [configure the API endpoint and HTTP headers](/graphql-api/getting-started/authentication/graphql-client-headers) in the GraphiQL client.
15+
16+
<Aside type='tip' header='Tip'>
17+
18+
To explore the documentation for the data sets and fields in the Cloudflare GraphQL schema, click **Docs** to open the _Documentation Explorer_ pane.
19+
20+
For an introduction, see [Explore the GraphQL schema](/graphql-api/getting-started/explore-graphql-schema).
21+
22+
</Aside>
23+
24+
## Set up a query and choose a data set
25+
26+
Click on the editing pane of GraphiQL and add this base query, replacing `zone-id` with your Cloudflare zone ID:
27+
28+
![GraphiQL base query](../../static/images/graphiql-base-query.png)
29+
30+
<Aside type='tip' header='Tip'>
31+
32+
To find the ID for a zone, log in to your Cloudflare account and click the site for which you want to obtain the zone ID. In the Cloudflare dashboard **Overview** page, scroll to the **API** section in the right sidebar, which displays your zone ID and account ID.
33+
34+
</Aside>
35+
36+
To assist query building, the GraphiQL client has word completion. Insert your cursor in the query, in this case on the line below `zones`, and start entering a value to engage the feature. For example, when you type `firewall`, a popup menu displays the data sets that return firewall information:
37+
38+
![firewall nodes](../../static/images/graphiql-word-completion.png)
39+
40+
The text at the bottom of the list displays a short description of the data that the node returns.
41+
42+
Select the data set you want to query and insert it. Either click the item in the list, or scroll using arrow keys and press <kbd>Return</kbd>. This example uses the `firewallEventsAdaptive` data set.
43+
44+
## Supply required parameters
45+
46+
Hover your mouse over a field to display a tooltip that describes the data set. In this example, hovering over the `firewallEventsAdaptive` node displays this description:
47+
48+
![GraphiQL parameters](../../static/images/graphiql-set-up-base-query.png)
49+
50+
To display information about the data set, including required parameters, click the data set name (blue text). The **Documentation Explorer** opens and displays details about the data set:
51+
52+
![GraphiQL parameters](../../static/images/graphiql-parameters.png)
53+
54+
Note that the `filter` and `limit` arguments are required, as indicated by the exclamation mark (`!`) after their type definitions (gold text). In this example, the `orderBy` argument is not required, though when used it requires a value of type `ZoneFirewallEventsAdaptiveOrderBy`.
55+
56+
To browse a list of supported filter fields, click the filter type definition (gold text) in the Documentation Explorer. In this example, the type is `ZoneFirewallEventsAdaptiveFilter_InputObject`:
57+
58+
![GraphiQL filter fields](../../static/images/graphiql-filter-fields.png)
59+
60+
This example query shows the required `filter` and `limit` arguments for `firewallEventsAdaptive`:
61+
62+
![GraphiQL filter values](../../static/images/graphiql-filter-values.png)
63+
64+
## Define the fields to return with your query
65+
66+
To browse the fields you can return with your query, hover your cursor over the dataset name in your query, and in the tooltip that displays, click the data type definition (gold text):
67+
68+
![GraphiQL parameters](../../static/images/graphiql-set-up-base-query.png)
69+
70+
**The Documentation Explorer** opens and displays a list of fields:
71+
72+
![List of fields](../../static/images/graphiql-return-fields.png)
73+
74+
To add the data fields that you want to display, type an opening brace (`{`) after the closing parenthesis for the parameters, then start typing the name of a field that you want to fetch. Use word completion to choose a field.
75+
76+
This example query returns the `action`, `datetime`, `clientRequestHTTPHost`, and `userAgent` fields:
77+
78+
![Example query with return fields](../../static/images/graphiql-query-return-field-values.png)
79+
80+
Once you have entered all the fields you want your query to return, click the **Play** button to submit the query. The response pane contains the data fetched from the Cloudflare GraphQL API endpoint.
81+
82+
![GraphiQL response](../../static/images/create-query-fw-data-set-play.png)
83+
84+
## Variable substitution
85+
86+
The GraphiQL client allows you to create variables for use in your queries.
87+
88+
To add a query variable, click the **Query Variables** pane and enter a JSON object that defines your variable.
89+
90+
This example JSON object defines a query variable for a zone ID:
91+
92+
```json
93+
{"zoneTag":"xxxxxxxxx"}
94+
```
95+
96+
To use a variable in your query, prepend the `$` character to your variable name and use it to replace the desired value. When using a variable as a value in a query, do not wrap the variable in quotes as you would for a literal value.
97+
98+
This example query uses the `zoneTag` query variable to represent the zone ID:
99+
100+
![GraphiQL query variables](../../static/images/graphiql-query-variables.png)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Use curl to query the Analytics API
3+
order: 60
4+
---
5+
6+
# Use curl to query the Analytics API
7+
8+
You can submit a [query built with the GraphiQL client](/graphql-api/getting-started/compose-graphql-query) as the payload in the `data` field of a POST request to the Analytics API.
9+
10+
The advantage of executing a request with [curl](https://curl.se/) 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+
```bash
19+
---
20+
header: Example bash script that uses curl to query Analytics API
21+
---
22+
#!/bin/bash
23+
#
24+
# This script fetches the last 24 hours of firewall events for the ZoneID passed
25+
# in as the first parameter using the global key passed in as the second parameter.
26+
######################################################################################
27+
28+
ZoneID="$1"
29+
global_key="$2"
30+
31+
#
32+
# Calculate 24 hours back and produce the start and end times in the appropriate format.
33+
back_seconds=60*60*24 # 24 hours
34+
end_epoch=$(date +'%s')
35+
let start_epoch=$end_epoch-$back_seconds
36+
start_date=$(date --date="@$start_epoch" +'%Y-%m-%dT%H:%m:%SZ')
37+
end_date=$(date --date="@$end_epoch" +'%Y-%m-%dT%H:%m:%SZ')
38+
39+
PAYLOAD='{ "query":
40+
"query {
41+
viewer {
42+
zones(filter: { zoneTag: $zoneTag }) {
43+
firewallEventsAdaptive(
44+
filter: $filter
45+
limit: 10000
46+
orderBy: [datetime_DESC, rayName_DESC]
47+
) {
48+
action,
49+
datetime,
50+
rayName,
51+
clientRequestHTTPHost,
52+
userAgent
53+
}
54+
}
55+
}
56+
}",'
57+
PAYLOAD="$PAYLOAD
58+
59+
\"variables\": {
60+
\"zoneTag\": \"$ZoneID\",
61+
\"filter\": {
62+
\"datetime_gt\": \"$start_date\",
63+
\"datetime_leq\": \"$end_date\"
64+
}
65+
}
66+
}"
67+
68+
# Run query to GraphQL API endpoint
69+
70+
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/
71+
```

0 commit comments

Comments
 (0)