Prices
Retrieve stock prices for any supported stock symbol.
Making Requests
Use the prices() method on the stocks resource to fetch stock prices. The method returns a MarketDataResult which resolves to decoded records by default. The output format is controlled by the outputFormat parameter (or MARKETDATA_OUTPUT_FORMAT env var):
| Output Format | Result Payload | Description |
|---|---|---|
| internal (default) | StockPrice[] or StockPriceHuman[] | Array of decoded price records, one per symbol. |
| json | Raw JSON object | The compressed, array-keyed response object as returned by the API. |
| csv | Blob | CSV payload. Use .save(filename) on the result to write it to disk. |
prices
// Positional form
prices<P>(
symbols: string | string[],
params?: P,
): MarketDataResult<StockPrice[] | StockPriceHuman[]>
// Object form
prices<P>(
params: P & { symbols: string | string[] },
): MarketDataResult<StockPrice[] | StockPriceHuman[]>
Fetches stock prices for one or more symbols. The return type is narrowed automatically:
- If
human: true(oruseHumanReadable: true) → payload isStockPriceHuman[] - If
outputFormat: "csv"→ payload isBlob - Otherwise → payload is
StockPrice[]
Parameters
-
symbols(string | string[])A single symbol string or an array of symbol strings for which to fetch prices.
-
outputFormat(optional)The format of the returned data. See Settings for details. Also accepts the alias
format. -
dateFormat(optional)The date format to use in the response. Also accepts the alias
dateformat. See Settings for details. -
columns(optional)Specify which columns to include in the response. See Settings for details.
-
addHeaders(optional)Whether to include headers in CSV output. Also accepts the alias
headers. See Settings for details. -
useHumanReadable(optional)Whether to use human-readable field names. Also accepts the alias
human. See Settings for details. -
mode(optional)The data mode to use (
"live","cached","delayed"). See Settings for details.
Returns
-
MarketDataResult<StockPrice[] | StockPriceHuman[] | Blob>A Result wrapping the prices data in the requested format. Handle success and failure with
.match(),.isOk()/.isErr(), or any other neverthrow method.
- Single Symbol
- Multiple Symbols
- Human Readable
- Raw JSON
- CSV
import { MarketDataClient } from "marketdata-sdk-js";
const client = new MarketDataClient();
const result = await client.stocks.prices("AAPL");
result.match(
(prices) => console.log(prices[0].mid),
(error) => console.error(error.message),
);
import { MarketDataClient } from "marketdata-sdk-js";
const client = new MarketDataClient();
const result = await client.stocks.prices(["AAPL", "MSFT", "GOOGL"]);
result.match(
(prices) => {
for (const p of prices) {
console.log(`${p.symbol}: ${p.mid}`);
}
},
(error) => console.error(error.message),
);
import { MarketDataClient } from "marketdata-sdk-js";
const client = new MarketDataClient();
const result = await client.stocks.prices("AAPL", { human: true });
result.match(
(prices) => {
for (const p of prices) {
console.log(`${p.Symbol}: mid=${p.Mid}, change=${p.Change_Percent}`);
}
},
(error) => console.error(error.message),
);
import { MarketDataClient, OutputFormat } from "marketdata-sdk-js";
const client = new MarketDataClient();
const result = await client.stocks.prices(["AAPL", "MSFT"], {
outputFormat: OutputFormat.JSON,
});
result.match(
(json) => console.log(json),
(error) => console.error(error.message),
);
import { MarketDataClient, OutputFormat } from "marketdata-sdk-js";
const client = new MarketDataClient();
const result = await client.stocks.prices(["AAPL", "MSFT"], {
outputFormat: OutputFormat.CSV,
});
// Save the Blob to disk (Node.js)
const filename = await result.save("prices.csv");
console.log(`CSV saved to: ${filename}`);
StockPrice
interface StockPrice {
s?: string;
symbol: string;
mid: number;
change: number;
changepct: number;
updated: number;
}
StockPrice represents a single stock price, with short machine-readable field names.
Properties
s(string, optional): Status indicator ("ok"for successful responses).symbol(string): The stock symbol.mid(number): The mid price calculated between the ask and bid.change(number): The price change.changepct(number): The fractional price change (e.g.0.0124for +1.24%).updated(number): Unix timestamp when the price was last updated.
StockPriceHuman
interface StockPriceHuman {
s?: string;
Symbol: string;
Mid: number;
Change_Price: number;
Change_Percent: number;
Date: number;
}
StockPriceHuman represents a stock price in human-readable format with capitalized, snake-case field names. Returned when human: true (or useHumanReadable: true) is set.
Properties
Symbol(string): The stock symbol.Mid(number): The mid price.Change_Price(number): The price change.Change_Percent(number): The percent change.Date(number): Unix timestamp of the update.