import { createClient, RequestError } from "@parlayx/sdk";
const client = createClient({
keyId: process.env.PARLAYX_KEY_ID!,
privateKeyHex: process.env.PARLAYX_PRIVATE_KEY_HEX!,
});
try {
/* Discovery is a chain: sports, then competitions, then events, then the
markets on an event. */
const { competitions } = await client.listCompetitions({ sport: "spt_baseball" });
const competition = competitions.find((c) => c.id === "cmp_mlb") ?? competitions[0];
if (!competition) throw new Error("no competition found");
const { events } = await client.listEvents({ competitionId: competition.id });
const event = events[0];
if (!event) throw new Error("no events found");
/* An outcome carries one listing per venue that quotes it. Only an
`executable` listing can be ordered. */
const { markets } = await client.listEventMarkets(event.id);
const listing = markets
.flatMap((market) => market.outcomes)
.flatMap((outcome) => outcome.listings)
.find((l) => l.venue === "KALSHI" && l.executable);
if (listing?.venue !== "KALSHI") throw new Error("no executable Kalshi listing found");
const { orderId } = await client.kalshi.submitOrder({
ticker: listing.ticker,
side: listing.side,
action: "BUY",
count: 1,
priceCents: 45,
timeInForce: { type: "GTC" },
});
const order = await client.kalshi.getOrder(orderId);
console.log("order status:", order.status);
} catch (error) {
if (error instanceof RequestError) {
console.error(`API error ${error.status} (${error.code ?? "unknown"}): ${error.message}`);
} else {
throw error;
}
}