Introduction
This article describes how to quickly get started with the GitHub REST API using GitHub CLI, curl
, or JavaScript. For a more detailed guide, see Getting started with the REST API.
Using Octokit.js
You can use Octokit.js to interact with the GitHub REST API in your JavaScript scripts. For more information, see Scripting with the REST API and JavaScript.
-
Create an access token. For example, create a personal access token or a GitHub App user access token. You will use this token to authenticate your request, so you should give it any scopes or permissions that are required to access that endpoint. For more information, see Authenticating to the REST API or Identifying and authorizing users for GitHub Apps.
Warning
Treat your access token like a password.
To keep your token secure, you can store your token as a secret and run your script through GitHub Actions. For more information, see the Using Octokit.js in GitHub Actions section.
You can also store your token as a Codespaces secret and run your script in Codespaces. For more information, see Managing encrypted secrets for your codespaces.
If these options are not possible, consider using another CLI service to store your token securely.
-
Install
octokit
. For example,npm install octokit
. For other ways to install or loadoctokit
, see the Octokit.js README. -
Import
octokit
in your script. For example,import { Octokit } from "octokit";
. For other ways to importoctokit
, see the Octokit.js README. -
Create an instance of
Octokit
with your token. ReplaceYOUR-TOKEN
with your token.JavaScript const octokit = new Octokit({ auth: 'YOUR-TOKEN' });
const octokit = new Octokit({ auth: 'YOUR-TOKEN' });
-
Use
octokit.request
to execute your request. Send the HTTP method and path as the first argument. Specify any path, query, and body parameters in an object as the second argument. For more information about parameters, see Getting started with the REST API.For example, in the following request the HTTP method is
GET
, the path is/repos/{owner}/{repo}/issues
, and the parameters areowner: "octocat"
andrepo: "Spoon-Knife"
.JavaScript await octokit.request("GET /repos/{owner}/{repo}/issues", { owner: "octocat", repo: "Spoon-Knife", });
await octokit.request("GET /repos/{owner}/{repo}/issues", { owner: "octocat", repo: "Spoon-Knife", });
Using Octokit.js in GitHub Actions
You can also execute your JavaScript scripts in your GitHub Actions workflows. For more information, see Workflow syntax for GitHub Actions.
Authenticating with an access token
GitHub recommends that you use the built-in GITHUB_TOKEN
instead of creating a token. If this is not possible, store your token as a secret and replace GITHUB_TOKEN
in the example below with the name of your secret. For more information about GITHUB_TOKEN
, see Automatic token authentication. For more information about secrets, see Using secrets in GitHub Actions.
The following example workflow:
- Checks out the repository content
- Sets up Node.js
- Installs
octokit
- Stores the value of
GITHUB_TOKEN
as an environment variable calledTOKEN
and runs.github/actions-scripts/use-the-api.mjs
, which can access that environment variable asprocess.env.TOKEN
on:
workflow_dispatch:
jobs:
use_api_via_script:
runs-on: ubuntu-latest
permissions:
issues: read
steps:
- name: Check out repo content
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '16.17.0'
cache: npm
- name: Install dependencies
run: npm install octokit
- name: Run script
run: |
node .github/actions-scripts/use-the-api.mjs
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
The following is an example JavaScript script with the file path .github/actions-scripts/use-the-api.mjs
.
import { Octokit } from "octokit"
const octokit = new Octokit({
auth: process.env.TOKEN
});
try {
const result = await octokit.request("GET /repos/{owner}/{repo}/issues", {
owner: "octocat",
repo: "Spoon-Knife",
});
const titleAndAuthor = result.data.map(issue => {title: issue.title, authorID: issue.user.id})
console.log(titleAndAuthor)
} catch (error) {
console.log(`Error! Status: ${error.status}. Message: ${error.response.data.message}`)
}
Authenticating with a GitHub App
If you are authenticating with a GitHub App, you can create an installation access token within your workflow:
-
Store your GitHub App's ID as a configuration variable. In the following example, replace
APP_ID
with the name of the configuration variable. You can find your app ID on the settings page for your app or through the App API. For more information, see REST API endpoints for GitHub Apps. For more information about configuration variables, see Store information in variables. -
Generate a private key for your app. Store the contents of the resulting file as a secret. (Store the entire contents of the file, including
-----BEGIN RSA PRIVATE KEY-----
and-----END RSA PRIVATE KEY-----
.) In the following example, replaceAPP_PEM
with the name of the secret. For more information, see Managing private keys for GitHub Apps. For more information about secrets, see Using secrets in GitHub Actions. -
Add a step to generate a token, and use that token instead of
GITHUB_TOKEN
. Note that this token will expire after 60 minutes. For example:on: workflow_dispatch: jobs: use_api_via_script: runs-on: ubuntu-latest steps: - name: Check out repo content uses: actions/checkout@v4 - name: Setup Node uses: actions/setup-node@v4 with: node-version: '16.17.0' cache: npm - name: Install dependencies run: npm install octokit - name: Generate token id: generate-token uses: actions/create-github-app-token@v1 with: app-id: ${{ vars.APP_ID }} private-key: ${{ secrets.APP_PEM }} - name: Run script run: | node .github/actions-scripts/use-the-api.mjs env: TOKEN: ${{ steps.generate-token.outputs.token }}
Next steps
For a more detailed guide, see Getting started with the REST API.