20 KiB
title, intro, allowTitleToDifferFromFilename, versions, shortTitle, topics, redirect_from
| title | intro | allowTitleToDifferFromFilename | versions | shortTitle | topics | redirect_from | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Quickstart for GitHub REST API | Learn how to get started with the {% data variables.product.prodname_dotcom %} REST API. | true |
|
Quickstart |
|
|
This article describes how to quickly get started with the {% data variables.product.prodname_dotcom %} REST API using {% data variables.product.prodname_cli %}, JavaScript, or curl. For a more detailed guide, see "AUTOTITLE."
{% cli %}
Getting started using {% data variables.product.prodname_cli %}
Using {% data variables.product.prodname_cli %} in the command line
{% data variables.product.prodname_cli %} is the easiest way to use the {% data variables.product.prodname_dotcom %} REST API from the command line.
{% ifversion ghes or ghae %} {% note %}
Note: The following example is intended for {% data variables.product.prodname_dotcom_the_website %}. If you'd prefer to try the example using {% data variables.product.product_name %}, you must replace octocat/Spoon-Knife with a repository on {% ifversion ghes %}your instance{% elsif ghae %}{% data variables.product.product_name %}{% endif %}. Alternatively, rerun the gh auth login command to authenticate to {% data variables.product.prodname_dotcom_the_website %} instead of {% ifversion ghes %}your instance{% elsif ghae %}{% data variables.product.product_name %}{% endif %}.
{% endnote %} {% endif %}
-
Install {% data variables.product.prodname_cli %} if you haven't installed it yet. For installation instructions, see the {% data variables.product.prodname_cli %} repository.
-
Use the
auth loginsubcommand to authenticate to {% data variables.product.prodname_cli %}. For more information, see the {% data variables.product.prodname_cli %}auth logindocumentation.gh auth login -
Use the
apisubcommand to make your API request. For more information, see the {% data variables.product.prodname_cli %}apidocumentation.gh api repos/octocat/Spoon-Knife/issues
Using {% data variables.product.prodname_cli %} in {% data variables.product.prodname_actions %}
You can also use {% data variables.product.prodname_cli %} in your {% data variables.product.prodname_actions %} workflows. For more information, see "AUTOTITLE."
Instead of using the gh auth login command, pass an access token as an environment variable called GH_TOKEN. {% data variables.product.prodname_dotcom %} 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 "AUTOTITLE." For more information about secrets, see "AUTOTITLE."
{% ifversion ghes or ghae %} {% note %}
Note: The following example workflows are intended for {% data variables.product.prodname_dotcom_the_website %}. If you'd prefer to try the examples using {% data variables.product.product_name %}, you must replace octocat/Spoon-Knife with a repository on {% data variables.product.product_name %}.
{% endnote %} {% endif %}
on:
workflow_dispatch:
jobs:
use_api:
runs-on: ubuntu-latest
permissions:
issues: read
steps:
- env:
GH_TOKEN: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
run: |
gh api repos/octocat/Spoon-Knife/issues
If you are authenticating with a {% data variables.product.prodname_github_app %}, you can create an installation access token within your workflow:
-
Store your {% data variables.product.prodname_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 "AUTOTITLE" in the REST API documentation. For more information about secrets, see "AUTOTITLE." -
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 "AUTOTITLE." -
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:{% data reusables.actions.actions-not-certified-by-github-comment %} 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: {% raw %}${{ secrets.APP_ID }}{% endraw %} private_key: {% raw %}${{ secrets.APP_PEM }}{% endraw %} - name: Use API env: GH_TOKEN: {% raw %}${{ steps.generate_token.outputs.token }}{% endraw %} run: | gh api repos/octocat/Spoon-Knife/issues
{% endcli %}
{% javascript %}
Getting started using JavaScript
You can use Octokit.js to interact with the {% data variables.product.prodname_dotcom %} REST API in your JavaScript scripts. For more information, see "Scripting with the REST API and JavaScript."
Using Octokit.js
{% data reusables.rest-api.quickstart-location-javascript-admonition %}
-
Create an access token. For example, create a {% data variables.product.pat_generic %} or a {% data variables.product.prodname_github_app %} user access token. For more information, see "Creating a {% data variables.product.pat_generic %}" or "Identifying and authorizing users for GitHub Apps."
{% warning %}
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 {% data variables.product.prodname_actions %}. For more information, see the "Using Octokit.js in {% data variables.product.prodname_actions %}" section.
{%- ifversion fpt or ghec %}
You can also store your token as a {% data variables.product.prodname_codespaces %} secret and run your script in {% data variables.product.prodname_codespaces %}. For more information, see "Managing encrypted secrets for your codespaces."{% endif %}
If these options are not possible, consider using another CLI service to store your token securely.
{% endwarning %}
-
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 {% data variables.product.prodname_actions %}
You can also execute your JavaScript scripts in your {% data variables.product.prodname_actions %} workflows. For more information, see "AUTOTITLE."
{% data variables.product.prodname_dotcom %} 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 "AUTOTITLE." For more information about secrets, see "AUTOTITLE."
{% data reusables.rest-api.quickstart-location-javascript-admonition %}
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:
issues: read
steps:
- name: Check out repo content
uses: {% data reusables.actions.action-checkout %}
- name: Setup Node
uses: {% data reusables.actions.action-setup-node %}
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: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
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}`)
}
If you are authenticating with a {% data variables.product.prodname_github_app %}, you can create an installation access token within your workflow:
-
Store your {% data variables.product.prodname_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 App API. For more information, see "AUTOTITLE." For more information about secrets, see "AUTOTITLE." -
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 "AUTOTITLE." -
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:{% data reusables.actions.actions-not-certified-by-github-comment %} on: workflow_dispatch: jobs: use_api_via_script: runs-on: ubuntu-latest steps: - name: Check out repo content uses: {% data reusables.actions.action-checkout %} - name: Setup Node uses: {% data reusables.actions.action-setup-node %} with: node-version: '16.17.0' cache: npm - name: Install dependencies run: npm install octokit - name: Generate token id: generate_token uses: tibdex/github-app-token@c2055a00597a80f713b78b1650e8d3418f4d9a65 with: app_id: {% raw %}${{ secrets.APP_ID }}{% endraw %} private_key: {% raw %}${{ secrets.APP_PEM }}{% endraw %} - name: Run script run: | node .github/actions-scripts/use-the-api.mjs env: TOKEN: {% raw %}${{ steps.generate_token.outputs.token }}{% endraw %}
{% endjavascript %}
{% curl %}
Getting started using curl
Using curl in the command line
{% ifversion ghes or ghae %} {% note %}
Notes:
- The following example is intended for {% data variables.product.prodname_dotcom_the_website %}. If you'd prefer to try the example using {% data variables.product.product_name %}, you must replace
https://api.github.comwith{% data variables.product.api_url_code %}, and replaceHOSTNAMEwith the hostname for {% ifversion ghes %}{% data variables.location.product_location %}{% elsif ghae %}{% data variables.product.product_name %}{% endif %}. You must also replaceoctocat/Spoon-Knifewith a repository on {% data variables.product.product_name %}. - If you want to make API requests from the command line, {% data variables.product.prodname_dotcom %} recommends that you use {% data variables.product.prodname_cli %}, which simplifies authentication and requests. For more information about getting started with the REST API using {% data variables.product.prodname_cli %}, see the {% data variables.product.prodname_cli %} version of this article.
{% endnote %} {% endif %}
-
Install
curlif it isn't already installed on your machine. To check ifcurlis installed, executecurl --versionin the command line. If the output is information about the version ofcurl, it is installed. If you get a message similar tocommand not found: curl, you need to download and installcurl. For more information, see the curl project download page. -
Create an access token. For example, create a {% data variables.product.pat_generic %} or a {% data variables.product.prodname_github_app %} user access token. For more information, see "Creating a {% data variables.product.pat_generic %}" or "Identifying and authorizing users for GitHub Apps."
{% warning %}
Warning: Treat your access token like a password.
{%- ifversion fpt or ghec %}
To keep your token secure, you can store your token as a {% data variables.product.prodname_codespaces %} secret and use the command line through {% data variables.product.prodname_codespaces %}. For more information, see "Managing encrypted secrets for your codespaces."{% endif %}
You can also use {% data variables.product.prodname_cli %} instead of
curl. {% data variables.product.prodname_cli %} will take care of authentication for you. For more information, see the {% data variables.product.prodname_cli %} version of this page.If these options are not possible, consider using another CLI service to store your token securely.
{% endwarning %}
-
Use the
curlcommand to make your request. Pass your token in anAuthorizationheader. ReplaceYOUR-TOKENwith your token.curl --request GET \ --url "https://api.github.com/repos/octocat/Spoon-Knife/issues" \ --header "Accept: application/vnd.github+json" \ --header "Authorization: Bearer YOUR-TOKEN"{% note %}
Note: {% data reusables.getting-started.bearer-vs-token %}
{% endnote %}
Using curl commands in {% data variables.product.prodname_actions %}
You can also use curl commands in your {% data variables.product.prodname_actions %} workflows.
{% data variables.product.prodname_dotcom %} 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 "AUTOTITLE." For more information about secrets, see "AUTOTITLE."
{% ifversion ghes or ghae %} {% note %}
Note: The following example workflows are intended for {% data variables.product.prodname_dotcom_the_website %}. If you'd prefer to try the examples using {% data variables.product.product_name %}, note the following differences.
- You must replace
https://api.github.comwith{% data variables.product.api_url_code %}, and replaceHOSTNAMEwith the hostname for {% ifversion ghes %}{% data variables.location.product_location %}{% elsif ghae %}{% data variables.product.product_name %}{% endif %}. - You must replace
octocat/Spoon-Knifewith a repository on {% data variables.product.product_name %}.
{% endnote %} {% endif %}
on:
workflow_dispatch:
jobs:
use_api:
runs-on: ubuntu-latest
permissions:
issues: read
steps:
- env:
GH_TOKEN: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
run: |
curl --request GET \
--url "https://api.github.com/repos/octocat/Spoon-Knife/issues" \
--header "Accept: application/vnd.github+json" \
--header "Authorization: Bearer $GH_TOKEN"
If you are authenticating with a {% data variables.product.prodname_github_app %}, you can create an installation access token within your workflow:
-
Store your {% data variables.product.prodname_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 App API. For more information, see "AUTOTITLE." For more information about secrets, see "AUTOTITLE." -
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 "AUTOTITLE." -
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:{% data reusables.actions.actions-not-certified-by-github-comment %} on: workflow_dispatch: jobs: use_api: runs-on: ubuntu-latest steps: - name: Generate token id: generate_token uses: tibdex/github-app-token@c2055a00597a80f713b78b1650e8d3418f4d9a65 with: app_id: {% raw %}${{ secrets.APP_ID }}{% endraw %} private_key: {% raw %}${{ secrets.APP_PEM }}{% endraw %} - name: Use API env: GH_TOKEN: {% raw %}${{ steps.generate_token.outputs.token }}{% endraw %} run: | curl --request GET \ --url "https://api.github.com/repos/octocat/Spoon-Knife/issues" \ --header "Accept: application/vnd.github+json" \ --header "Authorization: Bearer $GH_TOKEN"
{% endcurl %}
Next steps
For a more detailed guide, see "Getting started with the REST API."