Live example: member

Each section below is one member call to the lovebowls API: the code to make it, in the language you choose, and the live answer for the member who is logged in. Log in as a member of the demo club to run them. For the calls that need no login, see the visitor example.

Log in

A member call needs a member token as well as the API key. In a web page, LovebowlsAuth gets one: login() sends the browser to the lovebowls login page, run by Wix, and handleRedirect() finishes the sign-in when the browser comes back. Your page never sees the password. The client then sends the token as Authorization: Bearer <token> on member calls. Ask lovebowls for the OAuth client id and to put your redirect URI on its list. See Authentication.

The cURL and Python tabs below assume your server already holds a member token, in the environment variable LB_MEMBER_TOKEN, and sends it with a server key.

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

const auth = new LovebowlsAuth({
  clientId: "...",                                        // the OAuth client id (public): ask lovebowls
  redirectUri: "https://www.example-bowls.org/members/",  // on the client's list, exactly
});
const lb = new LovebowlsClient({ apiKey: "lb_pk_...", auth }); // your publishable key

auth.onChange((loggedIn) => showPage(loggedIn));  // sign-in, sign-out, or an expired session
await auth.handleRedirect();                      // true when the browser has just come back from the login

loginButton.onclick = () => auth.login();         // to the lovebowls login page
logoutButton.onclick = () => auth.logout();       // forgets the token here; it does not revoke it

Live result

Checking...

The token lasts four hours and is kept in this tab's sessionStorage. No refresh token is requested.

Your details

GET /v1/clubs/{clubId}/me. The member's own record: name, contact details, handicap and club roles. A person who is not a member of the club gets 403 not_a_member. See Members.

// lb is the client from "Log in", with auth
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";

const me = await lb.getMe(CLUB_ID);
console.log(me.name, me.roles.join(", "));
const API = "https://api.lovebowls.co.uk/v1";
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";
const headers = {
  "X-Api-Key": "lb_pk_...",               // your publishable key
  Authorization: `Bearer ${memberToken}`, // from the login
};

const res = await fetch(`${API}/clubs/${CLUB_ID}/me`, { headers });
if (!res.ok) throw new Error((await res.json()).code); // 401 member_token_invalid, 403 not_a_member
const me = await res.json();
curl "https://api.lovebowls.co.uk/v1/clubs/52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6/me" \
  -H "X-Api-Key: $LB_API_KEY" \
  -H "Authorization: Bearer $LB_MEMBER_TOKEN"
import os, requests

API = "https://api.lovebowls.co.uk/v1"
CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6"
headers = {
    "X-Api-Key": os.environ["LB_API_KEY"],  # a server key
    "Authorization": f"Bearer {os.environ['LB_MEMBER_TOKEN']}",
}

r = requests.get(f"{API}/clubs/{CLUB_ID}/me", headers=headers, timeout=10)
r.raise_for_status()
me = r.json()
print(me["name"], me["roles"])

Live result

Log in to run this example.

Your calendar

GET /v1/clubs/{clubId}/me/calendar. The member's own calendar: the fixtures they marked themselves available for, were picked for or withdrew from, every club event, and their away periods. Each fixture entry has the member's own status in me. With no from and to, the range is the current season. The list is not paged. The live result shows the next eight fixtures.

// lb is the client from "Log in", with auth
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";

const { items } = await lb.getMyCalendar(CLUB_ID);
for (const entry of items) {
  if (entry.kind !== "fixture") continue; // the other kind is "away", an away period
  console.log(entry.start, entry.fixture.opponent, entry.me?.selected ? "selected" : entry.me?.available ? "available" : "");
}
const API = "https://api.lovebowls.co.uk/v1";
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";
const headers = {
  "X-Api-Key": "lb_pk_...",               // your publishable key
  Authorization: `Bearer ${memberToken}`, // from the login
};

const res = await fetch(`${API}/clubs/${CLUB_ID}/me/calendar`, { headers });
if (!res.ok) throw new Error((await res.json()).code);
const { items } = await res.json();
curl "https://api.lovebowls.co.uk/v1/clubs/52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6/me/calendar" \
  -H "X-Api-Key: $LB_API_KEY" \
  -H "Authorization: Bearer $LB_MEMBER_TOKEN"
import os, requests

API = "https://api.lovebowls.co.uk/v1"
CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6"
headers = {
    "X-Api-Key": os.environ["LB_API_KEY"],  # a server key
    "Authorization": f"Bearer {os.environ['LB_MEMBER_TOKEN']}",
}

r = requests.get(f"{API}/clubs/{CLUB_ID}/me/calendar", headers=headers, timeout=10)
r.raise_for_status()
for entry in r.json()["items"]:
    print(entry["kind"], entry["start"])

Live result

Log in to run this example.

News for members

GET /v1/clubs/{clubId}/news, the same call as in the visitor example, with the member token. A member of the club also gets the members-only articles, which have membersOnly: true. Someone logged in who is not a member gets the public news, never an error. See News, honours and trophies.

// lb is the client from "Log in", with auth: it sends the token whenever a member is logged in
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";

const { items } = await lb.listNews(CLUB_ID, { limit: 10 });
for (const n of items) console.log(n.title, n.membersOnly ? "(members only)" : "");
const API = "https://api.lovebowls.co.uk/v1";
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";
const headers = {
  "X-Api-Key": "lb_pk_...",               // your publishable key
  Authorization: `Bearer ${memberToken}`, // from the login
};

const res = await fetch(`${API}/clubs/${CLUB_ID}/news?limit=10`, { headers });
if (!res.ok) throw new Error((await res.json()).code);
const { items, nextCursor } = await res.json(); // a cursor works only with a token, like this one
curl "https://api.lovebowls.co.uk/v1/clubs/52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6/news?limit=10" \
  -H "X-Api-Key: $LB_API_KEY" \
  -H "Authorization: Bearer $LB_MEMBER_TOKEN"
import os, requests

API = "https://api.lovebowls.co.uk/v1"
CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6"
headers = {
    "X-Api-Key": os.environ["LB_API_KEY"],  # a server key
    "Authorization": f"Bearer {os.environ['LB_MEMBER_TOKEN']}",
}

r = requests.get(f"{API}/clubs/{CLUB_ID}/news", headers=headers, params={"limit": 10}, timeout=10)
r.raise_for_status()
for n in r.json()["items"]:
    print(n["title"], "(members only)" if n["membersOnly"] else "")

Live result

Log in to run this example.

Your availability

GET /v1/clubs/{clubId}/me/availability lists the fixtures the member is available for. PUT and DELETE /v1/clubs/{clubId}/me/availability/{fixtureId} mark and clear one. A change can touch more than one fixture: fixtureIds in the answer lists them all. A refusal is a 409 with a stable code, for example teamsheet_published. See Selection.

The live result lists the next fixtures that take availability. The buttons change your real availability at the demo club.

import { isAvailabilityRefused } from "@lovebowls/api";
// lb is the client from "Log in", with auth
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";

const from = new Date();
const to = new Date(Date.now() + 120 * 86_400_000);
const { fixtureIds } = await lb.listMyAvailability(CLUB_ID, { from, to });

try {
  const change = await lb.setMyAvailability(CLUB_ID, fixtureId); // or clearMyAvailability
  console.log(change.available, change.fixtureIds);              // every fixture that changed
} catch (error) {
  if (isAvailabilityRefused(error)) console.log(error.code);     // 409: why the change is not allowed
  else throw error;
}
const API = "https://api.lovebowls.co.uk/v1";
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";
const headers = {
  "X-Api-Key": "lb_pk_...",               // your publishable key
  Authorization: `Bearer ${memberToken}`, // from the login
};

const list = await fetch(`${API}/clubs/${CLUB_ID}/me/availability`, { headers });
const { fixtureIds } = await list.json();

const res = await fetch(`${API}/clubs/${CLUB_ID}/me/availability/${fixtureId}`, { method: "PUT", headers }); // or "DELETE"
const body = await res.json();
if (res.status === 409) console.log(body.code); // refused: availability_not_enabled, teamsheet_published, ...
else if (!res.ok) throw new Error(body.code);
# the fixtures you are available for
curl "https://api.lovebowls.co.uk/v1/clubs/52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6/me/availability" \
  -H "X-Api-Key: $LB_API_KEY" \
  -H "Authorization: Bearer $LB_MEMBER_TOKEN"

# mark one; -X DELETE clears it
curl -X PUT "https://api.lovebowls.co.uk/v1/clubs/52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6/me/availability/$FIXTURE_ID" \
  -H "X-Api-Key: $LB_API_KEY" \
  -H "Authorization: Bearer $LB_MEMBER_TOKEN"
import os, requests

API = "https://api.lovebowls.co.uk/v1"
CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6"
headers = {
    "X-Api-Key": os.environ["LB_API_KEY"],  # a server key
    "Authorization": f"Bearer {os.environ['LB_MEMBER_TOKEN']}",
}

mine = requests.get(f"{API}/clubs/{CLUB_ID}/me/availability", headers=headers, timeout=10).json()["fixtureIds"]

r = requests.put(f"{API}/clubs/{CLUB_ID}/me/availability/{fixture_id}", headers=headers, timeout=10)  # or requests.delete
if r.status_code == 409:
    print("refused:", r.json()["code"])
else:
    r.raise_for_status()
    print(r.json()["fixtureIds"])

Live result

Log in to run this example.

Teamsheet

GET /v1/clubs/{clubId}/fixtures/{fixtureId}/teamsheet. The published team for a fixture: its rinks, each place with its position and player, and isMe on the member's own place. A fixture with teamsheetPublished: true has one; any other is 404 teamsheet_not_found.

// lb is the client from "Log in", with auth
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";

const sheet = await lb.getTeamsheet(CLUB_ID, fixtureId); // a fixture with teamsheetPublished: true
for (const rink of sheet.rinks) {
  console.log(rink.name);
  for (const p of rink.places) console.log(" ", p.positionName, p.name, p.isMe ? "(you)" : "");
}
const API = "https://api.lovebowls.co.uk/v1";
const CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6";
const headers = {
  "X-Api-Key": "lb_pk_...",               // your publishable key
  Authorization: `Bearer ${memberToken}`, // from the login
};

const res = await fetch(`${API}/clubs/${CLUB_ID}/fixtures/${fixtureId}/teamsheet`, { headers });
if (!res.ok) throw new Error((await res.json()).code); // 404 teamsheet_not_found
const sheet = await res.json();
curl "https://api.lovebowls.co.uk/v1/clubs/52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6/fixtures/$FIXTURE_ID/teamsheet" \
  -H "X-Api-Key: $LB_API_KEY" \
  -H "Authorization: Bearer $LB_MEMBER_TOKEN"
import os, requests

API = "https://api.lovebowls.co.uk/v1"
CLUB_ID = "52e5435c-8b5e-4e0d-abbb-6cb90b66f3b6"
headers = {
    "X-Api-Key": os.environ["LB_API_KEY"],  # a server key
    "Authorization": f"Bearer {os.environ['LB_MEMBER_TOKEN']}",
}

r = requests.get(f"{API}/clubs/{CLUB_ID}/fixtures/{fixture_id}/teamsheet", headers=headers, timeout=10)
r.raise_for_status()
for rink in r.json()["rinks"]:
    print(rink["name"], [p["name"] for p in rink["places"]])

Live result

Log in to run this example.

How this page works

The page holds a publishable key in a <meta name="lb-key"> tag. That key works only from this site's own address, so it is safe to show. The page is plain HTML and two small scripts: member.js runs the login and makes the calls, and ui.js draws the code tabs and the copy buttons. Read them and copy what you need.

Logging out forgets the token in this tab. It does not revoke it. See Authentication and Members.