CodeBlanks Logo Icon CodeBlanks

Summary - GitHub API

Combine API parts to set up an automated flow.

Automation with the GitHub API

The GitHub REST API can automate tasks such as retrieving repository information, authenticating a user, and managing organization resources. A script can run these operations in sequence to reduce repetitive work.

GET /user
GET /repos/{owner}/{repo}
GET /orgs/{org}/teams

Requests that require authentication use a token. Token permissions should cover only the operations that need to be performed.

Practice Create an authenticated API workflow

Use the GITHUB_TOKEN variable to send a GET request first to the GitHub user endpoint and then to the teams endpoint of the my-company organization. Use Bearer authentication and a JSON Accept header in both requests. Print the HTTP status code for each request.

Starter code

terminal
# Kullanıcı bilgilerini iste ve durum kodunu göster
# Organizasyon ekiplerini iste ve durum kodunu göster

Sample solution

terminal
curl -sS -o /dev/null -w "%{http_code}\n" -X GET -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" https://api.github.com/user
curl -sS -o /dev/null -w "%{http_code}\n" -X GET -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" https://api.github.com/orgs/my-company/teams

Expected output

200
200

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 An automation script must first authenticate a user with a token and then list an organization's teams. Which approach is suitable?

  • Run only local Git commands
  • Sequence REST API requests with an Authorization header Correct answer
  • Manually use a browser for every operation
  • Clone the repository again

Explanation: REST API requests can authenticate with a token in the Authorization header to create an automated workflow.

2 Complete authentication for the automation request

Question
curl -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" https://api.github.com/user____
Answer
curl -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" https://api.github.com/userAccept

Explanation: The Accept header specifies the response format expected from the API.

3 Parts of a GitHub API workflow

Term Meaning
REST API Accesses resources over HTTP
Token Carries authentication credentials
Organization Groups members and teams
Automation Runs repetitive requests automatically

All lessons in Expert