How to test the GitHub REST API
The GitHub REST API is one of the best-documented APIs around, and one of the most used: release scripts, bots, dashboards and internal tools all call it. This guide sets it up in Requestarium with a token, a few useful requests and tests, so you can explore it and automate checks against it.
Step 1: Create a token
On GitHub, open Settings → Developer settings → Personal access tokens → Fine-grained tokens and generate one. Pick:
- Repository access: only the repositories you'll test with.
- Permissions: start with read-only, such as Contents: Read and Issues: Read. Add write access later, only if you need it.
- Expiration: short. 30 days is plenty for exploring.
Step 2: Set up the collection
Create a collection called GitHub API and an environment with two variables:
| Variable | Value |
|---|---|
baseUrl | https://api.github.com |
githubToken | your token, marked as secret |
On the collection's Authorization tab, choose Bearer token with {{githubToken}}. Every request inherits it.
GitHub also recommends two headers on every request. Add them to each request, or set them once in the collection's pre-request script:
pm.request.headers.set('Accept', 'application/vnd.github+json');
pm.request.headers.set('X-GitHub-Api-Version', '2022-11-28');Step 3: Check the token
GET {{baseUrl}}/userpm.test('token works', () => pm.response.to.have.status(200));
pm.test('signed in as the expected user', () => {
pm.expect(pm.response.json().login).to.equal(pm.environment.get('githubLogin'));
});A 401 means the token is wrong or expired. A 404 on a repository you know exists usually means the token can't see it: fine-grained tokens return 404, not 403, for repositories outside their access.
Step 4: Useful requests
GET {{baseUrl}}/repos/{{owner}}/{{repo}}
GET {{baseUrl}}/repos/{{owner}}/{{repo}}/issues?state=open&per_page=20
GET {{baseUrl}}/repos/{{owner}}/{{repo}}/pulls?state=open
GET {{baseUrl}}/repos/{{owner}}/{{repo}}/releases/latestSet owner and repo in the environment and switch repositories by switching environments.
To create something, use a test repository and a token with write access:
POST {{baseUrl}}/repos/{{owner}}/{{repo}}/issues
Content-Type: application/json
{"title": "API test {{$timestamp}}", "body": "Created by an API test. Safe to close.", "labels": ["test"]}pm.test('issue created', () => pm.response.to.have.status(201));
pm.environment.set('issueNumber', String(pm.response.json().number));And clean up after yourself:
PATCH {{baseUrl}}/repos/{{owner}}/{{repo}}/issues/{{issueNumber}}
Content-Type: application/json
{"state": "closed"}Step 5: Pagination with the Link header
List endpoints return up to 100 items per page (per_page=100), and the next page's URL is in the Link response header, not in the body:
Link: <https://api.github.com/repositories/1/issues?page=2>; rel="next", <https://api.github.com/repositories/1/issues?page=5>; rel="last"Pull the next URL out with a script and use it in the following request:
const link = pm.response.headers.get('Link') || '';
const next = /<([^>]+)>;\s*rel="next"/.exec(link);
pm.environment.set('nextPage', next ? next[1] : '');
pm.test('page is not empty', () => pm.expect(pm.response.json()).to.not.be.empty);GET {{nextPage}}Step 6: Watch the rate limit
Authenticated requests get 5,000 requests an hour for a personal token. Each response tells you where you stand:
pm.test('rate limit not nearly exhausted', () => {
const remaining = Number(pm.response.headers.get('x-ratelimit-remaining'));
pm.expect(remaining).to.be.above(100);
});When you run out, GitHub answers 403 or 429 with x-ratelimit-remaining: 0. The reset time is in x-ratelimit-reset, as a Unix timestamp. More in Testing rate limits and 429 responses.
Run it from CI
If you build on top of the GitHub API, a scheduled run catches breaking changes and expired tokens before your users do:
requestarium run-collection "GitHub API" --env GitHub --env-var githubToken=GH_TEST_TOKENIn GitHub Actions itself, you can use the job's built-in GITHUB_TOKEN for the repository the workflow runs in.