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:

VariableValue
baseUrlhttps://api.github.com
githubTokenyour 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:

JavaScript
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

HTTP
GET {{baseUrl}}/user
JavaScript
pm.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

HTTP
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/latest

Set 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:

HTTP
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"]}
JavaScript
pm.test('issue created', () => pm.response.to.have.status(201));
pm.environment.set('issueNumber', String(pm.response.json().number));

And clean up after yourself:

HTTP
PATCH {{baseUrl}}/repos/{{owner}}/{{repo}}/issues/{{issueNumber}}
Content-Type: application/json

{"state": "closed"}

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:

Output
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:

JavaScript
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);
HTTP
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:

JavaScript
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:

Terminal
requestarium run-collection "GitHub API" --env GitHub --env-var githubToken=GH_TEST_TOKEN

In GitHub Actions itself, you can use the job's built-in GITHUB_TOKEN for the repository the workflow runs in.