{"meta":{"title":"Быстрый старт для GitHub REST API","intro":"Узнайте, как начать работу с REST API GitHub.","product":"REST API","breadcrumbs":[{"href":"/ru/rest","title":"REST API"},{"href":"/ru/rest/quickstart","title":"Быстрое начало"}],"documentType":"article"},"body":"# Быстрый старт для GitHub REST API\n\nУзнайте, как начать работу с REST API GitHub.\n\n<!-- TRANSLATION_FALLBACK prop=markdown type=TokenizationError line=312 col=36 msg=\"raw '{% raw %}${{ secrets.APP_PEM ...' not closed, line:312, col:36\" -->\n## Introduction\n\nThis 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](/en/rest/guides/getting-started-with-the-rest-api).\n\n<div class=\"ghd-tool cli\">\n\n## Using GitHub CLI in the command line\n\nGitHub CLI is the easiest way to use the GitHub REST API from the command line.\n\n1. Install GitHub CLI on macOS, Windows, or Linux. For more information, see [Installation](https://github.com/cli/cli?ref_product=cli\\&ref_type=engagement\\&ref_style=text#installation) in the GitHub CLI repository.\n\n2. To authenticate to GitHub, run the following command from your terminal.\n\n   ```shell\n   gh auth login\n   ```\n\n3. Select where you want to authenticate to:\n\n   * If you access GitHub at GitHub.com, select **GitHub.com**.\n   * If you access GitHub at a different domain, select **Other**, then enter your hostname (for example: `octocorp.ghe.com`).\n\n4. Follow the rest of the on-screen prompts.\n\n   GitHub CLI automatically stores your Git credentials for you when you choose HTTPS as your preferred protocol for Git operations and answer \"yes\" to the prompt asking if you would like to authenticate to Git with your GitHub credentials. This can be useful as it allows you to use Git commands like `git push` and `git pull` without needing to set up a separate credential manager or use SSH.\n\n5. Make a request using the GitHub CLI `api` subcommand, followed by the path. Use the `--method` or `-X` flag to specify the method. For more information, see the [GitHub CLI `api` documentation](https://cli.github.com/manual/gh_api).\n\n   This example makes a request to the \"Get Octocat\" endpoint, which uses the method `GET` and the path `/octocat`. For the full reference documentation for this endpoint, see [REST API endpoints for meta data](/en/rest/meta/meta#get-octocat).\n\n   ```shell copy\n   gh api /octocat --method GET\n   ```\n\n## Using GitHub CLI in GitHub Actions\n\nYou can also use GitHub CLI in your GitHub Actions workflows. For more information, see [Using GitHub CLI in workflows](/en/actions/using-workflows/using-github-cli-in-workflows).\n\n### Authenticating with an access token\n\nInstead 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 [Use GITHUB\\_TOKEN for authentication in workflows](/en/actions/security-guides/automatic-token-authentication). For more information about secrets, see [Using secrets in GitHub Actions](/en/actions/security-guides/encrypted-secrets).\n\nThe following example workflow uses the [List repository issues](/en/rest/issues/issues#list-repository-issues) endpoint, and requests a list of issues in the `octocat/Spoon-Knife` repository.\n\n```yaml copy\non:\n  workflow_dispatch:\njobs:\n  use_api:\n    runs-on: ubuntu-latest\n    permissions:\n      issues: read\n    steps:\n      - env:\n          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n        run: |\n          gh api https://api.github.com/repos/octocat/Spoon-Knife/issues\n```\n\n### Authenticating with a GitHub App\n\nIf you are authenticating with a GitHub App, you can create an installation access token within your workflow:\n\n1. 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 API. For more information, see [REST API endpoints for GitHub Apps](/en/rest/apps/apps#get-an-app). For more information about configuration variables, see [Store information in variables](/en/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows).\n2. 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, replace `APP_PEM` with the name of the secret. For more information, see [Managing private keys for GitHub Apps](/en/apps/creating-github-apps/authenticating-with-a-github-app/managing-private-keys-for-github-apps). For more information about secrets, see [Using secrets in GitHub Actions](/en/actions/security-guides/encrypted-secrets).\n3. 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:\n\n   ```yaml copy\n   on:\n     workflow_dispatch:\n   jobs:\n     track_pr:\n       runs-on: ubuntu-latest\n       steps:\n         - name: Generate token\n           id: generate-token\n           uses: actions/create-github-app-token@v2\n           with:\n             app-id: ${{ vars.APP_ID }}\n             private-key: ${{ secrets.APP_PEM }}\n         - name: Use API\n           env:\n             GH_TOKEN: ${{ steps.generate-token.outputs.token }}\n           run: |\n             gh api https://api.github.com/repos/octocat/Spoon-Knife/issues\n   ```\n\n</div>\n\n<div class=\"ghd-tool javascript\">\n\n## Using Octokit.js\n\nYou 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](/en/rest/guides/scripting-with-the-rest-api-and-javascript).\n\n1. 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](/en/rest/overview/authenticating-to-the-rest-api) or [Identifying and authorizing users for GitHub Apps](/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps).\n\n   > \\[!WARNING]\n   > Treat your access token like a password.\n   >\n   > 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](#using-octokitjs-in-github-actions) section.\n\n   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](/en/codespaces/managing-your-codespaces/managing-encrypted-secrets-for-your-codespaces).\n\n   > If these options are not possible, consider using another CLI service to store your token securely.\n\n2. Install `octokit`. For example, `npm install octokit`. For other ways to install or load `octokit`, see [the Octokit.js README](https://github.com/octokit/octokit.js/#readme).\n\n3. Import `octokit` in your script. For example, `import { Octokit } from \"octokit\";`. For other ways to import `octokit`, see [the Octokit.js README](https://github.com/octokit/octokit.js/#readme).\n\n4. Create an instance of `Octokit` with your token. Replace `YOUR-TOKEN` with your token.\n\n   ```javascript copy\n   const octokit = new Octokit({ \n     auth: 'YOUR-TOKEN'\n   });\n   ```\n\n5. 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](/en/rest/guides/getting-started-with-the-rest-api#using-parameters).\n\n   For example, in the following request the HTTP method is `GET`, the path is `/repos/{owner}/{repo}/issues`, and the parameters are `owner: \"octocat\"` and `repo: \"Spoon-Knife\"`.\n\n   ```javascript copy\n   await octokit.request(\"GET /repos/{owner}/{repo}/issues\", {\n     owner: \"octocat\",\n     repo: \"Spoon-Knife\",\n   });\n   ```\n\n## Using Octokit.js in GitHub Actions\n\nYou can also execute your JavaScript scripts in your GitHub Actions workflows. For more information, see [Workflow syntax for GitHub Actions](/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun).\n\n### Authenticating with an access token\n\nGitHub 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 [Use GITHUB\\_TOKEN for authentication in workflows](/en/actions/security-guides/automatic-token-authentication). For more information about secrets, see [Using secrets in GitHub Actions](/en/actions/security-guides/encrypted-secrets).\n\nThe following example workflow:\n\n1. Checks out the repository content\n2. Sets up Node.js\n3. Installs `octokit`\n4. Stores the value of `GITHUB_TOKEN` as an environment variable called `TOKEN` and runs `.github/actions-scripts/use-the-api.mjs`, which can access that environment variable as `process.env.TOKEN`\n\n```yaml\non:\n  workflow_dispatch:\njobs:\n  use_api_via_script:\n    runs-on: ubuntu-latest\n    permissions:\n      issues: read\n    steps:\n      - name: Check out repo content\n        uses: actions/checkout@v6\n\n      - name: Setup Node\n        uses: actions/setup-node@v4\n        with:\n          node-version: '16.17.0'\n          cache: npm\n\n      - name: Install dependencies\n        run: npm install octokit\n\n      - name: Run script\n        run: |\n          node .github/actions-scripts/use-the-api.mjs\n        env:\n          TOKEN: ${{ secrets.GITHUB_TOKEN }}\n```\n\nThe following is an example JavaScript script with the file path `.github/actions-scripts/use-the-api.mjs`.\n\n```javascript\nimport { Octokit } from \"octokit\"\n\nconst octokit = new Octokit({ \n  auth: process.env.TOKEN\n});\n\ntry {\n  const result = await octokit.request(\"GET /repos/{owner}/{repo}/issues\", {\n      owner: \"octocat\",\n      repo: \"Spoon-Knife\",\n    });\n\n  const titleAndAuthor = result.data.map(issue => {title: issue.title, authorID: issue.user.id})\n\n  console.log(titleAndAuthor)\n\n} catch (error) {\n  console.log(`Error! Status: ${error.status}. Message: ${error.response.data.message}`)\n}\n```\n\n### Authenticating with a GitHub App\n\nIf you are authenticating with a GitHub App, you can create an installation access token within your workflow:\n\n1. 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](/en/rest/apps/apps#get-an-app). For more information about configuration variables, see [Store information in variables](/en/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows).\n2. 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, replace `APP_PEM` with the name of the secret. For more information, see [Managing private keys for GitHub Apps](/en/apps/creating-github-apps/authenticating-with-a-github-app/managing-private-keys-for-github-apps). For more information about secrets, see [Using secrets in GitHub Actions](/en/actions/security-guides/encrypted-secrets).\n3. 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:\n\n   ```yaml\n   on:\n     workflow_dispatch:\n   jobs:\n     use_api_via_script:\n       runs-on: ubuntu-latest\n       steps:\n         - name: Check out repo content\n           uses: actions/checkout@v6\n\n         - name: Setup Node\n           uses: actions/setup-node@v4\n           with:\n             node-version: '16.17.0'\n             cache: npm\n\n         - name: Install dependencies\n           run: npm install octokit\n\n         - name: Generate token\n           id: generate-token\n           uses: actions/create-github-app-token@v2\n           with:\n             app-id: ${{ vars.APP_ID }}\n             private-key: ${{ secrets.APP_PEM }}\n\n         - name: Run script\n           run: |\n             node .github/actions-scripts/use-the-api.mjs\n           env:\n             TOKEN: ${{ steps.generate-token.outputs.token }}\n\n   ```\n\n</div>\n\n<div class=\"ghd-tool curl\">\n\n## Using `curl` in the command line\n\n> \\[!NOTE]\n> If you want to make API requests from the command line, GitHub recommends that you use GitHub CLI, which simplifies authentication and requests. For more information about getting started with the REST API using GitHub CLI, see the GitHub CLI version of this article.\n\n1. Install `curl` if it isn't already installed on your machine. To check if `curl` is installed, execute `curl --version` in the command line. If the output provides information about the version of `curl`, that means `curl` is installed. If you get a message similar to `command not found: curl`, you need to download and install `curl`. For more information, see [the curl project download page](https://curl.se/download.html).\n\n2. 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 the endpoint. For more information, see [Authenticating to the REST API](/en/rest/overview/authenticating-to-the-rest-api).\n\n   > \\[!WARNING]\n   > Treat your access token like a password.\n   >\n   > To keep your token secure, you can store your token as a Codespaces secret and use the command line through Codespaces. For more information, see [Managing encrypted secrets for your codespaces](/en/codespaces/managing-your-codespaces/managing-encrypted-secrets-for-your-codespaces).\n\n   > You can also use GitHub CLI instead of `curl`. GitHub CLI will take care of authentication for you. For more information, see the GitHub CLI version of this page.\n   >\n   > If these options are not possible, consider using another CLI service to store your token securely.\n\n3. Use the `curl` command to make your request. Pass your token in an `Authorization` header. Replace `YOUR-TOKEN` with your token.\n\n   ```shell copy\n   curl --request GET \\\n   --url \"https://api.github.com/repos/octocat/Spoon-Knife/issues\" \\\n   --header \"Accept: application/vnd.github+json\" \\\n   --header \"Authorization: Bearer YOUR-TOKEN\"\n   ```\n\n   > \\[!NOTE]\n   > In most cases, you can use `Authorization: Bearer` or `Authorization: token` to pass a token. However, if you are passing a JSON web token (JWT), you must use `Authorization: Bearer`.\n\n## Using `curl` commands in GitHub Actions\n\nYou can also use `curl` commands in your GitHub Actions workflows.\n\n### Authenticating with an access token\n\nGitHub 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 [Use GITHUB\\_TOKEN for authentication in workflows](/en/actions/security-guides/automatic-token-authentication). For more information about secrets, see [Using secrets in GitHub Actions](/en/actions/security-guides/encrypted-secrets).\n\n```yaml copy\non:\n  workflow_dispatch:\njobs:\n  use_api:\n    runs-on: ubuntu-latest\n    permissions:\n      issues: read\n    steps:\n      - env:\n          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n        run: |\n          curl --request GET \\\n          --url \"https://api.github.com/repos/octocat/Spoon-Knife/issues\" \\\n          --header \"Accept: application/vnd.github+json\" \\\n          --header \"Authorization: Bearer $GH_TOKEN\"\n```\n\n### Authenticating with a GitHub App\n\nIf you are authenticating with a GitHub App, you can create an installation access token within your workflow:\n\n1. 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](/en/rest/apps/apps#get-an-app). For more information about configuration variables, see [Store information in variables](/en/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows).\n2. 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, replace `APP_PEM` with the name of the secret. For more information, see [Managing private keys for GitHub Apps](/en/apps/creating-github-apps/authenticating-with-a-github-app/managing-private-keys-for-github-apps). For more information about storing secrets, see [Using secrets in GitHub Actions](/en/actions/security-guides/encrypted-secrets).\n3. 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:\n\n   ```yaml copy\n   on:\n     workflow_dispatch:\n   jobs:\n     use_api:\n       runs-on: ubuntu-latest\n       steps:\n         - name: Generate token\n           id: generate-token\n           uses: actions/create-github-app-token@v2\n           with:\n             app-id: ${{ vars.APP_ID }}\n             private-key: ${{ secrets.APP_PEM }}\n\n         - name: Use API\n           env:\n             GH_TOKEN: ${{ steps.generate-token.outputs.token }}\n           run: |\n             curl --request GET \\\n             --url \"https://api.github.com/repos/octocat/Spoon-Knife/issues\" \\\n             --header \"Accept: application/vnd.github+json\" \\\n             --header \"Authorization: Bearer $GH_TOKEN\"\n\n   ```\n\n</div>\n\n## Next steps\n\nFor a more detailed guide, see [Getting started with the REST API](/en/rest/guides/getting-started-with-the-rest-api)."}