If you want to access Analytics Engine data from within a Worker you can use fetch to access the SQL API. The API can return JSON data that is easy to interact with in JavaScript.
Authentication
In order that your Worker can authenticate with the API you will need your account ID and an API token.
Your 32 character account ID can be obtained from the Cloudflare dashboard.
An API token can also be generated in the dashboard. Refer to the SQL API docs for more information on this.
We recommend storing the account ID as an environment variable and the API token as a secret in your worker. This can be done through the dashboard or through Wrangler. Refer to the Workers documentation for more details on this.
Querying
Use the JavaScript fetch API as follows to execute a query:
The data will be returned in the format described in the FORMAT section of the API docs allowing you to extract meta information about the names and types of returned columns in addition to the data itself and a row count.
Example Worker
The following is a sample Worker which executes a query against a dataset of weather readings and displays minimum and maximum values for each city.
Environment variable setup
First the environment variables are set up with the account ID and API token.