Search code examples
githubgithub-api

GitHub API: Get Pinned Repositories


I was looking in the documentation but I couldn’t find how to do this. As the title says: How to get a user’s pinned repositories using the GitHub API ?


Solution

  • The pinned repositories data is not available on GitHub v3 API. But you can use GitHub's v4 GraphQL API to get the same with the GraphQL query below:

    {
      user(login: "GabrielBB") {
        pinnedItems(first: 6, types: REPOSITORY) {
          nodes {
            ... on Repository {
              name
            }
          }
        }
      }
    }
    

    The curl request for the above query:

    curl -L -X POST 'https://api.github.com/graphql' \
    -H 'Authorization: bearer <token>' \
    --data-raw '{"query":"{\n  user(login: \"GabrielBB\") {\n pinnedItems(first: 6, types: REPOSITORY) {\n nodes {\n ... on Repository {\n name\n }\n }\n }\n }\n}"
    '
    

    The response for the above request:

    {
        "data": {
            "user": {
                "pinnedItems": {
                    "nodes": [
                        {
                            "name": "xtate"
                        },
                        {
                            "name": "vscode-lombok"
                        },
                        {
                            "name": "react-use-session"
                        },
                        {
                            "name": "Android-CutOut"
                        },
                        {
                            "name": "xvfb-action"
                        },
                        {
                            "name": "Vehicles-API"
                        }
                    ]
                }
            }
        }
    }
    

    Note: You need to generate a token to access the GraphQL API, which you can generate by following the steps given here