> ## Content Index
> Fetch the complete content index at: https://codetraveler.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Introducing GitHubApiStatus
- URL: https://codetraveler.io/2021/01/12/introducing-githubapistatus/
- Published: 2021-01-12T22:47:02.000Z
- Updated: 2021-01-22T20:07:08.000Z
- Description: GitHub's API Rate Limits can be confusing and difficult to work with. This library makes it easy to check the rate limits with one line of code!
- Author: Brandon Minnick
- Tags: GitHub, API, C#, Rate Limit, Rate Limits, Rate, Limit, Xamarin, Blazor, ASP.NET, ASP.NET Core, iOS, Android, .NET, NuGet

### What is it? 

[GitHubApiStatus](https://github.com/brminnick/GitHubApiStatus?ref=codetraveler.io) is an [open-source library](https://github.com/brminnick/GitHubApiStatus?ref=codetraveler.io), [available on NuGet](https://github.com/brminnick/GitHubApiStatus/releases/tag/v2.0.0?ref=codetraveler.io), to make it easy to understand and leverage the [Rate Limits](https://github.com/brminnick/GitHubApiStatus?ref=codetraveler.io#github-api-rate-limits) for all of GitHub's APIs:

- GitHub's REST API
- GitHub's Search API
- GitHub's GraphQL API
- GitHub's Source Import API
- GitHub's App Manifest Configuration API

Each GitHub API has different rate limiting rules. For example, you may exceed the rate limit for GitHub's REST API, but that does not mean you've exceeded the rate limit for GitHub's GraphQL API. 

### Why Does This Exist?

If you've ever had to use the GitHub REST APIs, you may be familiar with [GitHub's API Rate Limits](https://docs.github.com/en/free-pro-team@latest/rest/overview/resources-in-the-rest-api?ref=codetraveler.io#rate-limiting):

![](https://storage.ghost.io/c/dc/c2/dcc2e43e-24f8-46bb-a899-9c446f3ef794/content/images/2021/01/image.png)

This means that if you call a GitHub API more than 5,000 times within a 60-minute window, even if you are authenticated, the GitHub APIs will return 403 FORBIDDEN.

But, did you also know that GitHub uses a [completely different rate limit for its GraphQL API](https://docs.github.com/en/free-pro-team@latest/graphql/overview/resource-limitations?ref=codetraveler.io#rate-limit):

![](https://storage.ghost.io/c/dc/c2/dcc2e43e-24f8-46bb-a899-9c446f3ef794/content/images/2021/01/image-1.png)

And to make matters EVEN MORE COMPLICATED, GitHub has different rate limit rules arounds its [Search APIs](https://docs.github.com/en/free-pro-team@latest/rest/reference/search?ref=codetraveler.io#rate-limit), its [Code Scanning APIs](https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/about-code-scanning?ref=codetraveler.io) and its [App Manifest Code Conversion APIs](https://docs.github.com/en/free-pro-team@latest/apps/building-github-apps/creating-github-apps-from-a-manifest/?ref=codetraveler.io#3-you-exchange-the-temporary-code-to-retrieve-the-app-configuration). 

When creating my open-source GitHub Insights mobile app, [GitTrends](https://codetraveler.io/2021/01/12/introducing-githubapistatus/GitTrends), I encountered some *very* frustrating bugs by not understanding the [GitHub API Rate Limits](https://github.com/brminnick/GitHubApiStatus?ref=codetraveler.io#github-api-rate-limits).

After learning the ins-and-outs of GitHub API Rate Limits, I wanted to share this expertise with the world. 

Hopefully this library helps prevent headaches from future developers using the GitHub APIs! 

### How Do I Use it? 

Initialize `GitHubApiStatusService` by passing in your [GitHub Personal Access Token](https://github.com/settings/tokens?ref=codetraveler.io):

```csharp
var gitHubApiStatusService = new GitHubApiStatusService(new AuthenticationHeaderValue("bearer", "Your GitHub Personal Access Token, e.g. 123456789012345"));
```

Then, call `Task<GitHubApiRateLimits> GitHubApiStatusService.GetApiRateLimits()` to see the current status of the GitHub API Rate Limits:

```csharp
var apiRateLimits = await gitHubApiStatusService.GetApiRateLimits();

// REST API Results
Console.WriteLine($"What is the GitHub REST API Rate Limit? {apiRateLimits.RestApi.RateLimit}"); // What is the GitHub REST API Rate Limit? 5000
Console.WriteLine($"How many REST API requests do I have remaining? {apiRateLimits.RestApi.RemainingRequestCount}"); // How many REST API requests do I have remaining? 4983
Console.WriteLine($"How long until the GitHub REST API Rate Limit resets? {apiRateLimits.RestApi.RateLimitReset_TimeRemaining}"); // How long until the GitHub REST API Rate Limit resets? 00:40:13.8035515
Console.WriteLine($"When does the GitHub REST API Rate Limit reset? {apiRateLimits.RestApi.RateLimitReset_DateTime}"); // When does the GitHub REST API Rate Limit reset? 10/29/2020 3:28:58 AM +00:00

Console.WriteLine();

// GraphQL API Results
Console.WriteLine($"What is the GitHub GraphQL API Rate Limit? {apiRateLimits.GraphQLApi.RateLimit}"); // What is the GitHub GraphQL API Rate Limit? 5000
Console.WriteLine($"How many GraphQL API requests do I have remaining? {apiRateLimits.GraphQLApi.RemainingRequestCount}"); // How many GraphQL API requests do I have remaining? 5000
Console.WriteLine($"How long until the GitHub GraphQL API Rate Limit resets? {apiRateLimits.GraphQLApi.RateLimitReset_TimeRemaining}"); // How long until the GitHub GraphQL API Rate Limit resets? 00:59:59.8034526
Console.WriteLine($"When does the GitHub GraphQL API Rate Limit reset? {apiRateLimits.GraphQLApi.RateLimitReset_DateTime}"); // When does the GitHub GraphQL API Rate Limit reset? 10/29/2020 3:48:44 AM +00:00
```

### How do I use it with ASP.NET Core?

First, install the [GitHubApiStatus.Extensions NuGet Package](https://www.nuget.org/packages/GitHubApiStatus.Extensions?ref=codetraveler.io).

In `ConfigureServices`, add it to the `IServiceCollection` using `services.AddGitHubApiStatusService(...)`, passing in your [GitHub Personal Access Token](https://github.com/settings/tokens?ref=codetraveler.io):

```csharp
public class Startup
{
    // ...

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGitHubApiStatusService(new AuthenticationHeaderValue("bearer", "[Your GitHub Personal Access Token, e.g. 123456789012345]"), new ProductHeaderValue("MyApp"))
            .ConfigurePrimaryHttpMessageHandler(config => new HttpClientHandler { AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate });

        services.AddRazorPages();
    }

    /// ...
    
}
```

Then reference `IGitHubApiStatusService` from any constructor:

```csharp
class MyPageModel : PageModel
{
    readonly ILogger<IndexModel> _logger;
    readonly IGitHubApiStatusService _gitHubApiStatusService;

    public MyPageModel(IGitHubApiStatusService gitHubApiStatusService, ILogger<MyPageModel> logger)
    {
        _logger = logger;
        _gitHubApiStatusService = gitHubApiStatusService;
    }

    // ...
}
```

### How do I use it with Blazor?

([Check out this open-source Blazor sample app](https://github.com/brminnick/GitHubApiStatus/tree/main/Src/GitStatus.Web?ref=codetraveler.io))

First, install the [GitHubApiStatus.Extensions NuGet Package](https://www.nuget.org/packages/GitHubApiStatus.Extensions?ref=codetraveler.io).

In `Program.cs`, add it to the `IServiceCollection` using `builder.Services.AddGitHubApiStatusService(...)`, passing in your [GitHub Personal Access Token](https://github.com/settings/tokens?ref=codetraveler.io):

```csharp
public class Program
{
    public static Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("#app");
        builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

        // AddGitHubApiStatusService 
        builder.Services.AddGitHubApiStatusService(new AuthenticationHeaderValue("bearer", "[Your GitHub Personal Access Token, e.g. 123456789012345]"), new ProductHeaderValue("YourAppName"))
            .ConfigurePrimaryHttpMessageHandler(config => new HttpClientHandler { AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate });

        return builder.Build().RunAsync();
    }
}
```

Then inject `IGitHubApiStatusService` into any Blazor page:

```csharp
@page "/graphql"
@using GitHubApiStatus
@inject IGitHubApiStatusService GitHubApiStatusService

<h1>GitHub REST Api Status</h1>

<p>@_graphQLApiStatus</p>

<button class="btn btn-primary" @onclick="GetGraphQLApiStatus">Get Status</button>

@code {
    string _graphQLApiStatus = string.Empty;

    async Task GetGraphQLApiStatus()
    {
        var apiRateLimitStatuses = await GitHubApiStatusService.GetApiRateLimits(System.Threading.CancellationToken.None).ConfigureAwait(false);
        _graphQLApiStatus = apiRateLimitStatuses.GraphQLApi.ToString();
    }
}
```

### Apps Using GitHubApiStatus

Here are a few open-source apps using GitHubApiStatus:

[GitTrends](https://gittrends.com/?ref=codetraveler.io) (Xamarin)

GitTrends is an open-source iOS and Android app that provides insights into your GitHub Repos. Quickly and easily review your total Views, Clones and Stars for each and understand the referring sites to your repositories! 

[GitStatus](https://github.com/brminnick/GitHubApiStatus/tree/main/Src/GitStatus?ref=codetraveler.io) (Xamarin)

GitStatus is an open-source iOS and Android app that displays your current rate limit for the GitHub REST API and the GitHub GraphQL API.

[GitStatus.Blazor](https://github.com/brminnick/GitHubApiStatus/tree/main/Src/GitStatus.Web?ref=codetraveler.io) (Blazor)

GitStatus.Blazor is an open-source website that displays your current rate limit for the GitHub REST API and the GitHub GraphQL API.