> ## Documentation Index
> Fetch the complete documentation index at: https://dune-tables-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Export Data Out of Dune

> How to export data out of Dune

This guide provides a step-by-step process on exporting data from Dune, either through CSV downloads or the API.

## Identify the Dataset for Export

Begin by pinpointing the dataset or query result you wish to export. For instance, given the recent buzz around Friend.Tech, you might want to export the results from this query: [https://dune.com/queries/2945343](https://dune.com/queries/2945343).

## Export via CSV

* Navigate to the query interface.
* Simply click the "Export to CSV" button.

<div
  style={{
 position: "relative",
 paddingBottom: "calc(55.052083333333336% + 41px)",
 height: 0,
 width: "100%"
}}
>
  <iframe
    src="https://demo.arcade.software/ZGmdSGBEqRwUpopdF4RR?embed"
    frameBorder={0}
    loading="lazy"
    webkitallowfullscreen=""
    mozallowfullscreen=""
    allowFullScreen=""
    style={{
   position: "absolute",
   top: 0,
   left: 0,
   width: "100%",
   height: "100%",
   colorScheme: "light"
  }}
    title="Export via CSV"
  />
</div>

## Export via API

Ensure you have an API key. If not, generate one:

1. Navigate to: Settings → API.
2. Click on "Create new API key".
3. Remember to copy the entire API key before confirming.

<div
  style={{
 position: "relative",
 paddingBottom: "calc(55.052083333333336% + 41px)",
 height: 0,
 width: "100%"
}}
>
  <iframe
    src="https://demo.arcade.software/f07eudjtOJz9URF8TGa5?embed"
    frameBorder={0}
    loading="lazy"
    webkitallowfullscreen=""
    mozallowfullscreen=""
    allowFullScreen=""
    style={{
   position: "absolute",
   top: 0,
   left: 0,
   width: "100%",
   height: "100%",
   colorScheme: "light"
  }}
    title="how to generate API key"
  />
</div>

For this guide, we'll focus on exporting data using the ['get latest query result'](/api-reference/executions/endpoint/get-query-result) endpoint. Here's a Python example:

<Note>
  First, create a .env file with the API key you just created.

  ```
  DUNE_API_KEY= <insert your key>
  ```
</Note>

```py

import dotenv
import os
import json
import requests
import pandas as pd

def get_latest_query_result(query_id):
    res_df = pd.DataFrame()
    try:
        result_url = f"https://api.dune.com/api-referencev1/query/{query_id}/results"
        result_response = requests.get(result_url, headers=headers)
        print(json.loads(result_response.text))
        query_res = json.loads(result_response.text)["result"]["rows"]
        res_df = pd.DataFrame.from_dict(query_res)
    except Exception as e:
        print(f"Error retrieving result for query {query_id}: {e}")
    return res_df

dotenv.load_dotenv('path_to_env_file')  # Replace 'path_to_env_file' with your .env file path
api_key = os.getenv("DUNE_API_KEY")
query_id = 2945343
headers = {"x-dune-api-key": api_key}
latest_res = get_latest_query_result(query_id)
print(latest_res)

```

There are multiple methods to export data via the API, and it's possible to get data in various formats like JSON and CSV. You can even integrate the data with Google Sheets. For a comprehensive guide, refer to [this page](/api-reference/executions/endpoint/get-query-result).

<Tip>Access the 'get latest result' endpoint directly from the Dune UI. When viewing a query, click the API icon in the top-right corner and copy the endpoint URL.</Tip>

<div
  style={{
 position: "relative",
 paddingBottom: "calc(55.052083333333336% + 41px)",
 height: 0,
 width: "100%"
}}
>
  <iframe
    src="https://demo.arcade.software/8B1oBaaI9LjLubSVjjc9?embed"
    frameBorder={0}
    loading="lazy"
    webkitallowfullscreen=""
    mozallowfullscreen=""
    allowFullScreen=""
    style={{
   position: "absolute",
   top: 0,
   left: 0,
   width: "100%",
   height: "100%",
   colorScheme: "light"
  }}
    title="Easy access to API data export"
  />
</div>
