Search code examples
javascriptnode.jsgithubgithub-api

How do I get file names from a directory in a repository using Github API


I'm trying to make an electron app that gets the file names of a directory in a repository. I just don't know how to do this with the api. I know there is a way, I just don't know how.

For example:

I want to get the file names in the src/ directory of a github repository using the api.

I use axios to make api requests.


Solution

  • Use Get repository content endpoint, documented here

    Check out an example using Octokit

    List Repository contents                                                                                 View in Fusebit
    // If you don't send the path property, by default will send the contents from the root level
    const repoContent = await github.rest.repos.getContent({
      owner: 'repo-owner',
      repo: 'repo-name'
    });
      
    console.log('Files found at root level', repoContent.data.map((file) => file.name));