Gathering detailed insights and metrics for octokit
Gathering detailed insights and metrics for octokit
Gathering detailed insights and metrics for octokit
Gathering detailed insights and metrics for octokit
The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.
npm install octokit
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7,045 Stars
409 Commits
1,033 Forks
154 Watching
12 Branches
50 Contributors
Updated on 27 Nov 2024
TypeScript (86.51%)
JavaScript (13.49%)
Cumulative downloads
Total Downloads
Last day
-2.5%
114,279
Compared to previous day
Last week
0.1%
605,859
Compared to previous week
Last month
6.6%
2,650,604
Compared to previous month
Last year
79.3%
27,189,227
Compared to previous year
10
The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.
The octokit
package integrates the three main Octokit libraries
Browsers |
Load octokit directly from esm.sh
|
---|---|
Deno |
Load octokit directly from esm.sh
|
Node |
Install with
|
[!IMPORTANT] As we use conditional exports, you will need to adapt your
tsconfig.json
by setting"moduleResolution": "node16", "module": "node16"
.See the TypeScript docs on package.json "exports".
See this helpful guide on transitioning to ESM from @sindresorhus
Octokit
API Clientstandalone minimal Octokit: @octokit/core
.
The Octokit
client can be used to send requests to GitHub's REST API and queries to GitHub's GraphQL API.
Example: Get the username for the authenticated user.
1// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo 2const octokit = new Octokit({ auth: `personal-access-token123` }); 3 4// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user 5const { 6 data: { login }, 7} = await octokit.rest.users.getAuthenticated(); 8console.log("Hello, %s", login);
The most commonly used options are
name | type | description |
---|---|---|
userAgent
|
String
|
Setting a user agent is required for all requests sent to GitHub's Platform APIs. The user agent defaults to something like this:
|
authStrategy
|
Function
|
Defaults to See Authentication below. |
auth
|
String or Object
|
Set to a personal access token unless you changed the See Authentication below. |
baseUrl
|
String
|
When using with GitHub Enterprise Server, set
|
Advanced options
name | type | description |
---|---|---|
request
|
Object
|
Node only
The |
timeZone
|
String
|
Sets the
The time zone header will determine the timezone used for generating the timestamp when creating commits. See GitHub's Timezones documentation. |
throttle
|
Object
|
By default, requests are retried once and warnings are logged in case of hitting a rate or secondary rate limit.
To opt-out of this feature:
Throttling in a cluster is supported using a Redis backend. See |
retry
|
Object
|
To opt-out of this feature:
|
By default, the Octokit
API client supports authentication using a static token.
There are different means of authentication that are supported by GitHub, that are described in detail at octokit/authentication-strategies.js. You can set each of them as the authStrategy
constructor option, and pass the strategy options as the auth
constructor option.
For example, in order to authenticate as a GitHub App Installation:
1import { createAppAuth } from "@octokit/auth-app"; 2const octokit = new Octokit({ 3 authStrategy: createAppAuth, 4 auth: { 5 appId: 1, 6 privateKey: "-----BEGIN PRIVATE KEY-----\n...", 7 installationId: 123, 8 }, 9}); 10 11// authenticates as app based on request URLs 12const { 13 data: { slug }, 14} = await octokit.rest.apps.getAuthenticated(); 15 16// creates an installation access token as needed 17// assumes that installationId 123 belongs to @octocat, otherwise the request will fail 18await octokit.rest.issues.create({ 19 owner: "octocat", 20 repo: "hello-world", 21 title: "Hello world from " + slug, 22});
You can use the App
or OAuthApp
SDKs which provide APIs and internal wiring to cover most use cases.
For example, to implement the above using App
1const app = new App({ appId, privateKey }); 2const { data: slug } = await app.octokit.rest.apps.getAuthenticated(); 3const octokit = await app.getInstallationOctokit(123); 4await octokit.rest.issues.create({ 5 owner: "octocat", 6 repo: "hello-world", 7 title: "Hello world from " + slug, 8});
Learn more about how authentication strategies work or how to create your own.
By default, the Octokit
API client does not make use of the standard proxy server environment variables. To add support for proxy servers you will need to provide an https client that supports them such as undici.ProxyAgent()
.
For example, this would use a ProxyAgent
to make requests through a proxy server:
1import { fetch as undiciFetch, ProxyAgent } from 'undici';
2
3const myFetch = (url, options) => {
4 return undiciFetch(url, {
5 ...options,
6 dispatcher: new ProxyAgent(<your_proxy_url>)
7 })
8}
9
10const octokit = new Octokit({
11 request: {
12 fetch: myFetch
13 },
14});
If you are writing a module that uses Octokit
and is designed to be used by other people, you should ensure that consumers can provide an alternative agent for your Octokit
or as a parameter to specific calls such as:
1import { fetch as undiciFetch, ProxyAgent } from 'undici'; 2 3const myFetch = (url, options) => { 4 return undiciFetch(url, { 5 ...options, 6 dispatcher: new ProxyAgent(<your_proxy_url>) 7 }) 8} 9 10octokit.rest.repos.get({ 11 owner, 12 repo, 13 request: { 14 fetch: myFetch 15 }, 16});
If you get the following error:
fetch is not set. Please pass a fetch implementation as new Octokit({ request: { fetch }}).
It probably means you are trying to run Octokit with an unsupported version of NodeJS. Octokit requires Node 18 or higher, which includes a native fetch API.
To bypass this problem you can provide your own fetch
implementation (or a built-in version like node-fetch
) like this:
1import fetch from "node-fetch"; 2 3const octokit = new Octokit({ 4 request: { 5 fetch: fetch, 6 }, 7});
There are two ways of using the GitHub REST API, the octokit.rest.*
endpoint methods and octokit.request
. Both act the same way, the octokit.rest.*
methods are just added for convenience, they use octokit.request
internally.
For example
1await octokit.rest.issues.create({ 2 owner: "octocat", 3 repo: "hello-world", 4 title: "Hello, world!", 5 body: "I created this issue using Octokit!", 6});
Is the same as
1await octokit.request("POST /repos/{owner}/{repo}/issues", { 2 owner: "octocat", 3 repo: "hello-world", 4 title: "Hello, world!", 5 body: "I created this issue using Octokit!", 6});
In both cases a given request is authenticated, retried, and throttled transparently by the octokit
instance which also manages the accept
and user-agent
headers as needed.
octokit.request
can be used to send requests to other domains by passing a full URL and to send requests to endpoints that are not (yet) documented in GitHub's REST API documentation.
octokit.rest
endpoint methodsEvery GitHub REST API endpoint has an associated octokit.rest
endpoint method for better code readability and developer convenience. See @octokit/plugin-rest-endpoint-methods
for full details.
Example: Create an issue
1await octokit.rest.issues.create({ 2 owner: "octocat", 3 repo: "hello-world", 4 title: "Hello, world!", 5 body: "I created this issue using Octokit!", 6});
The octokit.rest
endpoint methods are generated automatically from GitHub's OpenAPI specification. We track operation ID and parameter name changes in order to implement deprecation warnings and reduce the frequency of breaking changes.
Under the covers, every endpoint method is just octokit.request
with defaults set, so it supports the same parameters as well as the .endpoint()
API.
octokit.request()
You can call the GitHub REST API directly using octokit.request
. The request
API matches GitHub's REST API documentation 1:1 so anything you see there, you can call using request
. See @octokit/request
for all the details.
Example: Create an issue
The octokit.request
API call corresponding to that issue creation documentation looks like this:
1// https://docs.github.com/en/rest/reference/issues#create-an-issue 2await octokit.request("POST /repos/{owner}/{repo}/issues", { 3 owner: "octocat", 4 repo: "hello-world", 5 title: "Hello, world!", 6 body: "I created this issue using Octokit!", 7});
The 1st argument is the REST API route as listed in GitHub's API documentation. The 2nd argument is an object with all parameters, independent of whether they are used in the path, query, or body.
All REST API endpoints that paginate return the first 30 items by default. If you want to retrieve all items, you can use the pagination API. The pagination API expects the REST API route as first argument, but you can also pass any of the octokit.rest.*.list*
methods for convenience and better code readability.
Example: iterate through all issues in a repository
1const iterator = octokit.paginate.iterator(octokit.rest.issues.listForRepo, { 2 owner: "octocat", 3 repo: "hello-world", 4 per_page: 100, 5}); 6 7// iterate through each response 8for await (const { data: issues } of iterator) { 9 for (const issue of issues) { 10 console.log("Issue #%d: %s", issue.number, issue.title); 11 } 12}
Using the async iterator is the most memory efficient way to iterate through all items. But you can also retrieve all items in a single call
1const issues = await octokit.paginate(octokit.rest.issues.listForRepo, { 2 owner: "octocat", 3 repo: "hello-world", 4 per_page: 100, 5});
Media type formats can be set using mediaType: { format }
on every request.
Example: retrieve the raw content of a package.json
file
1const { data } = await octokit.rest.repos.getContent({ 2 mediaType: { 3 format: "raw", 4 }, 5 owner: "octocat", 6 repo: "hello-world", 7 path: "package.json", 8}); 9console.log("package name: %s", JSON.parse(data).name);
Learn more about Media type formats.
Standalone module: @octokit/request-error
For request error handling, import RequestError
and use try...catch
statement.
1import { RequestError } from "octokit";
1try { 2 // your code here that sends at least one Octokit request 3 await octokit.request("GET /"); 4} catch (error) { 5 // Octokit errors are instances of RequestError, so they always have an `error.status` property containing the HTTP response code. 6 if (error instanceof RequestError) { 7 // handle Octokit error 8 // error.message; // Oops 9 // error.status; // 500 10 // error.request; // { method, url, headers, body } 11 // error.response; // { url, status, headers, data } 12 } else { 13 // handle all other errors 14 throw error; 15 } 16}
Octokit also supports GitHub's GraphQL API directly -- you can use the same queries shown in the documentation and available in the GraphQL explorer in your calls with octokit.graphql
.
Example: get the login of the authenticated user
1const { 2 viewer: { login }, 3} = await octokit.graphql(`{ 4 viewer { 5 login 6 } 7}`);
Variables can be passed as 2nd argument
1const { lastIssues } = await octokit.graphql( 2 ` 3 query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { 4 repository(owner: $owner, name: $repo) { 5 issues(last: $num) { 6 edges { 7 node { 8 title 9 } 10 } 11 } 12 } 13 } 14 `, 15 { 16 owner: "octokit", 17 repo: "graphql.js", 18 }, 19);
GitHub's GraphQL API returns a maximum of 100 items. If you want to retrieve all items, you can use the pagination API.
Example: get all issues
1const { allIssues } = await octokit.graphql.paginate( 2 ` 3 query allIssues($owner: String!, $repo: String!, $num: Int = 10, $cursor: String) { 4 repository(owner: $owner, name: $repo) { 5 issues(first: $num, after: $cursor) { 6 edges { 7 node { 8 title 9 } 10 } 11 pageInfo { 12 hasNextPage 13 endCursor 14 } 15 } 16 } 17 } 18 `, 19 { 20 owner: "octokit", 21 repo: "graphql.js", 22 }, 23);
Learn more about GitHub's GraphQL Pagination usage.
Previews can be enabled using the {mediaType: previews: [] }
option.
Example: create a label
1await octokit.graphql( 2 `mutation createLabel($repositoryId:ID!,name:String!,color:String!) { 3 createLabel(input:{repositoryId:$repositoryId,name:$name}) { 4 label: { 5 id 6 } 7 } 8}`, 9 { 10 repositoryId: 1, 11 name: "important", 12 color: "cc0000", 13 mediaType: { 14 previews: ["bane"], 15 }, 16 }, 17);
Learn more about GitHub's GraphQL schema previews
The App
client combines features for GitHub Apps, Webhooks, and OAuth
Standalone module: @octokit/app
For integrators, GitHub Apps are a means of authentication and authorization. A GitHub app can be registered on a GitHub user or organization account. A GitHub App registration defines a set of permissions and webhooks events it wants to receive and provides a set of credentials in return. Users can grant access to repositories by installing them.
Some API endpoints require the GitHub app to authenticate as itself using a JSON Web Token (JWT). For requests affecting an installation, an installation access token has to be created using the app's credentials and the installation ID.
The App
client takes care of all that for you.
Example: Dispatch a repository event in every repository the app is installed on
1import { App } from "octokit";
2
3const app = new App({ appId, privateKey });
4
5for await (const { octokit, repository } of app.eachRepository.iterator()) {
6 // https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event
7 await octokit.rest.repos.createDispatchEvent({
8 owner: repository.owner.login,
9 repo: repository.name,
10 event_type: "my_event",
11 client_payload: {
12 foo: "bar",
13 },
14 });
15 console.log("Event dispatched for %s", repository.full_name);
16}
Example: Get an octokit
instance authenticated as an installation
1const octokit = await app.getInstallationOctokit(123);
Learn more about apps.
Standalone module: @octokit/webhooks
When installing an app, events that the app registration requests will be sent as requests to the webhook URL set in the app's registration.
Webhook event requests are signed using the webhook secret, which is also part of the app's registration. You must verify that secret before handling the request payload.
The app.webhooks.*
APIs provide methods to receiving, verifying, and handling webhook events.
Example: create a comment on new issues
1import { createServer } from "node:http";
2import { App, createNodeMiddleware } from "octokit";
3
4const app = new App({
5 appId,
6 privateKey,
7 webhooks: { secret },
8});
9
10app.webhooks.on("issues.opened", ({ octokit, payload }) => {
11 return octokit.rest.issues.createComment({
12 owner: payload.repository.owner.login,
13 repo: payload.repository.name,
14 issue_number: payload.issue.number,
15 body: "Hello, World!",
16 });
17});
18
19// Your app can now receive webhook events at `/api/github/webhooks`
20createServer(createNodeMiddleware(app)).listen(3000);
For serverless environments, you can explicitly verify and receive an event
1await app.webhooks.verifyAndReceive({
2 id: request.headers["x-github-delivery"],
3 name: request.headers["x-github-event"],
4 signature: request.headers["x-hub-signature-256"],
5 payload: request.body,
6});
Learn more about GitHub webhooks.
Standalone module: @octokit/oauth-app
Both OAuth Apps and GitHub Apps support authenticating GitHub users using OAuth, see Authorizing OAuth Apps and Identifying and authorizing users for GitHub Apps.
There are some differences:
App
is for GitHub Apps. If you need OAuth App-specific functionality, use OAuthApp
instead.
Example: Watch a repository when a user logs in using the OAuth web flow
1import { createServer } from "node:http";
2import { App, createNodeMiddleware } from "octokit";
3
4const app = new App({
5 oauth: { clientId, clientSecret },
6});
7
8app.oauth.on("token.created", async ({ token, octokit }) => {
9 await octokit.rest.activity.setRepoSubscription({
10 owner: "octocat",
11 repo: "hello-world",
12 subscribed: true,
13 });
14});
15
16// Your app can receive the OAuth redirect at /api/github/oauth/callback
17// Users can initiate the OAuth web flow by opening /api/github/oauth/login
18createServer(createNodeMiddleware(app)).listen(3000);
For serverless environments, you can explicitly exchange the code
from the OAuth web flow redirect for an access token.
app.oauth.createToken()
returns an authentication object and emits the "token.created" event.
1const { token } = await app.oauth.createToken({
2 code: request.query.code,
3});
Example: create a token using the device flow.
1const { token } = await app.oauth.createToken({
2 async onVerification(verification) {
3 await sendMessageToUser(
4 request.body.phoneNumber,
5 `Your code is ${verification.user_code}. Enter it at ${verification.verification_uri}`,
6 );
7 },
8});
Example: Create an OAuth App Server with default scopes
1import { createServer } from "node:http"; 2import { OAuthApp, createNodeMiddleware } from "octokit"; 3 4const app = new OAuthApp({ 5 clientId, 6 clientSecret, 7 defaultScopes: ["repo", "gist"], 8}); 9 10app.oauth.on("token", async ({ token, octokit }) => { 11 await octokit.rest.gists.create({ 12 description: "I created this gist using Octokit!", 13 public: true, 14 files: { 15 "example.js": `/* some code here */`, 16 }, 17 }); 18}); 19 20// Your app can receive the OAuth redirect at /api/github/oauth/callback 21// Users can initiate the OAuth web flow by opening /api/oauth/login 22createServer(createNodeMiddleware(app)).listen(3000);
After registering your GitHub app, you need to create and deploy a server which can retrieve the webhook event requests from GitHub as well as accept redirects from the OAuth user web flow.
The simplest way to create such a server is to use createNodeMiddleware()
, it works with both, Node's http.createServer()
method as well as an Express middleware.
The default routes that the middleware exposes are
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 and ?scopes query parameters. ?scopes is a comma-separated list of supported OAuth scope names |
GET /api/github/oauth/callback | The client's redirect endpoint. This is where the token event gets triggered |
POST /api/github/oauth/token | Exchange an authorization code for an OAuth Access token. If successful, the token event gets triggered. |
GET /api/github/oauth/token | Check if token is valid. Must authenticate using token in Authorization header. Uses GitHub's POST /applications/{client_id}/token endpoint |
PATCH /api/github/oauth/token | Resets a token (invalidates current one, returns new token). Must authenticate using token in Authorization header. Uses GitHub's PATCH /applications/{client_id}/token endpoint. |
PATCH /api/github/oauth/refresh-token | Refreshes an expiring token (invalidates current one, returns new access token and refresh token). Must authenticate using token in Authorization header. Uses GitHub's POST https://github.com/login/oauth/access_token OAuth endpoint. |
POST /api/github/oauth/token/scoped | Creates a scoped token (does not invalidate the current one). Must authenticate using token in Authorization header. Uses GitHub's POST /applications/{client_id}/token/scoped endpoint. |
DELETE /api/github/oauth/token | Invalidates current token, basically the equivalent of a logout. Must authenticate using token in Authorization header. |
DELETE /api/github/oauth/grant | Revokes the user's grant, basically the equivalent of an uninstall. must authenticate using token in Authorization header. |
Example: create a GitHub server with express
1import express from "express";
2import { App, createNodeMiddleware } from "octokit";
3
4const expressApp = express();
5const octokitApp = new App({
6 appId,
7 privateKey,
8 webhooks: { secret },
9 oauth: { clientId, clientSecret },
10});
11
12expressApp.use(createNodeMiddleware(app));
13
14expressApp.listen(3000, () => {
15 console.log(`Example app listening at http://localhost:3000`);
16});
You must not expose your app's client secret to the user, so you cannot use the App
constructor. Instead, you have to create a server using the App
constructor which exposes the /api/github/oauth/*
routes, through which you can safely implement an OAuth login for apps running in a web browser.
If you set (User) Authorization callback URL
to your own app, than you need to read out the ?code=...&state=...
query parameters, compare the state
parameter to the value returned by app.oauthLoginUrl()
earlier to protect against forgery attacks, then exchange the code
for an OAuth Authorization token.
If you run an app server as described above, the default route to do that is POST /api/github/oauth/token
.
Once you successfully retrieved the token, it is also recommended to remove the ?code=...&state=...
query parameters from the browser's URL
1const code = new URL(location.href).searchParams.get("code"); 2if (code) { 3 // remove ?code=... from URL 4 const path = 5 location.pathname + 6 location.search.replace(/\b(code|state)=\w+/g, "").replace(/[?&]+$/, ""); 7 history.replaceState({}, "", path); 8 9 // exchange the code for a token with your backend. 10 // If you use https://github.com/octokit/oauth-app.js 11 // the exchange would look something like this 12 const response = await fetch("/api/github/oauth/token", { 13 method: "POST", 14 headers: { 15 "content-type": "application/json", 16 }, 17 body: JSON.stringify({ code }), 18 }); 19 const { token } = await response.json(); 20 // `token` is the OAuth Access Token that can be use 21 22 const { Octokit } = await import("https://esm.sh/@octokit/core"); 23 const octokit = new Octokit({ auth: token }); 24 25 const { 26 data: { login }, 27 } = await octokit.request("GET /user"); 28 alert("Hi there, " + login); 29}
🚧 We are working on @octokit/auth-oauth-user-client
to provide a simple API for all methods related to OAuth user tokens.
The plan is to add an new GET /api/github/oauth/octokit.js
route to the node middleware which will return a JavaScript file that can be imported into an HTML file. It will make a pre-authenticated octokit
Instance available.
standalone module: @octokit/action
🚧 A fully fledged Action
client is pending. You can use @actions/github
for the time being
The latest stable version of the package.
Stable Version
1
8.2/10
Summary
Unauthenticated Denial of Service in the octokit/webhooks library
Affected Versions
< 3.1.2
Patched Versions
3.1.2
Reason
all changesets reviewed
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
16 commit(s) and 8 issue activity found in the last 90 days -- score normalized to 10
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
SAST tool is run on all commits
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More