This article describes how to quickly get started with the GitHub REST API using GitHub CLI, JavaScript, or curl. For a more detailed guide, see "Getting started with the REST API."
Getting started using GitHub CLI
Using GitHub CLI in the command line
GitHub CLI is the easiest way to use the GitHub REST API from the command line.
Note: The following example is intended for GitHub.com. If you'd prefer to try the example using GitHub Enterprise Server, you must replace octocat/Spoon-Knife with a repository on your instance. Alternatively, rerun the gh auth login command to authenticate to GitHub.com instead of your instance.
-
Install GitHub CLI if you haven't installed it yet. For installation instructions, see the GitHub CLI repository.
-
Use the
auth loginsubcommand to authenticate to GitHub CLI. For more information, see the GitHub CLIauth logindocumentation.gh auth login -
Use the
apisubcommand to make your API request. For more information, see the GitHub CLIapidocumentation.gh api repos/octocat/Spoon-Knife/issues
Using GitHub CLI in GitHub Actions
You can also use GitHub CLI in your GitHub Actions workflows. For more information, see "Using GitHub CLI in workflows."
Instead of using the gh auth login command, pass an access token as an environment variable called GH_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 "Encrypted secrets."
Note: The following example workflows are intended for GitHub.com. If you'd prefer to try the examples using GitHub Enterprise Server, you must replace octocat/Spoon-Knife with a repository on GitHub Enterprise Server.
on:
workflow_dispatch:
jobs:
use_api:
runs-on: ubuntu-latest
permissions:
issues: read
steps:
- env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api repos/octocat/Spoon-Knife/issues
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 secret. In the following example, replace
APP_IDwith the name of the secret. You can find your app ID on the settings page for your app or through the API. For more information, see "GitHub Apps" in the REST API documentation. For more information about secrets, see "Encrypted secrets." -
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_PEMwith the name of the secret. For more information, see "Managing private keys for GitHub Apps." -
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:# This workflow uses actions that are not certified by GitHub. # They are provided by a third-party and are governed by # separate terms of service, privacy policy, and support # documentation. on: workflow_dispatch: jobs: track_pr: runs-on: ubuntu-latest steps: - name: Generate token id: generate_token uses: tibdex/github-app-token@c2055a00597a80f713b78b1650e8d3418f4d9a65 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.APP_PEM }} - name: Use API env: GH_TOKEN: ${{ steps.generate_token.outputs.token }} run: | gh api repos/octocat/Spoon-Knife/issues
Getting started using JavaScript
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."
Using Octokit.js
Note: The following example is intended for GitHub.com. If you'd prefer to try the example using GitHub Enterprise Server, you must replace octocat/Spoon-Knife with a repository on your instance. Alternatively, you can create a new Octokit instance without specifying baseURL.
-
Create an access token. For example, create a personal access token or a GitHub App user access token. For more information, see "Creating a personal access token" 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.
If these options are not possible, consider using another service such as the 1Password CLI 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
octokitin your script. For example,import { Octokit } from "octokit";. For other ways to importoctokit, see the Octokit.js README. -
Create an instance of
Octokitwith your token. ReplaceYOUR-TOKENwith your token.const octokit = new Octokit({ auth: 'YOUR-TOKEN' }); -
Use
octokit.requestto 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 example, in the following request the HTTP method isGET, the path is/repos/{owner}/{repo}/issues, and the parameters areowner: "octocat"andrepo: "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."
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 "Encrypted secrets."
Note: The following example is intended for GitHub.com. If you'd prefer to try the example using GitHub Enterprise Server, you must replace octocat/Spoon-Knife with a repository on your instance. Alternatively, you can create a new Octokit instance without specifying baseURL.
The following example workflow:
- Checks out the repository content
- Sets up Node.js
- Installs
octokit - Stores the value of
GITHUB_TOKENas an environment variable calledTOKENand runs.github/actions-scripts/use-the-api.mjs, which can access that environment variable asprocess.env.TOKEN
Example workflow:
on:
workflow_dispatch:
jobs:
use_api_via_script:
runs-on: ubuntu-latest
permissions: