api-sports.js
API Sports is a JavaScript library to interact with sports api.
Features (WIP)
- Football (partial support)
- Basketball (coming soon)
- Volleyball (coming soon)
- Hockey (coming soon)
- American Football (coming soon)
- Rugby (coming soon)
Installation
WARNING: This library is still a work in progress. Many features are not implemented yet.
npm install api-sports.js
Example
Match updates are polled every 60 seconds by default. This library attempts to get the most information with the least amount of requests.
import { FootballEvents, SportsAPI } from 'api-sports.js';
const sports = new SportsAPI();
sports.setTokens({
football: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
});
sports.football.on(FootballEvents.Debug, console.log);
sports.football.on(FootballEvents.Start, async (event) => {
console.log(`Fixture ${event.fixtureId} started...`);
});
sports.football.on(FootballEvents.End, async (event) => {
console.log(`Fixture ${event.fixtureId} ended...`);
process.exit(0);
});
sports.football.on(FootballEvents.StatusUpdate, (event) => {
const status = event.getStatus();
console.log(
`Fixture ${event.fixtureId} status updated (${status.old} -> ${status.new})`
);
});
sports.football.on(FootballEvents.ScoreUpdate, async (event) => {
const scores = event.getScores();
const oldScore = (scores.old.home ?? 0) + ' - ' + (scores.old.away ?? 0);
const newScore = (scores.new.home ?? 0) + ' - ' + (scores.new.away ?? 0);
console.log(
`Fixture ${event.fixtureId} score updated (${oldScore} -> ${newScore})`
);
});
sports.football.on(FootballEvents.Substitution, async (event) => {
const substitutions = event.getSubstitutions();
const oldTotal = substitutions.old.length;
const newTotal = substitutions.new.length;
console.log(
`Fixture ${event.fixtureId} substitution updated. Total (${oldTotal} -> ${newTotal})`
);
});
const FIXTURE_ID = 1234135;
await sports.football.fixtures.subscriptions.addFixture(FIXTURE_ID);
MIT License
Authored and maintained by 3Bird