A Developer's Guide: Managing Rate Limits for the GitHub API

A Developer's Guide: Managing Rate Limits for the GitHub API

In this post, we'll explore how to keep your GitHub API requests in tune by managing installation rate limits, balancing app and user requests, and effectively handling GitHub tokens.

Eyal Solomon, Co-Founder & CEO

Eyal Solomon, Co-Founder & CEO

Rate Limits

API Consumption Management

The GitHub API is like a backstage pass to GitHub's vast capabilities, allowing you to automate tasks, integrate with other tools, and tailor GitHub's features to fit your unique workflow. However, like any exclusive access, there are house rules—specifically, rate limits that control how many API requests you can make within a certain timeframe. Understanding and respecting these limits is key to keeping your integrations running smoothly and avoiding any unexpected "access denied" moments.

Common use cases by industry:

The GitHub API serves as a versatile tool for various industries, enabling:

  • Security: Automating codebase scans to detect vulnerabilities, providing real-time alerts, and suggesting remediation steps.
  • Developer Tools: Integrating products into GitHub workflows, enhancing code analysis, and streamlining CI/CD pipelines.
  • Project Management Platforms: Synchronizing issues, pull requests, and project boards to ensure seamless alignment between development and management teams.
  • CI/CD Services: Triggering builds, running tests, and deploying applications automatically upon repository changes, optimizing the development pipeline.

Challenges GitHub's API Rate Limits

While the GitHub API offers extensive capabilities, it enforces multiple rate limits to ensure fair usage and maintain performance. These include primary rate limits, which cap the number of requests per hour based on authentication status, and secondary rate limits, which restrict behaviors like excessive concurrent requests. Developers must closely monitor their API usage to avoid exceeding these limits, as doing so can adversely affect product performance and lead to temporary access restrictions.

Understanding the structure of API request and response

When making a request to the GitHub API, the response includes both a body and headers. The headers provide essential metadata about the response and the current state of rate limits. Here's an example of a response from the GitHub API:

Request:

Response headers:

Response Body:

Explanation of Response Headers:

  1. Date: The date and time when the response was generated.
  2. Content-Type: Indicates that the response body is in JSON format.
  3. Content-Length: The size of the response body in bytes.
  4. X-RateLimit-Limit: The maximum number of requests you're permitted to make per hour.
  5. X-RateLimit-Remaining: The number of requests remaining in the current rate limit window.
  6. X-RateLimit-Reset: The time at which the current rate limit window resets, in Unix epoch time.
  7. ETag: A unique identifier representing the current version of the resource. Useful for caching and conditional requests.
  8. Cache-Control: Directives for caching mechanisms in both requests and responses.
  9. Vary: Indicates the request headers that determine whether a cached response can be used.
  10. X-GitHub-Media-Type: Specifies the GitHub API version and format.
  11. X-GitHub-Request-Id: A unique identifier for the request, useful for debugging and support.

Challenges Using GitHub API

Developers utilizing GitHub's REST API often encounter challenges related to rate limits, which are mechanisms GitHub employs to regulate the number of API requests a user or application can make within a specific timeframe. Understanding these limits is crucial to ensure seamless integration and prevent disruptions.

1. Primary and Secondary Rate Limits

GitHub enforces two types of rate limits:

  1. Primary Rate Limits: These define the maximum number of API requests allowed per hour. The limits vary based on the authentication method:some text
    • Unauthenticated Requests: Limited to 60 requests per hour.
    • Authenticated User Requests: Up to 5,000 requests per hour.
    • GitHub Apps: For apps installed on a GitHub Enterprise Cloud organization, the limit is 15,000 requests per hour.
  2. Secondary Rate Limits: These are additional constraints imposed to prevent abuse and ensure the API's availability for all users. Triggers for secondary rate limits include:some text
    • Making too many concurrent requests (more than 100).
    • Excessive requests to a single endpoint per minute.
    • High CPU time consumption by requests.
    • Rapid content creation on GitHub.

Exceeding secondary rate limits results in temporary restrictions, typically indicated by a 403 or 429 response status. (Resource: GitHub Docs)

2. GITHUB_TOKEN and Its Rate Limits

In GitHub Actions workflows, the GITHUB_TOKEN is a special token provided for authentication. Its rate limits are:

  • Standard Repositories: 1,000 requests per hour per repository.
  • GitHub Enterprise Cloud Accounts: 15,000 requests per hour per repository.

These limits are designed to accommodate the automation needs within GitHub Actions while preventing excessive load on the API. 

3. Understanding Points in Secondary Rate Limits

Secondary rate limits often utilize a point system to quantify the cost of different API requests:

  • REST API Requests:some text
    • GET, HEAD, OPTIONS: 1 point.
    • POST, PATCH, PUT, DELETE: 5 points.
  • GraphQL API Requests:some text
    • Without mutations: 1 point.
    • With mutations: 5 points.

Some endpoints may have different point costs, which are not publicly disclosed. Monitoring these points helps in managing the rate limits effectively. 

4. Challenges with GitHub App Installation Rate Limits

For GitHub Apps, rate limits can vary based on the installation context:

  • Non-GitHub Enterprise Cloud Organizations:some text
    • Base limit of 5,000 requests per hour.
    • Installations with more than 20 repositories receive an additional 50 requests per hour for each repository.
    • Installations on organizations with more than 20 users receive an additional 50 requests per hour for each user.
    • The rate limit cannot exceed 12,500 requests per hour.

This scaling mechanism requires developers to monitor the number of repositories and users associated with their app installations to accurately track and manage their rate limits. 

5. Balancing App and User Requests

When a GitHub App uses a user access token (as opposed to an installation access token), the rate limit is dictated by the authenticated user's primary rate limit. This means that all requests made by the user—whether directly, through another GitHub App, or via an OAuth app—share the same rate limit. Consequently, high usage from one source can impact the availability of the rate limit for others.

Best Practices to handle Github API Rate Limits

1. Monitor API Usage Across Users and Applications

Regularly track API calls to understand consumption patterns and identify potential issues. Utilize tools that provide insights into request volumes per user and application, enabling proactive management of rate limits. Lunar.dev's Custom Metrics Collection Flow facilitates the tracking of user-defined metrics for API requests and responses, such as request size or rate limits, offering real-time monitoring of API performance and usage patterns.

2. Implement Client-Side Rate Limiting Strategies

Enforce rate limiting on the client side to prevent exceeding GitHub's imposed limits. This approach ensures that API usage remains within acceptable thresholds, maintaining stable integration. Lunar.dev's Client-Side Limiting Flow regulates API traffic by enforcing predefined quotas within the flow, dynamically managing requests and providing appropriate responses when quotas are exceeded.

3. Set Up Alerts for Approaching Rate Limits

Establish alerting mechanisms to notify when API usage nears rate limits. This proactive measure allows for timely adjustments to prevent service interruptions. Integrating such alerts with monitoring systems ensures that teams are informed and can take corrective actions promptly.

4. Implement Retry Mechanisms with Exponential Backoff

Incorporate retry strategies that include exponential backoff when handling rate limit errors. This method reduces the likelihood of overwhelming the API and increases the chances of successful requests upon retry. By gradually increasing the wait time between retries, systems can adapt to temporary spikes in API usage without causing further strain.

5. Utilize Queuing to Smooth Traffic Peaks and Prioritize Requests

Implement queuing systems to manage and distribute API requests evenly, preventing sudden spikes that could lead to rate limit breaches. Prioritize critical requests to ensure essential operations are processed first. Lunar.dev's Priority Queue Flow manages API requests based on their priority, ensuring high-priority traffic is handled first while maintaining smooth processing for lower-priority requests.

6. Authenticate Requests

Always authenticate API requests to benefit from higher rate limits and enhanced security. Utilize personal access tokens, OAuth tokens, or GitHub App installations to authenticate, as authenticated requests are granted more generous rate limits compared to unauthenticated ones.

7. Optimize API Usage

Reduce the number of API calls by fetching only necessary data and utilizing efficient endpoints. Implement pagination to handle large datasets and cache responses when appropriate to minimize redundant requests.

By adhering to these best practices and leveraging Lunar.dev's solutions, developers can effectively manage GitHub API rate limits, ensuring reliable and efficient integration.

To conclude:

GitHub enforces rate limits to regulate the number of API requests you can make within a certain timeframe.

Effectively managing GitHub API rate limits is crucial for maintaining seamless integration and optimal performance. Implementing best practices, alongside strategies aligned with Lunar.dev's guidelines, can help prevent disruptions and ensure efficient API usage.

Ready to Start your journey?

Manage a single service and unlock API management at scale