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 MarketDataPromise that resolves to decoded records by default, or rejects with a MarketDataClientError subclass on failure. 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,
): MarketDataPromise<StockPrice[] | StockPriceHuman[]>
// Object form
prices<P>(
params: P & { symbols: string | string[] },
): MarketDataPromise<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
-
MarketDataPromise<StockPrice[] | StockPriceHuman[] | Blob>A Promise that resolves to the prices data in the requested format, or rejects with a
MarketDataClientErrorsubclass on failure. Handle withawait+try/catch.
- Single Symbol
- Multiple Symbols
- Human Readable
- Raw JSON
- CSV
import { MarketDataClient } from "@marketdata/sdk";
const client = new MarketDataClient();
try {
const prices = await client.stocks.prices("AAPL");
console.log(prices[0].mid);
} catch (error) {
console.error(error);
}
import { MarketDataClient } from "@marketdata/sdk";
const client = new MarketDataClient();
try {
const prices = await client.stocks.prices(["AAPL", "MSFT", "GOOGL"]);
for (const p of prices) {
console.log(`${p.symbol}: ${p.mid}`);
}
} catch (error) {
console.error(error);
}
import { MarketDataClient } from "@marketdata/sdk";
const client = new MarketDataClient();
try {
const prices = await client.stocks.prices("AAPL", { human: true });
for (const p of prices) {
console.log(`${p.Symbol}: mid=${p.Mid}, change=${p.Change_Percent}`);
}
} catch (error) {
console.error(error);
}
import { MarketDataClient, OutputFormat } from "@marketdata/sdk";
const client = new MarketDataClient();
try {
const json = await client.stocks.prices(["AAPL", "MSFT"], {
outputFormat: OutputFormat.JSON,
});
console.log(json);
} catch (error) {
console.error(error);
}
import { MarketDataClient, OutputFormat } from "@marketdata/sdk";
const client = new MarketDataClient();
const csv = await client.stocks.prices(["AAPL", "MSFT"], {
outputFormat: OutputFormat.CSV,
});
// Save the Blob to disk (Node.js)
const filename = await csv.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.