{
  "name": "Automate sports betting data with dino.markets and Google Sheets",
  "nodes": [
    {
      "id": "00000001-0000-4000-8000-000000000001",
      "name": "Every morning at 7:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        200
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 7
            }
          ]
        }
      }
    },
    {
      "id": "00000002-0000-4000-8000-000000000001",
      "name": "Every night at 23:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        400
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 23
            }
          ]
        }
      }
    },
    {
      "id": "00000003-0000-4000-8000-000000000001",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "league",
              "value": "nba",
              "type": "string"
            },
            {
              "id": "2",
              "name": "market",
              "value": "moneyline",
              "type": "string"
            },
            {
              "id": "3",
              "name": "hoursAhead",
              "value": 26,
              "type": "number"
            },
            {
              "id": "5",
              "name": "sheetUrl",
              "value": "PASTE_YOUR_GOOGLE_SHEET_URL",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "00000004-0000-4000-8000-000000000001",
      "name": "Is it the morning run?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $('Every morning at 7:00').isExecuted }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "00000005-0000-4000-8000-000000000001",
      "name": "Read odds for the next games",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        760,
        140
      ],
      "parameters": {
        "url": "https://api.dino.markets/v2/odds",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "league",
              "value": "={{ $('Settings').first().json.league }}"
            },
            {
              "name": "markets",
              "value": "={{ $('Settings').first().json.market }}"
            },
            {
              "name": "status",
              "value": "scheduled"
            },
            {
              "name": "limit",
              "value": "100"
            },
            {
              "name": "start_before",
              "value": "={{ $now.toUTC().plus({ hours: $('Settings').first().json.hoursAhead }).startOf('second').toISO({ suppressMilliseconds: true }) }}"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "executeOnce": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "onError": "continueErrorOutput"
    },
    {
      "id": "00000006-0000-4000-8000-000000000001",
      "name": "Find the best prices",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1000,
        140
      ],
      "parameters": {
        "jsCode": "// One row per game with the best current price on each side.\nconst settings = $('Settings').first().json;\nconst answer = $input.first().json;\nconst out = [];\nfor (const event of answer.events || []) {\n  if (event.kind !== 'game' || event.status !== 'scheduled') continue;\n  const best = {};\n  for (const book of event.books) {\n    for (const market of book.markets) {\n      if (market.key !== settings.market) continue;\n      for (const o of market.outcomes) {\n        const price = Number(o.price);\n        if (price > 1 && (!best[o.side] || price > best[o.side].price)) best[o.side] = { price, book: book.name };\n      }\n    }\n  }\n  if (Object.keys(best).length === 0) continue;\n  const p = side => best[side] ? best[side].price : '';\n  const b = side => best[side] ? best[side].book : '';\n  out.push({ json: {\n    event_id: event.id, league: event.league, start_time: event.start_time,\n    home_team: event.home.name, away_team: event.away.name,\n    home_price: p('home'), home_book: b('home'), away_price: p('away'), away_book: b('away'),\n    draw_price: p('draw'), draw_book: b('draw'), odds_read_at: answer.as_of } });\n}\nreturn out;\n"
      }
    },
    {
      "id": "00000007-0000-4000-8000-000000000001",
      "name": "Save games and odds",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1240,
        140
      ],
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "={{ $('Settings').first().json.sheetUrl }}"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Games"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "event_id"
          ],
          "value": {
            "event_id": "={{ $json.event_id }}",
            "league": "={{ $json.league }}",
            "start_time": "={{ $json.start_time }}",
            "home_team": "={{ $json.home_team }}",
            "away_team": "={{ $json.away_team }}",
            "home_price": "={{ $json.home_price }}",
            "home_book": "={{ $json.home_book }}",
            "away_price": "={{ $json.away_price }}",
            "away_book": "={{ $json.away_book }}",
            "draw_price": "={{ $json.draw_price }}",
            "draw_book": "={{ $json.draw_book }}",
            "odds_read_at": "={{ $json.odds_read_at }}"
          },
          "schema": [
            {
              "id": "event_id",
              "displayName": "event_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "league",
              "displayName": "league",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "start_time",
              "displayName": "start_time",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "home_team",
              "displayName": "home_team",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "away_team",
              "displayName": "away_team",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "home_price",
              "displayName": "home_price",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "home_book",
              "displayName": "home_book",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "away_price",
              "displayName": "away_price",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "away_book",
              "displayName": "away_book",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "draw_price",
              "displayName": "draw_price",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "draw_book",
              "displayName": "draw_book",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "odds_read_at",
              "displayName": "odds_read_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      }
    },
    {
      "id": "00000008-0000-4000-8000-000000000001",
      "name": "Read saved games",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        760,
        520
      ],
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "={{ $('Settings').first().json.sheetUrl }}"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Games"
        },
        "options": {
          "outputFormatting": {
            "values": {
              "general": "UNFORMATTED_VALUE",
              "date": "FORMATTED_STRING"
            }
          }
        }
      },
      "alwaysOutputData": true
    },
    {
      "id": "00000009-0000-4000-8000-000000000001",
      "name": "Read finished games",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1000,
        520
      ],
      "parameters": {
        "url": "https://api.dino.markets/v2/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "league",
              "value": "={{ $('Settings').first().json.league }}"
            },
            {
              "name": "status",
              "value": "finished"
            },
            {
              "name": "limit",
              "value": "100"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "executeOnce": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "onError": "continueErrorOutput"
    },
    {
      "id": "00000010-0000-4000-8000-000000000001",
      "name": "Match scores to saved games",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1240,
        520
      ],
      "parameters": {
        "jsCode": "// Final scores for games that are in the sheet and have no score yet.\nconst waiting = new Set();\nfor (const row of $('Read saved games').all()) {\n  const done = row.json.home_score !== undefined && row.json.home_score !== null && row.json.home_score !== '';\n  if (row.json.event_id && !done) waiting.add(String(row.json.event_id));\n}\nconst number = s => /^\\d+$/.test(String(s).trim()) ? Number(s) : null;\nconst out = [];\nfor (const event of $input.first().json.events || []) {\n  if (!waiting.has(event.id) || event.status !== 'finished' || !event.score) continue;\n  if (!event.score.home || !event.score.away) continue;\n  const home = number(event.score.home), away = number(event.score.away);\n  // No winner for a score that is text, as in cricket, or a fight with no winner.\n  let winner = '';\n  if (home !== null && away !== null) {\n    if (home !== away) winner = home > away ? event.home.name : event.away.name;\n    else if (!/mma|martial|boxing/i.test(event.sport)) winner = 'Draw';\n  }\n  out.push({ json: { event_id: event.id, home_score: home ?? event.score.home, away_score: away ?? event.score.away, winner } });\n}\nreturn out;\n"
      }
    },
    {
      "id": "00000011-0000-4000-8000-000000000001",
      "name": "Save results",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1480,
        520
      ],
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "={{ $('Settings').first().json.sheetUrl }}"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Games"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "event_id"
          ],
          "value": {
            "event_id": "={{ $json.event_id }}",
            "home_score": "={{ $json.home_score }}",
            "away_score": "={{ $json.away_score }}",
            "winner": "={{ $json.winner }}"
          },
          "schema": [
            {
              "id": "event_id",
              "displayName": "event_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "home_score",
              "displayName": "home_score",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "away_score",
              "displayName": "away_score",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "winner",
              "displayName": "winner",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      }
    },
    {
      "id": "00000012-0000-4000-8000-000000000001",
      "name": "Stop and show the API error",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        1240,
        800
      ],
      "parameters": {
        "errorMessage": "=dino.markets was not read: {{ $json.error.status || '' }} {{ $json.error.message || $json.error }}. Check your key, your credits at dino.markets/dashboard, and the league in Settings."
      }
    },
    {
      "id": "00000013-0000-4000-8000-000000000001",
      "name": "Note 12",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -580,
        -60
      ],
      "parameters": {
        "content": "### How it works\nFor people who keep their own sports betting records or test a prediction model. Each morning it reads one league's games for the next 26 hours from the dino.markets API and saves one row per game to Google Sheets, with the best available price on each side and the book that has it. Each night it reads the finished games, matches them by game id and fills in the final score and the winner. Games already filled in are left alone, and running it twice adds no rows.\n\n### Setup\n1. Create a free dino.markets key and add it as a Header Auth credential on both HTTP Request nodes: name `x-api-key`, value your key.\n2. Make a Google Sheet with a tab named `Games` and this first row: event_id, league, start_time, home_team, away_team, home_price, home_book, away_price, away_book, draw_price, draw_book, odds_read_at, home_score, away_score, winner. Connect your Google account on the three Sheets nodes.\n3. Paste the sheet link in **Settings**, then run the morning trigger once by hand.\n\n[Setup guide](https://dino.markets/guides/automate-sports-betting-data/)\n\n### Customization\nChange the league, market and hours ahead in **Settings**, and the times on the two triggers. For soccer use `moneyline_3way`, which also fills the draw columns.\n\n### Limits\nA morning run uses 1 credit. Reading scores is free. Up to 100 games a run. Scores exist only for leagues the API lists with has_scores, and only for games that started in the last 72 hours. Cricket scores are saved as text with no winner. Exchange and prediction market prices are before their fees.",
        "width": 500,
        "height": 900
      }
    },
    {
      "id": "00000014-0000-4000-8000-000000000001",
      "name": "Note 13",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        60
      ],
      "parameters": {
        "content": "## Schedule and settings\nA morning run and a night run share the same **Settings**.",
        "width": 640,
        "height": 520,
        "color": 7
      }
    },
    {
      "id": "00000015-0000-4000-8000-000000000001",
      "name": "Note 14",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        700,
        0
      ],
      "parameters": {
        "content": "## Morning: games and odds\nOne row per game, with the best price on each side.",
        "width": 780,
        "height": 320,
        "color": 7
      }
    },
    {
      "id": "00000016-0000-4000-8000-000000000001",
      "name": "Note 15",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        700,
        380
      ],
      "parameters": {
        "content": "## Night: final scores\nFills in games that are in the sheet and have no score yet.",
        "width": 980,
        "height": 320,
        "color": 7
      }
    },
    {
      "id": "00000017-0000-4000-8000-000000000001",
      "name": "Note 16",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1180,
        720
      ],
      "parameters": {
        "content": "## Errors\nFailed calls are retried, then shown.",
        "width": 300,
        "height": 240,
        "color": 7
      }
    }
  ],
  "connections": {
    "Every morning at 7:00": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Every night at 23:00": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Is it the morning run?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is it the morning run?": {
      "main": [
        [
          {
            "node": "Read odds for the next games",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Read saved games",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read odds for the next games": {
      "main": [
        [
          {
            "node": "Find the best prices",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stop and show the API error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find the best prices": {
      "main": [
        [
          {
            "node": "Save games and odds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read saved games": {
      "main": [
        [
          {
            "node": "Read finished games",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read finished games": {
      "main": [
        [
          {
            "node": "Match scores to saved games",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stop and show the API error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match scores to saved games": {
      "main": [
        [
          {
            "node": "Save results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}