mkreiser / ESPN-Fantasy-Football-API

Connect to ESPN's fantasy football API via this JS API client for web and NodeJS. Available as an npm package.
http://espn-fantasy-football-api.s3-website.us-east-2.amazonaws.com/
GNU Lesser General Public License v3.0
296 stars 74 forks source link

Working Example #244

Open jlag34 opened 1 month ago

jlag34 commented 1 month ago

Is there a very basic working example you could give me? I have the follow code:

const { Client } = require('espn-fantasy-football-api/node');
const axios = require('axios')

const leagueId = 30031
const seasonId = 2023

// const espnS2 = my espnS2 code
// const SWID = my SWID code

async function getLeagueData() {
  try {

    // const routeBase = `https://site.api.espn.com/${seasonId}/segments/0/leagues/${leagueId}`;
    // const routeParams = `?view=mMatchup&view=mMatchupScore&scoringPeriodId=${3}`;
    // const route = `${routeBase}${routeParams}`;
    // const data = await axios.get(route)
    // console.log('DATA : ', data)
    const client = new Client({ leagueId });

  //   // Fetch league info
    const leagueInfo = await client.getTeamsAtWeek({ seasonId, scoringPeriodId: 1 });
    console.log('League Info:', JSON.stringify(leagueInfo, null, 2));

  //   // Fetch teams (users) in the league
  //   const teams = await client.getTeamsAtWeek({ seasonId, scoringPeriodId: 1 });
  //   console.log('Teams:', JSON.stringify(teams, null, 2));

  //   // Fetch rosters in the league
  //   const rosters = await client.getBoxscoreForWeek({ seasonId, scoringPeriodId: 1 });
  //   console.log('Rosters:', JSON.stringify(rosters, null, 2));
  } catch (error) {
      console.error('Error fetching data:', error);
  }
}

getLeagueData()

The league id is just one I found on Google:

https://fantasy.espn.com/football/league?leagueId=30031

When I run this code, I just get back an empty array. When I try to use the axios version I get an error. When I use the client to put in my own person league, I also get an empty array. I made the league public but I also tried with the two codes and still got nothing.

I'm just looking to be able to get the league settings, rosters, and users based on a league id. Any way you can point me in the right direction? I had GPT look at the repo but still no luck. Thanks