The GitHub API enables developers to programmatically interact with GitHub's platform, allowing automation of tasks like managing repositories, issues, pull requests, and user data. It offers two main versions: the REST API v3 (widely used) and the GraphQL API v4, which provides more flexibility for complex queries.
Key Features
REST API: Follows standard HTTP methods and is ideal for straightforward interactions. For example, fetching user data:
fetch('https://api.github.com/users/octocat') .then(response => response.json()) .then(data => console.log(data));GraphQL API: Allows precise data retrieval with custom queries, reducing over-fetching. It’s particularly useful for complex data needs.
Authentication
Use a Personal Access Token (PAT) for authenticated requests. Generate one at GitHub Settings > Developer Settings > Personal Access Tokens.
Include the token in the
Authorizationheader:-H "Authorization: Bearer YOUR_TOKEN"
Documentation & Resources
Official REST API Docs: docs.github.com/en/rest
GitHub Topics: Explore community projects using
github-apitag: github.com/topics/github-apiFree API Lists: Discover other public APIs at public-apis/public-apis
Pricing & Limits
Free tier: Unlimited public API calls; rate-limited for private repositories and authenticated users (60 requests/hour without token, 5,000/hour with token).
Enterprise: Higher limits and advanced features via GitHub Enterprise Cloud or Server.
Use Cases
Automate repository creation and branch management.
Sync code from external tools (e.g., LeetCode to GitHub).
Build CI/CD integrations, monitoring dashboards, or profile stats generators.
For full details, refer to the GitHub REST API documentation.
How to get a file via GitHub APIs - Stack Overflow
Creating release with api
Exposed API key
Help with Github API and Github GraphQL API
Videos
As the description (located at http://developer.github.com/v3/repos/contents/) says:
/repos/:owner/:repo/contents/:path
An ajax code will be:
$.ajax({
url: readme_uri,
dataType: 'jsonp',
success: function(results)
{
var content = results.data.content;
});
Replace the readme_uri by the proper /repos/:owner/:repo/contents/:path.
This GitHub API page provides the full reference. The API endpoint for reading a file:
https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}
{
"encoding": "base64",
"size": 5362,
"name": "README.md",
"content": "encoded content ...",
"sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
...
}
- Consider using a personal access token
- Rate-limits (up to 60 per-hour for anonymous, up to 5,000 per-hour for authenticated) read more
- Enable accessing files in private repos
- The file content in the response is base64 encoded string
Using curl
Reading https://github.com/airbnb/javascript/blob/master/package.json using GitHub's API via curl:
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/airbnb/javascript/contents/package.json
- Make sure to pass header
Accept: application/vnd.github.v3.rawto get raw file response (thanks jakub.g)
Using Python
Reading https://github.com/airbnb/javascript/blob/master/package.json using GitHub's API in Python:
import base64
import json
import requests
import os
def github_read_file(username, repository_name, file_path, github_token=None):
headers = {}
if github_token:
headers['Authorization'] = f"token {github_token}"
url = f'https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}'
r = requests.get(url, headers=headers)
r.raise_for_status()
data = r.json()
file_content = data['content']
file_content_encoding = data.get('encoding')
if file_content_encoding == 'base64':
file_content = base64.b64decode(file_content).decode()
return file_content
def main():
github_token = os.environ['GITHUB_TOKEN']
username = 'airbnb'
repository_name = 'javascript'
file_path = 'package.json'
file_content = github_read_file(username, repository_name, file_path, github_token=github_token)
data = json.loads(file_content)
print(data['name'])
if __name__ == '__main__':
main()
- Define an environment variable
GITHUB_TOKENbefore running