> For the complete documentation index, see [llms.txt](https://developer.barchart.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.barchart.com/market-replay-docs/getting-started/python-example.md).

# Python example

Use Python when you want a quick way to request Market Replay data and turn the response into structured rows.

This example uses the standard library only.

## What this example does

It shows how to:

* authenticate with `username` and `password`
* call the minute endpoint
* parse the CSV response
* work with rows as dictionaries

## Example script

```python
import csv
import io
import urllib.parse
import urllib.request

BASE_URL = "https://historical.aws.barchart.com/historical/queryminutes.ashx"

params = {
    "username": "YOUR_USERNAME",
    "password": "YOUR_PASSWORD",
    "symbol": "AAPL",
    "start": "200902030900",
    "end": "200902031200",
    "interval": "5",
}

url = f"{BASE_URL}?{urllib.parse.urlencode(params)}"

with urllib.request.urlopen(url) as response:
    text = response.read().decode("utf-8")

reader = csv.reader(io.StringIO(text))
rows = []
for row in reader:
    rows.append(
        {
            "timestamp": row[0],
            "trading_day": row[1],
            "open": row[2],
            "high": row[3],
            "low": row[4],
            "close": row[5],
            "volume": row[6],
        }
    )

print(f"Returned {len(rows)} rows")
print(rows[:3])
```

## Sample output shape

Minute data rows use this format:

```
YYYY-MM-DD HH:MM,TRADING_DAY,OPEN,HIGH,LOW,CLOSE,VOLUME
```

## Use environment variables

For real usage, read credentials from environment variables instead of hard-coding them.

```python
import csv
import io
import os
import urllib.parse
import urllib.request

BASE_URL = "https://historical.aws.barchart.com/historical/queryticks.ashx"

params = {
    "username": os.environ["BARCHART_USERNAME"],
    "password": os.environ["BARCHART_PASSWORD"],
    "symbol": "AAPL",
    "maxrecords": "10",
    "order": "desc",
}

url = f"{BASE_URL}?{urllib.parse.urlencode(params)}"

with urllib.request.urlopen(url) as response:
    text = response.read().decode("utf-8")

for line in text.splitlines()[:3]:
    print(line)
```

## Change the endpoint

Swap the request URL and parsing logic based on the dataset:

* `queryticks.ashx` for tick data
* `queryminutes.ashx` for minute bars
* `queryeod.ashx` for end-of-day bars
* `queryevents.ashx` for splits, dividends, and earnings

## Next steps

* Start with [Authentication](/market-replay-docs/reference/authentication.md)
* Use [Quickstart](/market-replay-docs/getting-started/quickstart.md) for the first request flow
* Compare with [JavaScript example](/market-replay-docs/getting-started/javascript-example.md)
* Copy from [curl examples](/market-replay-docs/getting-started/curl-examples.md)
* Use [Query parameter cheat sheet](/market-replay-docs/reference/query-parameter-cheat-sheet.md)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developer.barchart.com/market-replay-docs/getting-started/python-example.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
