# Examples

Complete, runnable projects: https://github.com/seankarltonlee/tickermint-examples. Each reads `TICKERMINT_API_KEY` (optional for trying it) and shows the "Prices: TickerMint" link.

| Example | What it does | Plan it fits |
|---|---|---|
| [Python](https://github.com/seankarltonlee/tickermint-examples/tree/main/examples/python) | Card price and 30-day change from the command line | no key / free key |
| [JavaScript and TypeScript](https://github.com/seankarltonlee/tickermint-examples/tree/main/examples/javascript) | The same in Node 18+ with fetch | no key / free key |
| [Google Sheets](https://github.com/seankarltonlee/tickermint-examples/tree/main/examples/google-sheets) | IMPORTDATA formulas and a =TICKERMINT() Apps Script function | free key |
| [Discord bot](https://github.com/seankarltonlee/tickermint-examples/tree/main/examples/discord-bot) | discord.js /price command with 30-day history | free key (hobby), API Pro if the bot is commercial |
| [Next.js price widget](https://github.com/seankarltonlee/tickermint-examples/tree/main/examples/nextjs-price-widget) | Server component price card for a store page, key kept server side | API Pro (a store is commercial use) |

## Python

```python
import os, requests

API = "https://api.tickermint.cards"
headers = {"X-API-Key": os.environ["TICKERMINT_API_KEY"]} if os.environ.get("TICKERMINT_API_KEY") else {}

hits = requests.get(f"{API}/products/search", params={"q": "charizard ex 199/165", "game": "pokemon"},
                    headers=headers, timeout=15).json()
card = hits[0]
print(card["name"], card["set_name"], "$" + card["market"])   # prices are decimal strings
print("Prices: TickerMint", card["tickermint_url"])               # the required link back

history = requests.get(f"{API}/products/{card['product_id']}/prices", headers=headers, timeout=15).json()
for printing, days in history.items():                           # {"Holofoil": [{"snapshot_date", "low", "market"}, ...]}
    print(printing, days[-1]["snapshot_date"], days[-1]["market"])
```

## Discord bot (discord.js v14)

```js
// /price <card>: market price, 30-day change, link back
const API = "https://api.tickermint.cards";
const headers = { "X-API-Key": process.env.TICKERMINT_API_KEY };
client.on("interactionCreate", async (i) => {
  if (!i.isChatInputCommand() || i.commandName !== "price") return;
  const [card] = await (await fetch(`${API}/products/search?q=${encodeURIComponent(i.options.getString("card"))}`, { headers })).json();
  if (!card) return i.reply("No card found.");
  const hist = await (await fetch(`${API}/products/${card.product_id}/prices`, { headers })).json();
  const days = Object.values(hist)[0] ?? [];
  const first = Number(days.at(-31)?.market ?? days[0]?.market), last = Number(days.at(-1)?.market);
  const change = first ? ((last / first - 1) * 100).toFixed(1) : "n/a";
  await i.reply({ embeds: [{ title: card.name, url: card.tickermint_url,
    description: `$${card.market} (30 days: ${change}%)`, footer: { text: "Prices: TickerMint" } }] });
});
```

## Next.js price widget (App Router, server component)

```tsx
// app/components/card-price.tsx: the key stays on the server
export async function CardPrice({ productId }: { productId: number }) {
  const res = await fetch(`https://api.tickermint.cards/products/${productId}`, {
    headers: { "X-API-Key": process.env.TICKERMINT_API_KEY! },
    next: { revalidate: 3600 }, // prices change once a day
  });
  if (!res.ok) return null;
  const p = await res.json();
  return (
    <div>
      <strong>{p.name}</strong> ${p.market}
      <a href={p.tickermint_url}>Prices: TickerMint</a>
    </div>
  );
}
```

## Google Sheets

```
=IMPORTDATA("https://api.tickermint.cards/sheets/price?q=charizard%20ex%20199/165&key=YOUR_KEY")
=IMPORTDATA("https://api.tickermint.cards/sheets/price?q=517045&field=low")
=IMPORTDATA("https://api.tickermint.cards/sets/23237.csv?key=YOUR_KEY")
```

Guides on the site: https://tickermint.cards/developers/google-sheets, https://tickermint.cards/developers/python, https://tickermint.cards/developers/javascript, https://tickermint.cards/developers/for/discord-bot
