api-sports.js
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

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

Watching a football match 📺🍿

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',
});

// Debug 🪲
sports.football.on(FootballEvents.Debug, console.log);

// Detect when a football match starts
sports.football.on(FootballEvents.Start, async (event) => {
  console.log(`Fixture ${event.fixtureId} started...`);
});

// Detect when a football match ends
sports.football.on(FootballEvents.End, async (event) => {
  console.log(`Fixture ${event.fixtureId} ended...`);
  process.exit(0);
});

// Detect when a football match status is updated
sports.football.on(FootballEvents.StatusUpdate, (event) => {
  const status = event.getStatus();

  console.log(
    `Fixture ${event.fixtureId} status updated (${status.old} -> ${status.new})`
  );
});

// Detect when a football match score is updated
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})`
  );
});

// Detect when a football match substitution is updated
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})`
  );
});

// Your fixture id
const FIXTURE_ID = 1234135;

// this will immediately start watching the fixture. If the fixture date is in the past, it checks
// the status and if it's not finished, it will start watching it. If it's finished, it will not
// start watching it and instead exit with StaleFixture event. If the fixture date is in the
// future, it will register a cron job for that particular fixture and start watching it when the time comes.
// calling this method multiple times will not always result in multiple requests
// it instead creates a batch of 20 requests, reducing the amount of requests made
await sports.football.fixtures.subscriptions.addFixture(FIXTURE_ID);

MIT License

Authored and maintained by 3Bird

Package Sidebar

Install

npm i api-sports.js

Weekly Downloads

2

Version

0.0.5

License

MIT

Unpacked Size

354 kB

Total Files

7

Last publish

Collaborators

  • snowflake7