Retrieving information about the latest active Daily meeting session

Options

Quick TypeScript snippet to retrieve info about the latest active Daily meeting session for a given room name via our REST API. I'm trying out a version of this in a Netlify func to implement some admin webinar functionality, to let hosts see who is already in a webinar room before they join.

// getActiveMeetingSession() uses Daily's REST API to get information about the latest
// active meeting session in a given Daily room. It will return either an empty
// JSON object, or an object containing active session information.
async function getActiveMeetingSession(
  apiKey: string,
  roomName: string
): Promise<string> {
  // Prepare our headers, containing our Daily API key
  const headers = {
    Authorization: `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  };

  // Construct URL for our request
  // See https://docs.daily.co/reference/rest-api/meetings/get-meeting-information
  const url = `https://api.daily.co/v1/meetings/?room=${roomName}&limit=1`;

  const sessionsErrMsg = 'failed to get meeting sessions';
  // Make GET request to given URL with our Authorization headers
  const res = await axios.get(url, { headers });

  // Check response code and existence of body
  if (res.status !== 200 || !res.data) {
    console.error(`unexpected meeting session response: Status: ${res.status}`);
    throw new Error(sessionsErrMsg);
  }

  const sessionData = res.data.data;
  if (!sessionData || sessionData.length === 0) {
    return '{}';
  }
  const lastSession = sessionData[0];
  // We only want the latest active session, so return
  // empty JSON object if last session is not ongoing.
  if (!lastSession.ongoing) {
    return '{}';
  }
  return lastSession;
}


Tagged: