search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Overview of GitHub API Pagination Mechanism
Use octokit.paginate for efficient pagination (recommended)
Manually implement paging logic
Things to note
Home Web Front-end JS Tutorial How to efficiently obtain more than 100 user lists through GitHub API (pagination tutorial)

How to efficiently obtain more than 100 user lists through GitHub API (pagination tutorial)

Jan 21, 2026 am 07:00 AM

How to efficiently obtain more than 100 user lists through GitHub API (pagination tutorial)

This tutorial aims to solve the default 100 user limit issue encountered when using the GitHub API to get a list of users. We will introduce two main paging strategies in detail: utilizing the built-in paginate method of the Octokit library to implement automated paging, and manually implementing circular paging logic based on the since parameter. The article will provide clear code examples and highlight considerations for choosing appropriate methods in different scenarios, especially clarifying that the /users endpoint does not directly support username string filtering.

Overview of GitHub API Pagination Mechanism

When using the GitHub REST API to obtain a resource list, such as obtaining a user list (GET /users), the API usually sets a default upper limit on the number of returns, a common one is 100 records per page. This means that if you need to get more than 100 users, you must use paging mechanism.

For the GET /users endpoint, the GitHub API provides a parameter called since. This parameter accepts a user ID, and the API will return all users with an ID greater than the user ID, thereby achieving the purpose of obtaining subsequent data "starting from a certain point". By continuously updating the since parameter to the ID of the last user in the results on the previous page, we can gradually traverse the entire user list.

It is important to note that the GET /users endpoint returns users in ascending order of user IDs. It does not provide the function of fuzzy matching or filtering directly through username strings. If your goal is to search for users based on their names containing a specific string, you should use the GET /search/users endpoint, which supports more complex queries via the q parameter, such as q=username_substring in:login. This tutorial will focus on troubleshooting pagination issues with the GET /users endpoint.

For JavaScript/TypeScript projects that use the octokit library to interact with the GitHub API, the most recommended and convenient way to paginate is to use its built-in paginate method. The octokit.paginate method can automatically handle the logic of subsequent requests, including updating the since parameter, looping requests until all data is obtained, and aggregating all results into an array for return. This greatly simplifies writing pagination code.

The following is sample code using the octokit.paginate method:

 import { Octokit } from "octokit";

// Initialize the Octokit instance. If authentication is required, you can pass in the auth token here.
const octokit = new Octokit({
  // auth: 'YOUR_GITHUB_TOKEN' // If you need to access private data or increase the rate limit, please provide the authentication token
});

async function getAllGitHubUsers() {
  try {
    console.log("Getting all GitHub users through octokit.paginate...");

    // Use the paginate method to get all users // The per_page parameter specifies the number returned per page, the maximum is 100
    const allUsers = await octokit.paginate("GET /users", {
      per_page: 100, // Request 100 users per page headers: {
        "X-GitHub-Api-Version": "2022-11-28", // Specify API version},
    });

    console.log(`Successfully obtained ${allUsers.length} GitHub users.`);
    // console.log(allUsers); // Print all user data return allUsers;

  } catch (error) {
    console.error("An error occurred while obtaining the GitHub user:", error);
    throw error;
  }
}

// Call the function getAllGitHubUsers();

Code analysis:

  • new Octokit({}): Create an Octokit instance. Depending on your needs, you can pass in an authentication token (auth) to increase the API rate limit or access protected resources.
  • octokit.paginate("GET /users", { ... }): This is the core call. The first parameter is the API endpoint path. The second parameter is an object used to specify request parameters, such as per_page (number fetched per page, maximum 100) and headers (set API version, etc.).
  • The paginate method will automatically process requests for all pages and accumulate all results into an array and return them. When the list length returned by the API is less than per_page, paginate will consider that the end of the list has been reached and stop the request.

Manually implement paging logic

If you are not using the Octokit library, or wish to have more fine-grained control over the paging process, you can manually implement the looping paging logic. This approach involves calling the API repeatedly in a loop, updating the since parameter with each request, until all data is obtained.

The following is the simulation code to manually implement the paging logic:

 // This is a simulation implementation to illustrate the logic of manual paging // The actual production environment should be replaced with a real API request library (such as axios, fetch, etc.)

async function getAllGitHubUsersManually() {
  let allUsers = [];
  let lastUserId = 0; //Initially start from the first user (ID is 0 or less)
  const perPage = 100; // Number of requests per page let hasMoreUsers = true;

  console.log("Manually retrieving all GitHub users...");

  while (hasMoreUsers) {
    try {
      // Simulate API request // In actual application, this would be `await octokit.request('GET /users', { since: lastUserId, per_page: perPage, ... })`
      // Or `await fetch(`https://api.github.com/users?since=${lastUserId}&per_page=${perPage}`, { ... })`
      const response = await mockApiRequest(lastUserId, perPage); // Mock function const currentUsers = response.data.users; // Assume the data structure returned by the API if (currentUsers && currentUsers.length > 0) {
        allUsers = allUsers.concat(currentUsers);
        //Update lastUserId to the ID of the last user in the current batch
        lastUserId = currentUsers[currentUsers.length - 1].id;

        // If the number of users returned is less than perPage, it means the end of the list has been reached if (currentUsers.length  user.id > sinceId);
  if (startIndex === -1) {
    return { data: { users: [] } }; // No more users}

  const usersSlice = mockUsers.slice(startIndex, startIndex perPage);
  return { data: { users: usersSlice } };
}

// Call the function getAllGitHubUsersManually();

Code analysis:

  • allUsers: used to store all obtained user data.
  • lastUserId: records the ID of the last user in the previous request, used for the since parameter of the next request. The initial value is 0, which means starting from the user with the smallest ID.
  • while (hasMoreUsers): Loop condition to continue the request as long as there may be more users.
  • In each loop, construct an API request with since and per_page parameters.
  • Add the user data returned by the current request to the allUsers array.
  • Update lastUserId to the ID of the last user in the current batch.
  • if (currentUsers.length

Things to note

  1. API rate limiting (Rate Limiting): GitHub API has strict limits on request frequency. Frequent requests may result in your IP being temporarily banned. Be especially careful when using manual paging to ensure appropriate delays between requests, or use authentication tokens to increase the rate limit. octokit.paginate generally manages these limitations better.
  2. Error handling: In actual applications, complete error handling must be performed on API requests, such as network errors, error status codes returned by the API, etc.
  3. The difference between GET /users and GET /search/users: As mentioned earlier, GET /users is used to get the user list in order of ID.

The above is the detailed content of How to efficiently obtain more than 100 user lists through GitHub API (pagination tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to display last visited city and country of user on website How to display last visited city and country of user on website Mar 13, 2026 am 03:51 AM

The geographical location is obtained through the front-end and combined with the back-end storage to realize the dynamic prompt function of "the last visit was from XX city, XX country". It requires the help of IP location service, server-side persistence and front-end display logic.

The correct way to use express-validator for strong password verification The correct way to use express-validator for strong password verification Mar 09, 2026 am 03:33 AM

This article describes how to correctly configure the isStrongPassword option when using the express-validator library for strong password validation. Highlights a known issue with schema validation mode and provides detailed steps and code examples for using chained validation as an alternative to ensure passwords meet custom strength requirements.

Tailwind CSS dynamic class name invalidation problem: principle and solution Tailwind CSS dynamic class name invalidation problem: principle and solution Mar 07, 2026 am 12:30 AM

This article delves into the reason why Tailwind CSS cannot recognize dynamically generated class names (such as bg-[${variable}]) in React applications, mainly due to its JIT compiler's reliance on complete class names. The tutorial provides two effective solutions: one is to predefine the complete Tailwind class name in a variable, and the other is to use React's inline styles for specific CSS properties to help developers overcome dynamic style challenges and maintain code maintainability.

How to automate web console code execution without opening browser developer tools How to automate web console code execution without opening browser developer tools Mar 05, 2026 am 06:00 AM

This article introduces the use of browser extensions (such as Tampermonkey) to automatically run JavaScript code (such as debug.start()) when the page is loaded, without manually opening DevTools; it also explains why pure Python cannot directly operate the console of an opened web page, and possible automation alternatives.

How to combine multiple regular expressions into a replacement pattern that performs efficiently How to combine multiple regular expressions into a replacement pattern that performs efficiently Mar 13, 2026 am 12:03 AM

This article introduces how to safely combine multiple independent regular expressions (such as URL cleaning, specific pattern word filtering, special character deletion) into a single regular expression in JavaScript through logical or (|), and implement multiple rule cleaning in one replace() to avoid repeated string traversal.

How to use Destructuring assignment for objects in JavaScript? (ES6 features) How to use Destructuring assignment for objects in JavaScript? (ES6 features) Mar 05, 2026 am 02:39 AM

When deconstructing an object, if the property name is inconsistent with the variable name, you need to rename it with a colon, such as {firstName:first}; destructuring null/undefined will report a TypeError, so you should always use {}; the default value of the function parameter only takes effect for undefined, and null will still trigger destructuring failure.

How to uniformly sample a specified number of elements (such as 5) from an array How to uniformly sample a specified number of elements (such as 5) from an array Mar 13, 2026 am 02:42 AM

This article introduces an accurate and efficient algorithm for extracting a fixed number (such as 5) of elements that are as evenly distributed as possible from an array of any length, ensuring that the first and last elements must be selected, the middle elements are distributed proportionally, and the original order is maintained.

Complete tutorial on naturally sorting JavaScript arrays by numbers at the end of file names Complete tutorial on naturally sorting JavaScript arrays by numbers at the end of file names Mar 13, 2026 am 06:12 AM

This article explains in detail how to correctly numerically sort an array of file names containing increasing numeric suffixes, and solve the problem of 13810 < 13912 being misjudged as a larger problem caused by the default string sorting of Array.prototype.sort().

Related articles