Search code examples
githubgithub-api

GitHub API find all files with extension


I want to get links of all GitHub files with specific extension. I found out that I can use extension:bin to search for all *.bin files, URL https://github.com/search?q=extension%3Abin&type=Code

I found Using GitHub API to retrieve files with specific extension but this answer deals with specific repository.

Update: I was trying https://api.github.com/search/code?q=extension:ifc+size:1000..1500 but response is

{
    "message": "Validation Failed",
    "errors": [
        {
            "message": "Must include at least one user, organization, or repository",
        }
    ]
}

Do we have a method in GitHub API to search in all repositories? I didn't find here https://docs.github.com/en/rest/search#search-code


Solution

  • Root cause analysis

    I have reproduced the issue by performing an HTTP request without the authorization header:

    $ curl \
        --request GET \
        --header 'Accept: application/vnd.github+json' \
        'https://api.github.com/search/code?q=extension:ifc+size:1000..1500'
    {
      "message": "Validation Failed",
      "errors": [
        {
          "message": "Must include at least one user, organization, or repository",
          "resource": "Search",
          "field": "q",
          "code": "invalid"
        }
      ],
      "documentation_url": "https://docs.github.com/v3/search/"
    }
    

    Root cause

    Therefore, it looks like you have missed the authorization header.

    Solution

    Perform an HTTP request with the appropriate authorization header.

    Draft example command line

    curl \
        --request GET \
        --header 'Accept: application/vnd.github+json' \
        --header 'Authorization: Bearer <TOKEN>' \
        'https://api.github.com/search/code?q=extension:ifc+size:1000..1500'
    

    The example command line has worked fine (returned search results) for me.

    Please, replace <TOKEN> with a GitHub personal access token.

    Additional references