GitHub API allows you to interact with GitHub programmatically, can be used to automate operations, get data, integrate into other systems.
Type
Description
Endpoint
REST API
Traditional RESTful API
https://api.github.com
GraphQL API
Flexible query language
https://api.github.com/graphql
1. Personal Access Token (PAT)
# Using curl
curl -H " Authorization: token YOUR_TOKEN" \
https://api.github.com/user
# Using gh command
gh api user
// OAuth authentication flow
// 1. Redirect to GitHub authorization page
// 2. User authorizes
// 3. Get access token
// 4. Use token to call API
// Using GitHub App authentication
// 1. Generate JWT
// 2. Get installation token
// 3. Use token to call API
# Using curl
curl https://api.github.com/users/octocat
# Using gh
gh api users/octocat
Get Repository Information
# Get repository
curl https://api.github.com/repos/octocat/Hello-World
# Using gh
gh api repos/octocat/Hello-World
curl -X POST \
-H " Authorization: token YOUR_TOKEN" \
-H " Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/issues \
-d ' {"title":"Bug report","body":"Description of the bug"}'
# Create Issue
gh api repos/OWNER/REPO/issues \
-f title=' Bug report' \
-f body=' Description of the bug'
# Create PR
gh api repos/OWNER/REPO/pulls \
-f title=' New feature' \
-f head=' feature-branch' \
-f base=' main'
Query Repository Information
query {
repository (owner : " octocat" , name : " Hello-World" ) {
name
description
stargazerCount
issues (first : 5 ) {
edges {
node {
title
state
}
}
}
}
}
gh api graphql -f query='
{
repository(owner: "octocat", name: "Hello-World") {
name
description
stargazerCount
}
}'
Endpoint
Description
GET /user
Get current user
GET /users/{username}
Get user information
GET /user/repos
Get user repositories
Endpoint
Description
GET /repos/{owner}/{repo}
Get repository information
POST /repos/{owner}/{repo}/issues
Create Issue
GET /repos/{owner}/{repo}/pulls
Get PR list
POST /repos/{owner}/{repo}/pulls
Create PR
Endpoint
Description
GET /orgs/{org}
Get organization information
GET /orgs/{org}/repos
Get organization repositories
GET /orgs/{org}/members
Get organization members
# Using curl
curl -I https://api.github.com/users/octocat | grep -i ' x-ratelimit'
# Using gh
gh api rate_limit
Error Code
Description
401
Unauthorized
403
Forbidden
404
Not Found
422
Unprocessable Entity
403
Rate Limited
{
"message" : " Not Found" ,
"documentation_url" : " https://docs.github.com/rest"
}
# Using API in GitHub Actions
- name : Create Issue
run : |
gh api repos/${{ github.repository }}/issues \
-f title='Build failed' \
-f body='Build ${{ github.run_id }} failed'
import requests
# Get repository statistics
response = requests .get (
'https://api.github.com/repos/octocat/Hello-World' ,
headers = {'Authorization' : 'token YOUR_TOKEN' }
)
data = response .json ()
print (f"Stars: { data ['stargazers_count' ]} " )
3. Integrate into Other Systems
// Integrate into Slack
const { WebClient } = require ( '@slack/web-api' ) ;
const github = require ( '@octokit/rest' ) ;
// Notify Slack when new PR is created
Use Authenticated Requests : Get higher rate limits
Cache Responses : Reduce API calls
Handle Rate Limits : Implement backoff retry
Use Pagination : Use pagination when getting large amounts of data
Validate Input : Ensure request data is valid
Error Handling : Properly handle API errors