- Notifications
You must be signed in to change notification settings - Fork40
GitHub Apps toolset for Node.js
License
octokit/app.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
GitHub App toolset for Node.js
Browsers |
|
---|---|
Node | Install with const{ App, createNodeMiddleware}=require("@octokit/app"); |
Important
As we useconditional exports, you will need to adapt yourtsconfig.json
by setting"moduleResolution": "node16", "module": "node16"
.
See the TypeScript docs onpackage.json "exports".
See thishelpful guide on transitioning to ESM from@sindresorhus
constapp=newApp({appId:123,privateKey:"-----BEGIN PRIVATE KEY-----\n...",oauth:{clientId:"0123",clientSecret:"0123secret",},webhooks:{secret:"secret",},});const{ data}=awaitapp.octokit.request("/app");console.log("authenticated as %s",data.name);forawait(const{ installation}ofapp.eachInstallation.iterator()){forawait(const{ octokit, repository}ofapp.eachRepository.iterator({installationId:installation.id,})){awaitoctokit.request("POST /repos/{owner}/{repo}/dispatches",{owner:repository.owner.login,repo:repository.name,event_type:"my_event",});}}app.webhooks.on("issues.opened",async({ octokit, payload})=>{awaitoctokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments",{owner:payload.repository.owner.login,repo:payload.repository.name,issue_number:payload.issue.number,body:"Hello World!",},);});app.oauth.on("token",async({ token, octokit})=>{const{ data}=awaitoctokit.request("GET /user");console.log(`Token retrieved for${data.login}`);});require("http").createServer(createNodeMiddleware(app)).listen(3000);// can now receive requests at /api/github/*
Create a newApp
with custom defaults for theconstructor options
constMyApp=App.defaults({Octokit:MyOctokit,});constapp=newMyApp({ clientId, clientSecret});// app.octokit is now an instance of MyOctokit
name | type | description |
---|---|---|
appId | number | Required. Find theApp ID on the app’s about page in settings. |
privateKey | string | Required. Content of the*.pem file you downloaded from the app’s about page. You can generate a new private key if needed. |
Octokit | Constructor | You can pass in your own Octokit constructor with custom defaults and plugins. Note that For usage with enterprise, set const{ Octokit}=require("@octokit/core");newApp({appId:123,privateKey:"-----BEGIN PRIVATE KEY-----\n...",oauth:{clientId:123,clientSecret:"secret",},webhooks:{secret:"secret",},Octokit:Octokit.defaults({baseUrl:"https://ghe.my-company.com/api/v3",}),}); Defaults to |
log | object | Used for internal logging. Defaults toconsole . |
webhooks.secret | string | Required. Secret as configured in the GitHub App's settings. |
webhooks.transform | function | Only relevant for `app.webhooks.on`. Transform emitted event before calling handlers. Can be asynchronous. |
oauth.clientId | number | Find the OAuthClient ID on the app’s about page in settings. |
oauth.clientSecret | number | Find the OAuthClient Secret on the app’s about page in settings. |
oauth.allowSignup | boolean | Sets the default value forapp.oauth.getAuthorizationUrl(options) . |
Octokit instance. Uses theOctokit
constructor option if passed.
Seehttps://github.com/octokit/core.js#logging. Customize using thelog
constructor option.
constoctokit=awaitapp.getInstallationOctokit(123);
forawait(const{ octokit, installation}ofapp.eachInstallation.iterator()){/* ... */}awaitapp.eachInstallation(({ octokit, installation})=>/* ... */)
forawait(const{ octokit, repository}ofapp.eachRepository.iterator()){/* ... */}awaitapp.eachRepository(({ octokit, repository})=>/* ... */)
Optionally pass installation ID to iterate through all repositories in one installation
forawait(const{ octokit, repository}ofapp.eachRepository.iterator({ installationId})){/* ... */}awaitapp.eachRepository({ installationId},({ octokit, repository})=>/* ... */)
constinstallationUrl=awaitapp.getInstallationUrl();returnres.redirect(installationUrl);
Optionally pass the ID of a GitHub organization or user to request installation on that specific target.
If the user will be sent to a redirect URL after installation (such as if you request user authorization during installation), you can also supply astate
string that will be included in the query of the post-install redirect.
constinstallationUrl=awaitapp.getInstallationUrl({ state, target_id});returnres.redirect(installationUrl);
A middleware is a method or set of methods to handle requests for common environments.
By default, all middlewares expose the following routes
Route | Route Description |
---|---|
POST /api/github/webhooks | Endpoint to receive GitHub Webhook Event requests |
GET /api/github/oauth/login | Redirects to GitHub's authorization endpoint. Accepts optional?state query parameter. |
GET /api/github/oauth/callback | The client's redirect endpoint. This is where thetoken event gets triggered |
POST /api/github/oauth/token | Exchange an authorization code for an OAuth Access token. If successful, thetoken event gets triggered. |
GET /api/github/oauth/token | Check if token is valid. Must authenticate using token inAuthorization header. Uses GitHub'sPOST /applications/{client_id}/token endpoint |
PATCH /api/github/oauth/token | Resets a token (invalidates current one, returns new token). Must authenticate using token inAuthorization header. Uses GitHub'sPATCH /applications/{client_id}/token endpoint. |
DELETE /api/github/oauth/token | Invalidates current token, basically the equivalent of a logout. Must authenticate using token inAuthorization header. |
DELETE /api/github/oauth/grant | Revokes the user's grant, basically the equivalent of an uninstall. must authenticate using token inAuthorization header. |
Middleware for Node's built in http server orexpress
.
const{ App, createNodeMiddleware}=require("@octokit/app");constapp=newApp({appId:123,privateKey:"-----BEGIN PRIVATE KEY-----\n...",oauth:{clientId:"0123",clientSecret:"0123secret",},webhooks:{secret:"secret",},});constmiddleware=createNodeMiddleware(app);require("http").createServer(async(req,res)=>{// `middleware` returns `false` when `req` is unhandled (beyond `/api/github`)if(awaitmiddleware(req,res))return;res.writeHead(404);res.end();}).listen(3000);// can now receive user authorization callbacks at /api/github/*
The middleware returned fromcreateNodeMiddleware
can also serve as anExpress.js
middleware directly.
name | type | description |
---|---|---|
app | App instance | Required. |
options.pathPrefix | string | All exposed paths will be prefixed with the provided prefix. Defaults to |
log object | Used for internal logging. Defaults to
|
About
GitHub Apps toolset for Node.js
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.