Pagination

Many API endpoints support pagination in the form of cursors. To ensure quick response times, pagination is enabled by default for these endpoints.

Endpoint

GET https://www.britelink.io/api/v1/drugs

Query Parameters

ParameterDescriptionRequiredDefault
cursorA unique identifier for the starting point of the query. Used for navigating through paginated results.OptionalNone (Fetches the first set of results)
BATCH_COUNTThe fixed number of results returned per page.No50
  • cursor (optional): A unique identifier for the starting point of the query. If omitted, the API fetches the first set of results.
  • BATCH_COUNT (default = 50): Number of results per page. This is a fixed point provided by the internal API and cannot be changed. Trying to change this will result in a Bad Request error. see more on Erros.

Request Example

curl -X GET 'https://www.britelink.io/api/v1/drugs?cursor=yourCursorValue' \
-H 'Authorization: Bearer YOUR_API_KEY'

Authorization Header

Every request must include the Authorization header with a valid API key.

HeaderDescription
AuthorizationBearer token for API access. Format: Bearer YOUR_API_KEY

Response Structure

The response includes a list of drugs and a nextCursor value, which is used to fetch the next set of results.

{
  "drugs": [
    /* array of drug objects */
  ],
  "nextCursor": "nextCursorValue"
}

Understanding the Response

  • drugs: An array of drug information objects.
  • nextCursor: A string used to fetch the next set of results. If absent, it signifies the end of the dataset.

Using Cursors for Pagination

  • Initially, to fetch the first set of results, omit the cursor parameter.
  • In subsequent requests, use the nextCursor from the current response as the cursor parameter to fetch the next results.
  • The cursor is an opaque string and should not be modified.
  • The presence of a nextCursor indicates more results are available. If absent, it signifies the end of the dataset.

Client-Side Implementation

Initial data fetching will provide both the data (drugs) and a nextCursor. Use the nextCursor to retrieve the next batch of results. Here's an updated example of the fetchData function:

const apiKey = process.env.BRITE_API_KEY;
let nextCursor = null;

const fetchData = async () => {
  try {
    const params = nextCursor ? { cursor: nextCursor } : {};
    const response = await axios.get("https://www.britelink.io/api/v1/drugs", {
      headers: { Authorization: `Bearer ${apiKey}` },
      params,
    });

    if (response.data && response.data.nextCursor) {
      nextCursor = response.data.nextCursor;
    }

    console.log(response.data);
  } catch (error) {
    console.error("Error fetching data:", error);
  }
};

fetchData(); // For initial data
// To fetch more data, you can call fetchData() again, such as on a button click or a scroll event.

Handling Larger Batch Requests

For automated pagination, particularly when fetching a large number of items (like 1000 drugs), a loop can be implemented on the client side to fetch all pages automatically. This loop continues to call the API using the nextCursor from the previous response, accumulating results until the desired number is reached or no more items are available.

Here's an example for automatically fetching up to 1000 drugs:

const apiKey = process.env.BRITE_API_KEY;
const MAX_DRUGS = 1000;

const fetchAllDrugs = async () => {
  let allDrugs = [];
  let nextCursor = null;
  let continueFetching = true;

  while (continueFetching) {
    try {
      const params = nextCursor ? { cursor: nextCursor } : {};
      const response = await axios.get(
        "https://www.britelink.io/api/v1/drugs",
        {
          headers: { Authorization: `Bearer ${apiKey}` },
          params,
        }
      );

      const { drugs, nextCursor: newCursor } = response.data;
      allDrugs.push(...drugs);
      nextCursor = newCursor;

      if (!nextCursor || allDrugs.length >= MAX_DRUGS) {
        continueFetching = false;
      }
    } catch (error) {
      console.error("Error fetching data:", error);
      continueFetching = false;
    }
  }

  return allDrugs.slice(0, MAX_DRUGS);
};

fetchAllDrugs().then((drugs) => {
  console.log(drugs); // Logs up to 1000 drugs
});

This approach ensures that users do not need to manually make subsequent calls for large data sets.