> For the complete documentation index, see [llms.txt](https://docs.appfarm.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.appfarm.io/how-to/integrations/fetch-data-from-bigquery.md).

# Fetch data from BigQuery

### Step 1

To start this guide, you'll need a Bearer token from Google Cloud saved as a Secret. If you don't have this from before, please check out the following guide:

{% content-ref url="/pages/siFj4mf1SkaW9SxMkiTX" %}
[Retrieve a Bearer token from Google Cloud](https://docs.appfarm.io/how-to/integrations/retrieve-bearer-token-from-google-cloud.md)
{% endcontent-ref %}

### Step 2

Go to **Flows**, and select the [Flow](https://docs.appfarm.io/reference/flows.md) you want to use (or create a new one). Then, create a new action, which will be used to fetch data from BigQuery. You may name the Action "Process BigQuery". To keep the data, add an action data source for the object class you want to write to under **Action data**.

### Step 3

Inside the action Process BigQuery, add a new Action Node "Web request". Set the following properties:

* **Base URL:** `https://bigquery.googleapis.com`
* **Path:** `/bigquery/v2/projects/[bq_project_id]/queries`
* **Authorization:** Bearer token
* **Auth token:** Data-bind to `Secrets.[The Bearer Token from Google]`
* **Prefix Bearer keyword:** `true`
* **Method:** POST
* **Body Type:** Raw

Then, set the **Body Content** to the following:

```
return {
  "query": "SELECT [column] FROM [bq_project_id].[bq_dataset_name].[bq_table_name]",
  "parameterMode": "NAMED",
  "useLegacySql": false
}
```

To make the response readable for the Result Mapping in Appfarm, you need to flatten the result from BigQuery. To do so, copy/paste this code into the **Result Parser:**

```
//Flatten the result from BigQuery
function convertBQToFlattenResults(schema, rows) {

    let resultRows = []
    let result = {};
    
    function recurse (schemaCur, rowsCur, colName) {
    
        if (Array.isArray(schemaCur) && !Array.isArray(result[colName])) {
            for(let i=0, l=schemaCur.length; i<l; i++) {
                if (colName === "")
                    recurse(schemaCur[i], rowsCur.f[i], colName + schemaCur[i].name)
                else
                    recurse(schemaCur[i], rowsCur.f[i], colName + "." + schemaCur[i].name)
            }    
        }

        if (schemaCur.type && schemaCur.type === "RECORD") {
            if (schemaCur.mode !== "REPEATED") {
                let valIndex = 0
                for (let p in schemaCur.fields) {
                    if (rowsCur.v === null) {
                        recurse(schemaCur.fields[p], rowsCur, colName + "." + schemaCur.fields[p].name)
                    } else {
                        recurse(schemaCur.fields[p], rowsCur.v.f[valIndex], colName + "." + schemaCur.fields[p].name)
                    }
                    
                    valIndex++
                }
            } 
            
            if (schemaCur.mode === "REPEATED") {   
                result[colName] = [] 
                for (let x in rowsCur.v) {
                    recurse(schemaCur.fields, rowsCur.v[x], colName)
                }
            }
        } else {
            if (schemaCur.mode === "REPEATED") {
                if (rowsCur.v !== null) {
                    result[colName] = rowsCur.v.map( (value, index) => { return value.v })
                } else {
                    result[colName] = [ null ]
                }
                
            } else if (Array.isArray(result[colName])) {
                let nextRow = {} 
                for (let j in schemaCur) {
                    nextRow[colName + "." + schemaCur[j].name] = rowsCur.v.f[j].v 
                }
                result[colName].push(nextRow)
            } else {
                if (colName !== "")
                    result[colName] = rowsCur.v
            }
        }
    }

    for (let r=0, rowsCount=rows.length; r<rowsCount; r++) {
        result = {};
        recurse(schema, rows[r], "")
        resultRows.push(result)
    }

    return resultRows
}

let bqQueryApiResult = rawResponseData

let output = convertBQToFlattenResults(bqQueryApiResult.schema.fields, bqQueryApiResult.rows)

return output
```

Finally, add a new **Result Mapping.** Choose the data source you'd like to write data to, and write the column names from BigQuery into the value fields on the Object Class Properties.

### Run the action

Now everything is set up to retrieve data from BigQuery. Make sure to first retrieve the Bearer token. Then, run the action to fetch data from BigQuery. If everything is set up correctly, all the objects requested in your query will be added to the selected data source.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.appfarm.io/how-to/integrations/fetch-data-from-bigquery.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
