{
  "author": {
    "address": "0x425E78375336d1938cf0eaE5c40aa86CbE1C2eBB",
    "signature": "9e08cdd5a6fc1d927a33a481a2e49687786068e84ecec92ff373d1a4fa96fec47011074987f5f26d9c878913a8859c32dd1be679ed6f173ff82be1abc16bc6001b"
  },
  "available_to_orchestrator": true,
  "changelog": "First version: daily TCGplayer Pokémon price snapshot with a running history file, day-over-day changes, 30-day low/high/average, and top gainers and losers.",
  "content_hash": "sha256:d1fbc258b0d44cf76f7b3e1a0cf7ef01be2048f6b596d263b0388a8fcc1b3855",
  "created_at": "2026-10-07T19:39:15Z",
  "format": "osaurus.template",
  "format_version": 1,
  "id": "46A53F34-1B51-46DD-A495-8C55FB2A8EEC",
  "kind": "workflow",
  "name": "TCGplayer Pokémon Price Tracker",
  "requirements": {
    "knowledge": [],
    "mcp_servers": [],
    "models": [],
    "network": [
      "mp-search-api.tcgplayer.com"
    ],
    "notes": {},
    "permissions": [],
    "plugins": [],
    "secrets": [],
    "tools": [
      "file_write",
      "file_read"
    ]
  },
  "summary": "Snapshots Pokémon product prices from TCGplayer search, stores daily price history locally, and returns current prices with changes and 30-day trends.",
  "version": 1,
  "workflow": {
    "edges": [
      {
        "from": "fetch_prices",
        "to": "save_snapshot"
      },
      {
        "from": "save_snapshot",
        "to": "load_history"
      },
      {
        "from": "load_history",
        "to": "compare"
      }
    ],
    "examples": [
      {
        "input": {
          "history_file": "tcgplayer/pokemon-price-history.jsonl",
          "pages": 1,
          "search_text": ""
        },
        "name": "Daily snapshot, page 1"
      }
    ],
    "input_schema": {
      "additionalProperties": false,
      "properties": {
        "history_file": {
          "default": "tcgplayer/pokemon-price-history.jsonl",
          "description": "File (in the working folder) where the daily price history is kept. Use the same file every day.",
          "title": "History file",
          "type": "string"
        },
        "pages": {
          "default": 1,
          "description": "How many search result pages to snapshot (24 products per page, like the website grid).",
          "title": "Pages to track",
          "type": "integer"
        },
        "search_text": {
          "default": "",
          "description": "Optional search words to narrow the Pokémon results (leave blank for the same list as the website).",
          "title": "Search words",
          "type": "string"
        }
      },
      "required": [],
      "type": "object",
      "x-order": [
        "pages",
        "search_text",
        "history_file"
      ]
    },
    "model_slots": [],
    "nodes": [
      {
        "code": {
          "network": [
            "mp-search-api.tcgplayer.com"
          ],
          "secrets": [],
          "source": "async function run(input, ctx) {\n  const pages = Math.max(1, Math.min(20, parseInt(input.pages || 1, 10) || 1));\n  const q = encodeURIComponent((input.search_text || '').trim());\n  const size = 24;\n  const products = [];\n  const seen = {};\n  for (let p = 0; p < pages; p++) {\n    const body = {\n      algorithm: 'sales_dismax', from: p * size, size: size,\n      filters: { term: { productLineName: ['pokemon'] }, range: {}, match: {} },\n      listingSearch: { context: { cart: {} }, filters: { term: { sellerStatus: 'Live', channelId: 0 }, range: { quantity: { gte: 1 } }, exclude: { channelExclusion: 0 } } },\n      context: { cart: {}, shippingCountry: 'US', userProfile: {} },\n      settings: { useFuzzySearch: true, didYouMean: {} },\n      sort: {}\n    };\n    const res = await ctx.fetch('https://mp-search-api.tcgplayer.com/v1/search/request?q=' + q + '&isList=false', {\n      method: 'POST',\n      headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Origin': 'https://www.tcgplayer.com', 'Referer': 'https://www.tcgplayer.com/', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15' },\n      body: JSON.stringify(body)\n    });\n    if (!res.ok) throw new Error('TCGplayer search returned HTTP ' + res.status + ': ' + (await res.text()).slice(0, 300));\n    const data = await res.json();\n    const items = (((data.results || [])[0]) || {}).results || [];\n    ctx.log('page', p + 1, 'items', items.length);\n    for (const it of items) {\n      const id = Math.round(Number(it.productId));\n      if (!id || seen[id]) continue;\n      seen[id] = true;\n      const num = v => (v === null || v === undefined || v === '' || isNaN(Number(v))) ? null : Math.round(Number(v) * 100) / 100;\n      const attrs = it.customAttributes || {};\n      const slug = ('pokemon ' + (it.setUrlName || it.setName || '') + ' ' + (it.productUrlName || it.productName || '')).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');\n      const ptype = it.productTypeName || attrs.productTypeName || (attrs.number || it.rarityName ? 'Cards' : 'Sealed Products');\n      products.push({\n        product_id: id,\n        name: String(it.productName || ''),\n        set_name: String(it.setName || ''),\n        number: attrs.number ? String(attrs.number) : null,\n        rarity: it.rarityName ? String(it.rarityName) : null,\n        product_type: String(ptype),\n        market_price: num(it.marketPrice),\n        lowest_price: num(it.lowestPrice),\n        median_price: num(it.medianPrice),\n        listings: (it.totalListings === null || it.totalListings === undefined) ? null : Math.round(Number(it.totalListings)),\n        url: 'https://www.tcgplayer.com/product/' + id + '/' + slug\n      });\n    }\n    if (items.length < size) break;\n  }\n  if (products.length === 0) throw new Error('No products returned by TCGplayer search');\n  const now = new Date();\n  const snapshot_date = now.toISOString().slice(0, 10);\n  const captured_at = now.toISOString();\n  const jsonl = products.map(p => JSON.stringify({ d: snapshot_date, t: captured_at, id: p.product_id, name: p.name, set: p.set_name, mp: p.market_price, lp: p.lowest_price, md: p.median_price, n: p.listings })).join('\\n') + '\\n';\n  return { snapshot_date, product_count: products.length, products, jsonl, tail_lines: Math.max(2000, products.length * 60) };\n}",
          "timeout": 30
        },
        "id": "fetch_prices",
        "input": {
          "pages": "{{input.pages}}",
          "search_text": "{{input.search_text}}"
        },
        "intent": "Fetch current Pokémon prices from TCGplayer",
        "kind": "code",
        "name": "Fetch current Pokémon prices from TCGplayer",
        "output_schema": {
          "properties": {
            "jsonl": {
              "type": "string"
            },
            "product_count": {
              "type": "integer"
            },
            "products": {
              "items": {
                "type": "object"
              },
              "type": "array"
            },
            "snapshot_date": {
              "type": "string"
            },
            "tail_lines": {
              "type": "integer"
            }
          },
          "required": [
            "snapshot_date",
            "product_count",
            "products",
            "jsonl",
            "tail_lines"
          ],
          "type": "object"
        },
        "retry": {
          "backoff_seconds": 10,
          "max_attempts": 3
        },
        "timeout": 120
      },
      {
        "id": "save_snapshot",
        "input": {
          "content": "{{nodes.fetch_prices.output.jsonl}}",
          "mode": "append",
          "path": "{{input.history_file}}"
        },
        "intent": "Save today's snapshot",
        "kind": "tool",
        "name": "Add today's prices to the history file",
        "output_schema": {},
        "retry": {
          "backoff_seconds": 2,
          "max_attempts": 2
        },
        "timeout": 120,
        "tool": {
          "name": "file_write"
        }
      },
      {
        "id": "load_history",
        "input": {
          "path": "{{input.history_file}}",
          "tail_lines": "{{nodes.fetch_prices.output.tail_lines}}"
        },
        "intent": "Load last 30 days of history",
        "kind": "tool",
        "name": "Load recent price history",
        "output_schema": {},
        "retry": {
          "backoff_seconds": 1,
          "max_attempts": 1
        },
        "timeout": 120,
        "tool": {
          "name": "file_read"
        }
      },
      {
        "code": {
          "network": [],
          "secrets": [],
          "source": "async function run(input, ctx) {\n  const f = input.fetched;\n  const today = f.snapshot_date;\n  let h = input.history;\n  let text = typeof h === 'string' ? h : (h && (h.content || h.text || h.output || (h.data && (h.data.content || h.data.text)))) || '';\n  if (typeof text !== 'string') text = JSON.stringify(text);\n  const byId = {};\n  let rowsRead = 0;\n  for (let line of text.split('\\n')) {\n    line = line.replace(/^\\s*\\d+\\|/, '').trim();\n    if (!line.startsWith('{')) continue;\n    let r; try { r = JSON.parse(line); } catch (e) { continue; }\n    if (!r.id || !r.d) continue;\n    rowsRead++;\n    const m = (byId[r.id] = byId[r.id] || {});\n    m[r.d] = (r.mp === null || r.mp === undefined) ? null : Number(r.mp);\n  }\n  ctx.log('history rows read', rowsRead);\n  const cutoff = new Date(new Date(today + 'T00:00:00Z').getTime() - 30 * 86400000).toISOString().slice(0, 10);\n  const r2 = v => v === null ? null : Math.round(v * 100) / 100;\n  let newCount = 0;\n  const products = f.products.map(p => {\n    const m = Object.assign({}, byId[p.product_id] || {});\n    m[today] = p.market_price;\n    const dates = Object.keys(m).filter(d => d >= cutoff).sort();\n    const prior = Object.keys(m).filter(d => d < today && m[d] !== null).sort();\n    if (!Object.keys(m).some(d => d < today)) newCount++;\n    const prevDate = prior.length ? prior[prior.length - 1] : null;\n    const prev = prevDate ? m[prevDate] : null;\n    const prices = dates.map(d => m[d]).filter(v => v !== null);\n    const change = (prev !== null && p.market_price !== null) ? r2(p.market_price - prev) : null;\n    const change_pct = (prev && p.market_price !== null) ? r2((p.market_price - prev) / prev * 100) : null;\n    return {\n      product_id: p.product_id, name: p.name, set_name: p.set_name, number: p.number, rarity: p.rarity, product_type: p.product_type,\n      market_price: p.market_price, lowest_price: p.lowest_price, median_price: p.median_price, listings: p.listings,\n      previous_price: prev, previous_date: prevDate, change, change_pct,\n      low_30d: prices.length ? r2(Math.min(...prices)) : null,\n      high_30d: prices.length ? r2(Math.max(...prices)) : null,\n      avg_30d: prices.length ? r2(prices.reduce((a, b) => a + b, 0) / prices.length) : null,\n      days_tracked: dates.length, url: p.url\n    };\n  });\n  const movers = products.filter(p => p.change_pct !== null && p.change !== 0).map(p => ({ product_id: p.product_id, name: p.name, market_price: p.market_price, change: p.change, change_pct: p.change_pct }));\n  const top_gainers = movers.filter(m => m.change > 0).sort((a, b) => b.change_pct - a.change_pct).slice(0, 5);\n  const top_losers = movers.filter(m => m.change < 0).sort((a, b) => a.change_pct - b.change_pct).slice(0, 5);\n  return { snapshot_date: today, product_count: products.length, new_products: newCount, products, top_gainers, top_losers };\n}",
          "timeout": 30
        },
        "id": "compare",
        "input": {
          "fetched": "{{nodes.fetch_prices.output}}",
          "history": "{{nodes.load_history.output}}"
        },
        "intent": "Compute changes and trends",
        "kind": "code",
        "name": "Compute changes and trends",
        "output_schema": {
          "properties": {
            "new_products": {
              "type": "integer"
            },
            "product_count": {
              "type": "integer"
            },
            "products": {
              "items": {
                "type": "object"
              },
              "type": "array"
            },
            "snapshot_date": {
              "type": "string"
            },
            "top_gainers": {
              "items": {
                "type": "object"
              },
              "type": "array"
            },
            "top_losers": {
              "items": {
                "type": "object"
              },
              "type": "array"
            }
          },
          "required": [
            "snapshot_date",
            "product_count",
            "new_products",
            "products",
            "top_gainers",
            "top_losers"
          ],
          "type": "object"
        },
        "retry": {
          "backoff_seconds": 1,
          "max_attempts": 1
        },
        "timeout": 120
      }
    ],
    "output": {
      "new_products": "{{nodes.compare.output.new_products}}",
      "product_count": "{{nodes.compare.output.product_count}}",
      "products": "{{nodes.compare.output.products}}",
      "snapshot_date": "{{nodes.compare.output.snapshot_date}}",
      "top_gainers": "{{nodes.compare.output.top_gainers}}",
      "top_losers": "{{nodes.compare.output.top_losers}}"
    },
    "output_schema": {
      "additionalProperties": false,
      "properties": {
        "new_products": {
          "title": "Products seen for the first time",
          "type": "integer"
        },
        "product_count": {
          "title": "Products tracked",
          "type": "integer"
        },
        "products": {
          "items": {
            "additionalProperties": false,
            "properties": {
              "avg_30d": {
                "title": "30-day average",
                "type": [
                  "number",
                  "null"
                ]
              },
              "change": {
                "title": "Change since previous",
                "type": [
                  "number",
                  "null"
                ]
              },
              "change_pct": {
                "title": "Change since previous (%)",
                "type": [
                  "number",
                  "null"
                ]
              },
              "days_tracked": {
                "title": "Days tracked (30 days)",
                "type": "integer"
              },
              "high_30d": {
                "title": "30-day high",
                "type": [
                  "number",
                  "null"
                ]
              },
              "listings": {
                "title": "Listings",
                "type": [
                  "integer",
                  "null"
                ]
              },
              "low_30d": {
                "title": "30-day low",
                "type": [
                  "number",
                  "null"
                ]
              },
              "lowest_price": {
                "title": "Lowest listing (USD)",
                "type": [
                  "number",
                  "null"
                ]
              },
              "market_price": {
                "title": "Market price (USD)",
                "type": [
                  "number",
                  "null"
                ]
              },
              "median_price": {
                "title": "Median listing (USD)",
                "type": [
                  "number",
                  "null"
                ]
              },
              "name": {
                "title": "Name",
                "type": "string"
              },
              "number": {
                "title": "Card number",
                "type": [
                  "string",
                  "null"
                ]
              },
              "previous_date": {
                "title": "Previous snapshot date",
                "type": [
                  "string",
                  "null"
                ]
              },
              "previous_price": {
                "title": "Previous market price",
                "type": [
                  "number",
                  "null"
                ]
              },
              "product_id": {
                "title": "Product ID",
                "type": "integer"
              },
              "product_type": {
                "title": "Product type",
                "type": [
                  "string",
                  "null"
                ]
              },
              "rarity": {
                "title": "Rarity",
                "type": [
                  "string",
                  "null"
                ]
              },
              "set_name": {
                "title": "Set",
                "type": "string"
              },
              "url": {
                "title": "Link",
                "type": "string"
              }
            },
            "required": [
              "product_id",
              "name",
              "url",
              "market_price",
              "change",
              "change_pct",
              "days_tracked"
            ],
            "type": "object"
          },
          "title": "Products",
          "type": "array"
        },
        "snapshot_date": {
          "title": "Snapshot date",
          "type": "string"
        },
        "top_gainers": {
          "items": {
            "additionalProperties": false,
            "properties": {
              "change": {
                "title": "Change",
                "type": [
                  "number",
                  "null"
                ]
              },
              "change_pct": {
                "title": "Change (%)",
                "type": [
                  "number",
                  "null"
                ]
              },
              "market_price": {
                "title": "Market price",
                "type": [
                  "number",
                  "null"
                ]
              },
              "name": {
                "title": "Name",
                "type": "string"
              },
              "product_id": {
                "title": "Product ID",
                "type": "integer"
              }
            },
            "type": "object"
          },
          "title": "Biggest gainers",
          "type": "array"
        },
        "top_losers": {
          "items": {
            "additionalProperties": false,
            "properties": {
              "change": {
                "title": "Change",
                "type": [
                  "number",
                  "null"
                ]
              },
              "change_pct": {
                "title": "Change (%)",
                "type": [
                  "number",
                  "null"
                ]
              },
              "market_price": {
                "title": "Market price",
                "type": [
                  "number",
                  "null"
                ]
              },
              "name": {
                "title": "Name",
                "type": "string"
              },
              "product_id": {
                "title": "Product ID",
                "type": "integer"
              }
            },
            "type": "object"
          },
          "title": "Biggest losers",
          "type": "array"
        }
      },
      "required": [
        "snapshot_date",
        "product_count",
        "new_products",
        "products",
        "top_gainers",
        "top_losers"
      ],
      "type": "object"
    },
    "requirement_notes": {},
    "secrets": []
  }
}