JavaScript

Suggested libraries to connect to the StatsBomb live API using Javascript.

Example - Retrieving Matches

A query to return the available matches for your team within a given date

// node-fetch and util are only required for nodejs
const fetch = require('node-fetch');
const util = require('util');
const query = `query NextTeamMatch {
      live_match(
        where: {
          _and: [
            { match_date: { _lt: "2021-10-15",
                            _gte: "2021-10-30" } }
            {
              _or: [
                { match_home_team_id: { _eq: 42 },
                  match_away_team_id: { _eq: 42 } }
              ]
            }
          ]
        }
      ) {
        match_id
        match_date
        match_local_kick_off
        match_name
        match_home_team_id
        match_away_team_id
      }
}`;

fetch('<%= config[:endpoint] %>', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query
  })
})
  .then(r => r.json())
  .then(data => console.log('data returned:', util.inspect(data, {depth: null})));

Example response

{
  "NextTeamMatch": {
    "live_match": [
      {
        "match_id": 35,
        "match_date": "2020-10-09T15:00:00+00:00",
        "match_local_kick_off": "15:00:00",
        "match_name": "City vs United",
        "match_home_team_id": 111,
        "match_away_team_id": 222
      },
      {
        "match_id": 36,
        "match_date": "2020-10-09T15:00:00+00:00",
        "match_local_kick_off": "15:00:00",
        "match_name": "Spurs vs Liverpool",
        "match_home_team_id": 673,
        "match_away_team_id": 503
      }
    ]
  }
}

Retrieving competition data

Query or subscribe to all events in a particular competition, in this example competition data for the English Premier League.

// node-fetch and util are only required for nodejs
const fetch = require('node-fetch');
const util = require('util');
const query = `query CompetitionQuery {
  live_competition(where: {competition_area: {area_name: {_eq: "England"}}, competition_name: {_eq: "Premier League"}}) {
    competition_id
    competition_name
    competition_area {
      area_id
      area_name
    }
  }
}`;

fetch('<%= config[:endpoint] %>', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query
  })
})
  .then(r => r.json())
  .then(data => console.log('data returned:', util.inspect(data, {depth: null})));

Example response

{
  "data": {
    "statsbomb_competition": [
      {
        "competition_id": 2,
        "competition_name": "Premier League",
        "competition_area": {
          "area_id": 2,
          "area_name": "England"
        }
      }
    ]
  }
}

results matching ""

    No results matching ""