1
0
mirror of synced 2025-12-26 14:02:45 -05:00
Files
docs/content/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app.md
Sarah Edwards 0507884ca1 Authenticating as an app (#34845)
Co-authored-by: Hirsch Singhal <1666363+hpsin@users.noreply.github.com>
Co-authored-by: Laura Coursen <lecoursen@github.com>
2023-03-01 18:48:51 +00:00

3.7 KiB

title, intro, versions, topics, shortTitle
title intro versions topics shortTitle
Authenticating as a GitHub App You can authenticate as a {% data variables.product.prodname_github_app %} in order to generate an installation access token or manage your app.
fpt ghes ghae ghec
* * * *
GitHub Apps
Authenticate as an app

About authentication as a {% data variables.product.prodname_github_app %}

You must authenticate as a {% data variables.product.prodname_github_app %} in order to make REST API requests as the application. For example, if you want to use the API to generate an installation access token for accessing organization resources, list installations across organizations for your app, or suspend an app installation, you must authenticate as an app.

If a REST API endpoint requires you to authenticate as an app, the documentation for that endpoint will indicate that you must use a JWT to access the endpoint. The GraphQL API does not support any queries or mutations that require you to authenticate as an app.

Using a JSON Web Token (JWT) to authenticate as a {% data variables.product.prodname_github_app %}

  1. Generate a JSON Web Token (JWT) for your app. For more information, see "AUTOTITLE."

  2. Include the JWT in the Authorization header of your request. In the following example, replace YOUR_JWT with your JWT.

    curl --request POST \
    --url "{% data variables.product.api_url_pre %}/app/installations" \
    --header "Accept: application/vnd.github+json" \
    --header "Authorization: Bearer YOUR_JWT"{% ifversion api-date-versioning %}\
    --header "X-GitHub-Api-Version: {{ allVersions[currentVersion].latestApiVersion }}"{% endif %}
    

Using the Octokit.js SDK to authenticate as a {% data variables.product.prodname_github_app %}

You can use {% data variables.product.company_short %}'s Octokit.js SDK to authenticate as a {% data variables.product.prodname_github_app %}. One advantage of using the SDK to authenticate is that you do not need to generate a JSON web token (JWT) yourself. Additionally, the SDK will take care of regenerating the JWT when it expires.

{% note %}

Note: You must install and import octokit in order to use the Octokit.js library. The following example uses import statements in accordance with ES6. For more information about different installation and import methods, see Usage in the octokit/octokit repository.

{% endnote %}

  1. On the settings page for your app, get the app's ID.

    • For user-owned apps, the settings page is https://github.com/settings/apps/APP-SLUG.
    • For organization-owned apps, the settings page is https://github.com/organizations/ORGANIZATION/settings/apps/APP-SLUG.

    Replace APP-SLUG with the sluggified name of your app and ORGANIZATION with the sluggified name of your organization. For example, https://github.com/organizations/octo-org/settings/apps/octo-app.

  2. Generate a private key. For more information, see "AUTOTITLE."

  3. Import App from octokit.

    import { App } from "octokit";
    
  4. Create a new instance of App. In the following example, replace APP_ID with a reference to your app's ID. Replace PRIVATE_KEY with a reference to the value of your app's private key.

     const app = new App({
      appId: APP_ID,
      privateKey: PRIVATE_KEY,
    });
    
  5. Use an octokit method to make a request to a REST API endpoint that requires a JWT. For example:

    await app.octokit.request("/app")