Skip to content

Commit 4a5bdd4

Browse files
committed
feat(README): authentication
1 parent afa90e9 commit 4a5bdd4

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ request("POST /repos/:owner/:repo/issues/:number/labels", {
4949
});
5050
```
5151

52+
👶 [Small bundle size](https://bundlephobia.com/result?p=@octokit/request@5.0.3) (\<4kb minified + gzipped)
53+
54+
😎 [Authenticate](#authentication) with any of [GitHubs Authentication Strategies](https://github.com/octokit/auth.js).
55+
5256
👍 Sensible defaults
5357

5458
- `baseUrl`: `https://api.github.com`
@@ -59,8 +63,6 @@ request("POST /repos/:owner/:repo/issues/:number/labels", {
5963

6064
🧐 Simple to debug: Sets `error.request` to request options causing the error (with redacted credentials).
6165

62-
👶 Small bundle size (\<5kb minified + gzipped)
63-
6466
## Usage
6567

6668
<table>
@@ -110,6 +112,8 @@ console.log(`${result.data.length} repos found.`);
110112

111113
### GraphQL example
112114

115+
For GraphQL request we recommend using [`@octokit/graphql`](https://github.com/octokit/graphql.js#readme)
116+
113117
```js
114118
const result = await request("POST /graphql", {
115119
headers: {
@@ -144,6 +148,45 @@ const result = await request({
144148
});
145149
```
146150

151+
## Authentication
152+
153+
The simplest way to authenticate a request is to set the `Authorization` header directly, e.g. to a [personal access token](https://github.com/settings/tokens/).
154+
155+
```js
156+
const requestWithAuth = request.defaults({
157+
headers: {
158+
authorization: "token 0000000000000000000000000000000000000001"
159+
}
160+
});
161+
const result = await request("GET /user");
162+
```
163+
164+
For more complex authentication strategies such as GitHub Apps or Basic, we recommend the according authentication library exported by [`@octokit/auth`](https://github.com/octokit/auth.js).
165+
166+
```js
167+
const { createAppAuth } = require("@octokit/auth-app");
168+
const auth = createAppAuth({
169+
id: process.env.APP_ID,
170+
privateKey: process.env.PRIVATE_KEY,
171+
installationId: 123
172+
});
173+
const requestWithAuth = request.defaults({
174+
request: {
175+
hook: auth.hook
176+
},
177+
mediaType: {
178+
previews: ["machine-man"]
179+
}
180+
});
181+
182+
const { data: app } = await requestWithAuth("GET /app");
183+
const { data: app } = await requestWithAuth("POST /repos/:owner/:repo/issues", {
184+
owner: "octocat",
185+
repo: "hello-world",
186+
title: "Hello from the engine room"
187+
});
188+
```
189+
147190
## request()
148191

149192
`request(route, options)` or `request(options)`.

0 commit comments

Comments
 (0)