{
  "name": "Send filing-backed earnings updates for a ticker watchlist",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/30 6-21 * * 1-5"
            }
          ]
        }
      },
      "id": "bb12f74d-ade2-502d-8337-91569bc4c26f",
      "name": "Every 30 min, weekdays 6:00-21:30",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "61b3ac08-c59c-5f49-972b-049fae153e45",
              "name": "watchlist",
              "value": "NVDA, WMT, TJX, HD, TGT",
              "type": "string"
            },
            {
              "id": "251ab67d-f1e5-56d7-8895-7d0d2d3398e2",
              "name": "test_mode",
              "value": true,
              "type": "boolean"
            }
          ]
        },
        "options": {}
      },
      "id": "55d54400-3c8b-5be7-9aa6-4eb39750e86a",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "d366a41f-a21c-5a5d-ae6b-9e8cc45efa2c",
              "leftValue": "={{ $json.test_mode }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "8e6cc1a0-795d-5d6d-bdba-f8134b7fc1ea",
      "name": "Test mode?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Test mode: the no-key sample endpoint covers only these five tickers.\n// Keep the watchlist tickers that are among them; if none are, use NVDA.\nconst SAMPLE = ['NVDA', 'WMT', 'TJX', 'HD', 'TGT'];\nconst raw = String($('Config').first().json.watchlist || '');\nconst watch = raw.toUpperCase().split(/[\\s,;]+/).filter(Boolean);\nlet pick = watch.filter(t => SAMPLE.includes(t));\nif (pick.length === 0) pick = ['NVDA'];\nreturn pick.map(ticker => ({ json: { ticker } }));\n"
      },
      "id": "5dd68280-f1ca-5a55-9c3a-6aec9225b6e7",
      "name": "Sample tickers on watchlist",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        160
      ]
    },
    {
      "parameters": {
        "url": "https://yieldterminalpro.com/api/apps/6a44fce3122251d9ebac82ed/functions/data-sample",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "ticker",
              "value": "={{ $json.ticker }}"
            }
          ]
        },
        "options": {}
      },
      "id": "acb7fd8e-fedc-5bf6-a83d-1b98206b0f21",
      "name": "Sample company (no key)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        160
      ]
    },
    {
      "parameters": {
        "url": "https://yieldterminalpro.com/api/apps/6a44fce3122251d9ebac82ed/functions/data-api",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "endpoint",
              "value": "results"
            }
          ]
        },
        "options": {}
      },
      "id": "55dd80af-e2f4-5bd5-bb6d-4dc3c7fa3912",
      "name": "Results, latest filing day (key)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        770,
        440
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "YieldTerminal Pro API key (x-api-key)"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// One item per results release, same shape for both branches.\n// Withheld figures stay null, never 0: when a figure could not be confirmed the API sets *_text\n// to null (or leaves it out) and leaves the number fields out. A missing number means withheld.\nconst FIGS = ['revenue', 'net_income', 'eps'];\nconst out = [];\nfor (const item of $input.all()) {\n  const r = item.json || {};\n  if (r.error) {\n    // bad key, daily limit reached, ...: stop with a readable message\n    throw new Error('YieldTerminal Pro API: ' + r.error + (r.code ? ' (' + r.code + ')' : ''));\n  }\n  const rows = Array.isArray(r.results) ? r.results : [];\n  const isCompany = r.endpoint === 'company';\n  // company responses list releases newest first; only the newest one is an update\n  const pick = isCompany ? rows.slice(0, 1) : rows;\n  for (const row of pick) {\n    const o = {\n      ticker: String(row.ticker || r.ticker || '').toUpperCase(),\n      company: row.company || r.company || null,\n      form: row.form || null,\n      filed: row.filed || null,\n      accepted: row.accepted ?? null,\n      quarter_ended: row.quarter_ended ?? null,\n      accession: row.accession,\n      filing_url: row.source_url,\n      sample: r.sample === true,\n      // rows from the results-by-day endpoint carry no release sentences; the company endpoint does\n      needs_source_sentences: !isCompany,\n    };\n    for (const f of FIGS) {\n      const shown = row[f + '_text'];\n      const withheld = shown === null || shown === undefined || shown === '';\n      const v = row[f + '_value'];\n      o[f + '_withheld'] = withheld;\n      o[f + '_text'] = withheld ? null : shown;\n      o[f + '_value'] = !withheld && typeof v === 'number' ? v : null;\n      o[f + '_currency'] = withheld ? null : (row[f + '_currency'] || null);\n      o[f + '_label'] = row[f + '_label'] ?? null;\n      const proof = row[f + '_proof'];\n      o[f + '_source_sentence'] = !withheld && Array.isArray(proof) && proof.length ? proof[0] : null;\n    }\n    if (o.accession && o.ticker) out.push({ json: o });\n  }\n}\nreturn out;\n"
      },
      "id": "325de71f-6fc7-5ec5-b1de-dd0deb258df1",
      "name": "One item per release",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "7ac184e1-14f2-5d82-93d5-e0136231ebe7",
              "leftValue": "={{ $('Config').first().json.watchlist.toUpperCase().split(/[\\s,;]+/).filter(Boolean).includes($json.ticker) }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "50d8b69e-3551-5d6c-80f3-9f2a67cab9e4",
      "name": "On my watchlist",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        1320,
        300
      ]
    },
    {
      "parameters": {
        "operation": "removeItemsRepeatedInPreviousExecutions",
        "dedupeValue": "={{ $json.accession }}",
        "options": {}
      },
      "id": "8fc253df-9ca2-566c-8c79-2be23746c3c1",
      "name": "New filings only (accession)",
      "type": "n8n-nodes-base.removeDuplicates",
      "typeVersion": 2,
      "position": [
        1540,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "709b908d-8755-53d3-ba4b-ac20fe6e4ec6",
              "leftValue": "={{ $json.needs_source_sentences }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "08f830c7-e167-524b-b83a-83acce75c5c1",
      "name": "Needs source sentences?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1760,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://yieldterminalpro.com/api/apps/6a44fce3122251d9ebac82ed/functions/data-api",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "endpoint",
              "value": "company"
            },
            {
              "name": "ticker",
              "value": "={{ $json.ticker }}"
            }
          ]
        },
        "options": {}
      },
      "id": "7d367efa-fab4-5f02-85bd-387e992df6d1",
      "name": "Company release sentences (key)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1980,
        180
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "YieldTerminal Pro API key (x-api-key)"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "// Copy the release sentences (revenue_proof, net_income_proof, eps_proof) from the company\n// response onto the row, matched by accession. If the company page has not caught up yet,\n// or the call failed, the sentences stay null and the update still goes out.\nconst FIGS = ['revenue', 'net_income', 'eps'];\nconst out = [];\nconst responses = $input.all();\nfor (let i = 0; i < responses.length; i++) {\n  let orig;\n  try { orig = $('Needs source sentences?').itemMatching(i).json; }\n  catch (e) { orig = $('Needs source sentences?').all(0)[i].json; }\n  const row = { ...orig, needs_source_sentences: false };\n  const resp = responses[i].json || {};\n  const rel = Array.isArray(resp.results) ? resp.results.find(x => x.accession === orig.accession) : null;\n  if (rel) {\n    for (const f of FIGS) {\n      const proof = rel[f + '_proof'];\n      if (!row[f + '_withheld'] && Array.isArray(proof) && proof.length) row[f + '_source_sentence'] = proof[0];\n    }\n  }\n  out.push({ json: row });\n}\nreturn out;\n"
      },
      "id": "1ba241fa-d48d-5cc9-8b18-07447fd364d1",
      "name": "Attach source sentences",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2200,
        180
      ]
    },
    {
      "parameters": {
        "jsCode": "// Build the Slack text and the Sheets row. A withheld figure reads \"withheld\" in Slack and is a\n// blank cell (null) in Sheets: never 0.\nconst NAMES = { revenue: 'Revenue', net_income: 'Net income', eps: 'Diluted EPS' };\nreturn $input.all().map(({ json: r }) => {\n  const lines = [];\n  lines.push(`*${r.ticker}*${r.company ? ' (' + r.company + ')' : ''}: Form ${r.form || '?'} filed ${r.filed || '?'}${r.quarter_ended ? ', quarter ' + r.quarter_ended : ''}`);\n  for (const f of ['revenue', 'net_income', 'eps']) {\n    if (r[f + '_withheld']) {\n      lines.push(`${NAMES[f]}: withheld (could not be confirmed from the filing; not zero)`);\n    } else {\n      let s = `${NAMES[f]}: ${r[f + '_text']}`;\n      if (r[f + '_label']) s += ` [${r[f + '_label']}]`;\n      lines.push(s);\n      if (r[f + '_source_sentence']) lines.push(`> \"${String(r[f + '_source_sentence']).trim()}\"`);\n    }\n  }\n  lines.push(`Filing: ${r.filing_url}`);\n  lines.push(`Accession: ${r.accession}`);\n  if (r.sample) lines.push('_Test mode: no-key sample response_');\n  lines.push('Data: YieldTerminal Pro');\n  return { json: { ...r, slack_text: lines.join('\\n'), credit: 'Data: YieldTerminal Pro' } };\n});\n"
      },
      "id": "dbdf2280-e899-5d18-bb09-edf24e75794c",
      "name": "Format update",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2420,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "accessToken",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#earnings",
          "mode": "name"
        },
        "text": "={{ $json.slack_text }}",
        "otherOptions": {}
      },
      "id": "8888929e-4d9e-5ed0-933a-bac6242d5ebe",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        2660,
        200
      ],
      "credentials": {
        "slackApi": {
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "ticker": "={{ $json.ticker }}",
            "company": "={{ $json.company }}",
            "form": "={{ $json.form }}",
            "filed": "={{ $json.filed }}",
            "quarter_ended": "={{ $json.quarter_ended }}",
            "accession": "={{ $json.accession }}",
            "revenue_value": "={{ $json.revenue_value }}",
            "revenue_currency": "={{ $json.revenue_currency }}",
            "revenue_text": "={{ $json.revenue_text }}",
            "revenue_label": "={{ $json.revenue_label }}",
            "revenue_source_sentence": "={{ $json.revenue_source_sentence }}",
            "net_income_value": "={{ $json.net_income_value }}",
            "net_income_currency": "={{ $json.net_income_currency }}",
            "net_income_text": "={{ $json.net_income_text }}",
            "net_income_label": "={{ $json.net_income_label }}",
            "net_income_source_sentence": "={{ $json.net_income_source_sentence }}",
            "eps_value": "={{ $json.eps_value }}",
            "eps_text": "={{ $json.eps_text }}",
            "eps_label": "={{ $json.eps_label }}",
            "eps_source_sentence": "={{ $json.eps_source_sentence }}",
            "filing_url": "={{ $json.filing_url }}",
            "sample": "={{ $json.sample }}",
            "credit": "={{ $json.credit }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "ticker",
              "displayName": "ticker",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "company",
              "displayName": "company",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "form",
              "displayName": "form",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "filed",
              "displayName": "filed",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "quarter_ended",
              "displayName": "quarter_ended",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "accession",
              "displayName": "accession",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "revenue_value",
              "displayName": "revenue_value",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "revenue_currency",
              "displayName": "revenue_currency",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "revenue_text",
              "displayName": "revenue_text",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "revenue_label",
              "displayName": "revenue_label",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "revenue_source_sentence",
              "displayName": "revenue_source_sentence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "net_income_value",
              "displayName": "net_income_value",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "net_income_currency",
              "displayName": "net_income_currency",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "net_income_text",
              "displayName": "net_income_text",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "net_income_label",
              "displayName": "net_income_label",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "net_income_source_sentence",
              "displayName": "net_income_source_sentence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "eps_value",
              "displayName": "eps_value",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "eps_text",
              "displayName": "eps_text",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "eps_label",
              "displayName": "eps_label",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "eps_source_sentence",
              "displayName": "eps_source_sentence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "filing_url",
              "displayName": "filing_url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "sample",
              "displayName": "sample",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            },
            {
              "id": "credit",
              "displayName": "credit",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsed": true
            }
          ]
        },
        "options": {}
      },
      "id": "95343333-82ec-52a1-97b4-39dbd0805982",
      "name": "Append to Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        2660,
        420
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Filing-backed earnings updates for a watchlist\nPosts each new quarterly result for your tickers (revenue, net income, diluted EPS) with the SEC filing link and the release sentence that states each figure.\n\n**Try it with no key:** leave `test_mode` on in **Config** and click *Test workflow*. Test mode calls the public sample endpoint (NVDA, WMT, TJX, HD, TGT only).\n\n**Go live:** create a *Header Auth* credential with Name `x-api-key` and Value = your key (`ytp_` + 48 hex characters). Select it on the two HTTP nodes marked *(key)*, set `test_mode` to false, edit `watchlist`, connect Slack and/or Google Sheets (delete the one you don't use), then activate.\n\nCalls: 1 per run plus 1 per new filing on your list. At this schedule that is about 32 a day plus filings, under the free key's 100.\n\n_Disclosure: I sell this API (yieldterminalpro.com). Plans: https://yieldterminalpro.com/developers_",
        "height": 400,
        "width": 1240,
        "color": 6
      },
      "id": "84290e72-f9f6-5227-bb30-bde5bbac8983",
      "name": "Note: Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -340
      ]
    },
    {
      "parameters": {
        "content": "### Withheld is not zero\nA figure is published only when the release's table agrees with a second source. Otherwise the API leaves the number out. This workflow keeps it as **null** (blank cell in Sheets, \"withheld\" in Slack) and never writes 0.",
        "height": 260,
        "width": 520,
        "color": 3
      },
      "id": "37ba56ef-f9c3-5a5a-9fa9-311a95250419",
      "name": "Note: Withheld figures",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1080,
        540
      ]
    },
    {
      "parameters": {
        "content": "### Dedupe on accession\nEach SEC filing has one accession number, so a release is posted once. To replay test mode, run *Clear Deduplication History* on the *New filings only* node.",
        "height": 260,
        "width": 440,
        "color": 4
      },
      "id": "0b278a57-a47d-535b-a3d3-3e9e5ed99333",
      "name": "Note: Dedupe",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1620,
        540
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Every 30 min, weekdays 6:00-21:30": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Test mode?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test mode?": {
      "main": [
        [
          {
            "node": "Sample tickers on watchlist",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Results, latest filing day (key)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample tickers on watchlist": {
      "main": [
        [
          {
            "node": "Sample company (no key)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample company (no key)": {
      "main": [
        [
          {
            "node": "One item per release",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Results, latest filing day (key)": {
      "main": [
        [
          {
            "node": "One item per release",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One item per release": {
      "main": [
        [
          {
            "node": "On my watchlist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On my watchlist": {
      "main": [
        [
          {
            "node": "New filings only (accession)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New filings only (accession)": {
      "main": [
        [
          {
            "node": "Needs source sentences?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Needs source sentences?": {
      "main": [
        [
          {
            "node": "Company release sentences (key)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Format update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Company release sentences (key)": {
      "main": [
        [
          {
            "node": "Attach source sentences",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Attach source sentences": {
      "main": [
        [
          {
            "node": "Format update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format update": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          },
          {
            "node": "Append to Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "America/New_York"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
