> 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/javascript-example.md).

# JavaScript example

Use JavaScript when you want to call Market Replay from Node.js or a serverless function.

This example uses the built-in `fetch` API.

## What this example does

It shows how to:

* authenticate with `username` and `password`
* call the minute endpoint
* parse the CSV response
* turn rows into structured objects

## Example script

```javascript
const baseUrl = "https://historical.aws.barchart.com/historical/queryminutes.ashx";

const params = new URLSearchParams({
  username: process.env.BARCHART_USERNAME || "YOUR_USERNAME",
  password: process.env.BARCHART_PASSWORD || "YOUR_PASSWORD",
  symbol: "AAPL",
  start: "200902030900",
  end: "200902031200",
  interval: "5"
});

const response = await fetch(`${baseUrl}?${params.toString()}`);
const text = await response.text();

const rows = text
  .trim()
  .split("\n")
  .filter(Boolean)
  .map((line) => {
    const [timestamp, tradingDay, open, high, low, close, volume] = line.split(",");
    return {
      timestamp,
      tradingDay,
      open: Number(open),
      high: Number(high),
      low: Number(low),
      close: Number(close),
      volume: Number(volume)
    };
  });

console.log(`Returned ${rows.length} rows`);
console.log(rows.slice(0, 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

Keep credentials out of source control.

```javascript
const params = new URLSearchParams({
  username: process.env.BARCHART_USERNAME,
  password: process.env.BARCHART_PASSWORD,
  symbol: "AAPL",
  maxrecords: "10",
  order: "desc"
});
```

{% hint style="warning" %}
Do not put credentials in browser-side code. Use JavaScript on the server side.
{% endhint %}

## 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 [Python example](/market-replay-docs/getting-started/python-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/javascript-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.
