JavaScript SDK

@lovebowls/api is a small JavaScript client for this API, written in TypeScript, with no dependencies. It works in browsers and in Node 18 or later (it uses the global fetch). See it running in the live examples.

Install

From npm, for a project with a build step or for Node:

npm i @lovebowls/api
import { LovebowlsClient, LovebowlsApiError } from "@lovebowls/api";

From jsDelivr, for a plain HTML page with no build step:

<script type="module">
  import { LovebowlsClient } from "https://cdn.jsdelivr.net/npm/@lovebowls/api@0.5.0/+esm";
</script>

Pin the version, as above, so an update cannot change your page without you choosing it.

The client

const lb = new LovebowlsClient({
  apiKey: "lb_pk_...",                        // required; sent as X-Api-Key
  auth,                                       // optional; a LovebowlsAuth, for member methods
  memberToken,                                // optional; a token (or async function) you already hold
  baseUrl: "https://api.lovebowls.co.uk/v1",  // optional; this is the default
  fetch: customFetch,                         // optional; default is the global fetch
});

An empty apiKey throws a TypeError at once. Every method returns a promise and throws LovebowlsApiError on failure. Responses are the API's JSON unchanged, so dates stay ISO strings in UTC.

Methods

The names are the contract's operation ids. clubId is your club's id.

MethodReturns
getClub(clubId)the club: name, timeZone, teams, current season
listCompetitions(clubId, { kind? }){ items }, every competition (not paged)
listFixtures(clubId, { from?, to?, competition?, team?, kind?, limit?, cursor? }){ items, nextCursor }, one page
iterateFixtures(clubId, filters?)an async iterable over every fixture, page by page
getFixture(clubId, fixtureId)one fixture
getLeague(leagueId)a league and its table (leagues have no club id)
getMe(clubId)the signed-in member's own record. Needs a member token
getMyCalendar(clubId, { from?, to? }){ items }, the member's own calendar. Needs a member token; from and to as for fixtures
listMyAvailability(clubId, { from?, to? }){ fixtureIds }, the fixtures the member is available for (to exclusive). Needs a member token
setMyAvailability(clubId, fixtureId){ available: true, fixtureIds }: marks the member available (a PUT, idempotent). Needs a member token
clearMyAvailability(clubId, fixtureId){ available: false, fixtureIds }: clears it (a DELETE, idempotent). Needs a member token
getTeamsheet(clubId, fixtureId)the fixture's published teamsheet. Needs a member token
listNews(clubId, { limit?, cursor? }){ items, nextCursor }, one page of news, newest first. Sends the member token when held: a member also gets the members-only articles
iterateNews(clubId, { limit? })an async iterable over every news article, page by page
getNewsArticle(clubId, articleId)one article with its sanitised bodyHtml and attachments
listHonours(clubId){ items }, the honours board, newest year first (not paged)
listLifeMembers(clubId){ items }, the life members and founders (not paged)
listTrophies(clubId){ items }, the trophies without their winners (not paged)
getTrophy(clubId, trophyId)one trophy with its winners for every year, newest first

Filters:

The fields are described in Fixtures, Members, Selection and News, honours and trophies.

setMyAvailability and clearMyAvailability are two methods because they are the API's two operations, and so there is no boolean to get the wrong way round. A change the member may not make throws a LovebowlsApiError with status 409 and a code from AVAILABILITY_REFUSAL_CODES; isAvailabilityRefused(error) recognises it. See Selection.

When a member is logged in (auth, or a memberToken), the public methods also send the token, so a member can read a draft club (see Authentication), and the news methods also return the club's members-only articles. A signed-in person who is not a member of the club gets the public answer, never an error. With no token they are visitor calls, as before.

const club = await lb.getClub("CLUB_ID");
const { items: competitions } = await lb.listCompetitions("CLUB_ID", { kind: "match" });
const league = await lb.getLeague("LEAGUE_ID");
const fixture = await lb.getFixture("CLUB_ID", "FIXTURE_ID");

Member login

The member methods (getMe, getMyCalendar and the selection methods) need a member token. LovebowlsAuth runs the sign-in (authorization code with PKCE, plain fetch, no other library) and the client sends the token on member calls only:

import { LovebowlsClient, LovebowlsAuth } from "@lovebowls/api";

const auth = new LovebowlsAuth({
  clientId: "YOUR-OAUTH-CLIENT-ID",                 // public; ask lovebowls
  redirectUri: location.origin + location.pathname, // on the client's list, exactly
});
const lb = new LovebowlsClient({ apiKey: "lb_pk_...", auth });

await auth.handleRedirect();                        // call on every load of the return page
document.getElementById("login").onclick = () => auth.login();
document.getElementById("logout").onclick = () => auth.logout();
if (auth.isLoggedIn()) console.log(await lb.getMe("CLUB_ID"));

Options: storage ("session" default, "local", "memory" or your own), offlineAccess (off by default), postLogoutUrl. Methods: login(), handleRedirect(), isLoggedIn(), getAccessToken(), logout(), onChange(cb). The 4-hour token, why logout does not revoke it, the member errors and using a token from a server are in Authentication; the data is in Members.

Paging

listFixtures returns one page. Pass nextCursor back as cursor, with the same filters, until it is null; or let iterateFixtures do it:

for await (const fixture of lb.iterateFixtures("CLUB_ID", { from: "2027-04-01", to: "2027-10-01" })) {
  console.log(fixture.start, fixture.opponent ?? fixture.eventName);
}

See Paging for the rules.

Errors

Every failure is a LovebowlsApiError:

FieldMeaning
statusthe HTTP status; 0 for network_error
codethe stable code to branch on
title, detailfor people; they may change, and detail may be null
requestIdquote it when you ask for help
retryAfterseconds to wait, or null

code is the API's error code. The SDK adds some of its own: network_error (no answer: offline, a blocked cross-origin call), invalid_response (an answer the API would not send), member_token_required (a member method with no usable token; no request was made) and login_failed / state_mismatch (a sign-in that did not complete). The key and any member token never appear in an error message.

try {
  await lb.getFixture("CLUB_ID", id);
} catch (error) {
  if (!(error instanceof LovebowlsApiError)) throw error;
  if (error.code === "fixture_not_found") return null;
  if (error.code === "rate_limited") await new Promise((r) => setTimeout(r, (error.retryAfter ?? 1) * 1000));
  else throw error;
}

The browser rule

In a web page use a publishable key (lb_pk_...). It works only from the origins listed for it and only for reading, so it is safe to show in your page's source. Never put a server key (lb_sk_...) in a page: the API refuses it from a browser (server_key_in_browser), and anyone can read what a page contains. Keep server keys in server code and environment variables. See Keys.